tarad
06-30-2008, 03:52 PM
Hello! I wasn't really sure where to post this, since its sort of a combo of PHP and Javascript, but I won't be offended if this needs to be moved.
I can't post the whole project that I'm working on (its really involved and way too long, and this is just one function that I'm trying to get working) so I've created a simplified page, just trying to get this function to work.
Here's some background: I have a form. It has multiple submit buttons and different actions are taken on the form by the PHP above it reading from the $_POST variable (ie if(isset($_POST[update])), etc.) What I need to happen (this is not in the simplified page, this is just background) is: after a submit button is clicked, the session variables to be set BEFORE a pop up window is called that will use the session variables. So, in my PHP function that sets the session variables, at the end I have a Javascript call to pop up a window, like this: echo "\n <script language='JavaScript'> window.open('forward_task.php','mywindow','width=500,height=500,scrollbars=yes') </script> \n";
But my problem was how to get this to work. If I just used the isset and the name of the button, if the user refreshed the page the $_POST would still be set and the window would pop up again, even though they didn't click the button again. So, I was trying to think of a way to only call the function that sets the session variable and pops up the window ONLY IF the user just clicked the button. So I thought that maybe I could have an Javascript onclick event that would reload the page, with a parameter in the URL, say forward=true or something like that. Then, if that was set, call the function that sets the session variable and pops up the window. This is where I stopped, because I couldn't get even this to work - but my plans are to then refresh the page (either with Javascript or PHP) without the forward=true, so that the function wouldn't fire again if the user hit refresh.
Now, there's probably a wayyyyyy easier way to do this, so if you have any thoughts, let me know. Here's the simplified page, try.php, in PHP, HTML, and Javascript:
<?php
if (isset($_GET[forward]))
{
saveForForward();
}
function saveForForward()
{
/*Just an example, doing a little PHP then executing some Javascript*/
$name = $_POST[thename];
$name = "The Name Was: " . $name;
echo "\n <script language='JavaScript'> alert($name); </script> \n";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script language='JavaScript'>
function fwd_to_URL()
{
var theURL = "try.php?forward=true";
window.location.href = theURL;
return true;
}
</script>
<?php
var_dump($_POST);
?>
<form name=form1 method=post enctype="multipart/form-data">
<p> Name:
<input type="text" name="thename">
</p>
<input type="submit" value="submit" name="submit">
<input type="submit" value="check_name" name="check name" onClick="return fwd_to_URL();">
</form>
</body>
</html>
I know I've probably written enough already, but here's my thoughts on the order of events, if its possible:
onClick - submit the $_POST, reload the page with forward=true in the URL
if $_GET[forward]==true, or whatever, fire the save_for_forward() function (which would set the session, and pops up the window.
ideally, then I would reload the page without the forward=true in the URL
And by the way, this did work for me in two isolated incidents, lol, but I couldn't remember what combination of semi-colons, returns, and events that made it work. :/
Any ideas or help would be appreciated. If I need to totally rethink the process, that's fine too, I won't be offended. I've been feeling like I've just been putting bandaids on things and finding workarounds.
I can't post the whole project that I'm working on (its really involved and way too long, and this is just one function that I'm trying to get working) so I've created a simplified page, just trying to get this function to work.
Here's some background: I have a form. It has multiple submit buttons and different actions are taken on the form by the PHP above it reading from the $_POST variable (ie if(isset($_POST[update])), etc.) What I need to happen (this is not in the simplified page, this is just background) is: after a submit button is clicked, the session variables to be set BEFORE a pop up window is called that will use the session variables. So, in my PHP function that sets the session variables, at the end I have a Javascript call to pop up a window, like this: echo "\n <script language='JavaScript'> window.open('forward_task.php','mywindow','width=500,height=500,scrollbars=yes') </script> \n";
But my problem was how to get this to work. If I just used the isset and the name of the button, if the user refreshed the page the $_POST would still be set and the window would pop up again, even though they didn't click the button again. So, I was trying to think of a way to only call the function that sets the session variable and pops up the window ONLY IF the user just clicked the button. So I thought that maybe I could have an Javascript onclick event that would reload the page, with a parameter in the URL, say forward=true or something like that. Then, if that was set, call the function that sets the session variable and pops up the window. This is where I stopped, because I couldn't get even this to work - but my plans are to then refresh the page (either with Javascript or PHP) without the forward=true, so that the function wouldn't fire again if the user hit refresh.
Now, there's probably a wayyyyyy easier way to do this, so if you have any thoughts, let me know. Here's the simplified page, try.php, in PHP, HTML, and Javascript:
<?php
if (isset($_GET[forward]))
{
saveForForward();
}
function saveForForward()
{
/*Just an example, doing a little PHP then executing some Javascript*/
$name = $_POST[thename];
$name = "The Name Was: " . $name;
echo "\n <script language='JavaScript'> alert($name); </script> \n";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script language='JavaScript'>
function fwd_to_URL()
{
var theURL = "try.php?forward=true";
window.location.href = theURL;
return true;
}
</script>
<?php
var_dump($_POST);
?>
<form name=form1 method=post enctype="multipart/form-data">
<p> Name:
<input type="text" name="thename">
</p>
<input type="submit" value="submit" name="submit">
<input type="submit" value="check_name" name="check name" onClick="return fwd_to_URL();">
</form>
</body>
</html>
I know I've probably written enough already, but here's my thoughts on the order of events, if its possible:
onClick - submit the $_POST, reload the page with forward=true in the URL
if $_GET[forward]==true, or whatever, fire the save_for_forward() function (which would set the session, and pops up the window.
ideally, then I would reload the page without the forward=true in the URL
And by the way, this did work for me in two isolated incidents, lol, but I couldn't remember what combination of semi-colons, returns, and events that made it work. :/
Any ideas or help would be appreciated. If I need to totally rethink the process, that's fine too, I won't be offended. I've been feeling like I've just been putting bandaids on things and finding workarounds.