Hey Stonge,
Remeber the big difference between javascript and php: Javascript is CLIENT side scripting, and thus depends on the users-system.
PhP is SERVER side scripting, and thus happens on the server. $flag seems like a PhP variable. Thus you need to do through your php script (Unless you want to go grabbing url-s etc).
I suggest you look into the header("Location: somepage.php"); option:
if($flag==1)
{
header("Location: somepage.php");
}
Hope this helps,
J.