PDA

View Full Version : PHP & Javascript


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.

BWiz
06-30-2008, 08:35 PM
Use time() (http://php.net/time).



When the user clicks on the submit button, a session variable holding the time (of when the user clicked on the button) is created and stored. So, when it comes to the time() (http://php.net/time) to echo the window.open() JS function, your script pulls the current time() (http://php.net/time) and then matches it to the stored session time() (http://php.net/time) variable.

If these two values match (or if they are only one second away from one another, because theoretically there will be a short lapse of time between created the date on submit, and then checking it on echo), then the window.open() JS function is echoed.

Give it try.

tarad
06-30-2008, 09:25 PM
Use time() (http://php.net/time).

When the user clicks on the submit button, a session variable holding the time (of when the user clicked on the button) is created and stored. So, when it comes to the time() (http://php.net/time) to echo the window.open() JS function, your script pulls the current time() (http://php.net/time) and then matches it to the stored session time() (http://php.net/time) variable.

If these two values match (or if they are only one second away from one another, because theoretically there will be a short lapse of time between created the date on submit, and then checking it on echo), then the window.open() JS function is echoed.

Give it try.

Hey there! Thanks so much for your reply. Things were looking hopeless :)

Let me see if I'm following you... would I need to use JS to save the time that the use clicked the button, say in an onclick event? I know PHP has the time() function you were talking about, but if I call that in the submit then wouldn't it fire everytime the user refreshed the page? Which would defeat the whole purpose.

I like your idea, I'm just trying to figure out how to save the time that the user clicked the button from JavaScript so that I can retrieve it in PHP. I'm reading that you can save JS variables to cookies/sessions but I'm not sure how that works yet.

If I'm totally missing your point, let me know :) Correct me as needed.

Thanks,
Tara

tarad
07-01-2008, 03:23 PM
Thank you so much for the idea! I fleshed it out and made it work in my project. I created a hidden field, which was filled in with the current time via JavaScript in an onclick event for the form submission. Then, before launching the pop up window, I checked if the onclick time (divided by 1000 since it was in milliseconds, and PHP's time function is in seconds) was within around ~60 of now(), which seemed like a good interval. It works great! Thank you so much. I wouldn't have been able to do it without your idea.