jeffery1493 Posted November 19, 2009 Share Posted November 19, 2009 Hi All, :-\ This may be an easy question, or it may not be. However despite the fact that JavaScript is in the title and used, this is very much a PHP issue. If someone has qualms about what forum it should go in, then transfer me over. I have been stuck on this problem for a very long time, if anybody has any idea how to help I'd greatly appreciate. What I am trying to do is get some information from a JavaScript input message prompt, and pass it on to PHP to use in a mail message. I have tried many ways of doing this, as you can see here the most recent try is dropping the information into a cookie on the client side: <?php $message = '<script type="text/javascript">var javatime = prompt("INVITE - Would you like to add a TIME to meet this person?", "Enter TIME to meet here:");window.alert("The invite has been sent!");document.cookie = "javatimecookie="+javatime</script>'; echo $message; $mail_message = "Please meet me at: " . $_COOKIE['javatimecookie'] . " ***\n\nHope to see you there soon!\n\n\n---------------------\nTHIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY TO THIS ADDRESS"; mail($email_address, "***** You Have Been Paged! *****", $mail_message); ?> As you may expect by looking, PHP runs through all its code FIRST, before JavaScript ever has a chance to drop the cookie. So in effect, if I send 3 messages, message 1 will have a BLANK time, the cookie from message 1 will be in email 2, and cookie 2 will be in email 3. Definitely not what I want! I have tried other ways of passing the variable with no success. How in the world can you grab the information from the input prompt (the variable I call, "javatime"), and put it into the PHP mail message before it is sent? I fought with this for hours on end with no success..........thanks for any advice. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 You can't get PHP variables like that. The cookie wouldn't be set until you refreshed the page (as far as PHP knows) You have to remember that PHP is run by the server, its results are computed, and then the raw HTML is sent to the browser. The javascript is sent and done after the PHP has run, so it can't send any variables back to PHP. You can send an ajax request to send something to a database, but you will need a page reset for PHP to get the information from javascript Quote Link to comment Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 What you must understand is that PHP is a server-side language that runs once, sends the information to the browser and that's it. JavaScript is a client-side language that runs after the browser has received the output from the server. One way to accomplish this would be to do one at a time. Using JavaScript you would create the prompt, assign the cookie then query a page through AJAX to actually send the email. Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 how to you pass the varaibles from javascript to php on a page reset? can u only do it with cookies? Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 Thanks. I have looked through reams of google articles on this, and Ajax is mentioned again and again as an 'alternative'. But it seems every Ajax answer imaginable is so lengthy and complex I can't make heads or tails of it. It seems to me a very typeheavy language and not very english-friendly for deciphering. I could be wrong. You say a page refresh- could I force a page refresh in between the setting of the cookie and the emailing? Would that accomplish what I am trying to do? it appears so frustratingly simple, but so far so difficult a thing to accomplish- just pass a variable from one line to the next. Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 >>What you must understand is that PHP is a server-side language that runs once, sends the information to the browser and that's it. JavaScript is a client-side language that runs after the browser has received the output from the server.>> OMG if I had $1 for every time I read that line last night on so many web pages, I could retire for life. It is a very popular answer. lol Quote Link to comment Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 AJAX really isn't all that complicated, but it can be intimidating at first, if you're actually interested in learning the basics of it I'd suggest this tutorial. Is there any particular reason that you're using a JavaScript prompt? This seems like a much more appropriate task for a simple form, that way you don't need to mess around with cookies (which seem a bit unnecessary when just storing information for such a short period of time). If you insist on continuing with this method I suppose you could force a refresh to the page after the prompt is submitted with: location.reload(true); Then on the page using PHP you'd check to see if the cookie has a value, if it does get that value and send the email, clear the cookie, then output the prompt as normal. I'd really suggest just creating a simple form though. Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 reply yeah ajax they say cant do anything with mysql databases... but does that mean there cant be variables passed between javascript to php at all? i understand that someone is all " you need to have a page load or refresh" but what do u do to get the variable to php? so u can just get the variable after the page has been reloaded from the cookie using this: $_COOKIE['javatimecookie'] ? Quote Link to comment Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 One method would be to use cookies, I suppose. Another method of communication could be to redirect to another page utilizing the query string. Like.. window.location = 'somefile.php?id=' + id; What you're going to do is really depends on the situation. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 Ajax can send requests to pages that will do a mysql queries. This is actually a very common use of AJAX. There are multiple ways to send php variables to javascript. for example, like OP did, simply writing Javascript to the page will work. <?php //I want javascript to have a variable $var = "Hello"; echo '<script type="text/javascript">var = "'.$var.'";</script>'; ?> Using this sort of thing, in conjuction with json functions can be very useful. However, sending variables from javascript to PHP is harder, since PHP is usually executed before the javascript has a chance. Thats why page refreshes are necessary for the changes to be known to the server. Yes $_COOKIE['whatever'] will work, but you need to actually send the cookie to the server first (through an HTTP request) Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 I absolutely don't need to use JavaScript. Frankly I just couldn't find a good example of how to do it the other way. I'm not exactly a PHP black belt. I'm more of a PHP self trainee. I actually don't like the JavaScript prompt because it forces you to use "SCRIPT PROMPT" and "Explorer User Prompt" in the headers and I can't find anywhere on Google that tells you how to change those, if they are at all changeable. I tried location.reload(true); in between the cookie and sending and got Fatal error: Call to undefined function reload() in ..../index.php on line 354 Is there something basic I'm missing? Does an include file need to be loaded? Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 >>Yes $_COOKIE['whatever'] will work, but you need to actually send the cookie to the server first (through an HTTP request)>> What do you mean by this? How would you do it? Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 how do u send the cookie then? Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 When I say that you need to send the cookie to the server first through an HTTP request, I mean you need to reload the page. When you load the page, you make an HTTP request to the server, and through this you send your cookies, ip address, and other information Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 Unfortunately I tried: $page = "index.php"; header("Refresh: 1; url=$page"); In between the cookie setting and the email. I sent several. When the emails arrived, each was still one cookie behind, just as before! ? Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 wait do you mean I should send the variables inside the header request? Quote Link to comment Share on other sites More sharing options...
ghostcoder Posted November 19, 2009 Share Posted November 19, 2009 Why can't you just post the input you get from javascript back to the server to process it via PHP? If you set it in a cookie, then you'll have to reload the php anyway to read the cookie data. AJAX would be the modern way to do it. I always thought it was much harder than it really is. Takes a little while to setup your personal system but it's fun once you get the hang of it. I've also seen an iframe technique to load another script without refreshing the current page. I think that's more of a hack approach and I would just try to use XMLHttpRequest if you MUST NOT refresh the page. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 No, you can't. I would take Alex's advice, and just use a simple form. It would accomplish what you want without any trouble, and would be much easier. Since you need a page refresh anyways, I don't see the point of using javascript Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 I can try doing all that, but what about the idea of setting the cookie with the page reload? Wouldn't that work? How would it be done in this case? Just reloading the page didn't work, at least the way I stated it. Surely that would just be adding one additional line with what I have now. Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 then why is it not workin for the op? Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 Okay I'm confused. I looked up PHP prompts and this website says this: >>>> Jerry Stuckle Guest Posts: n/a #2: May 21st, 2007 re: Confirm dialog box Tomislav wrote: Quote: Hello, > I need a method to create Dialog box in PHP to confirm an action like for instance, sending an e-mail, so if "OK" is clicked action is performed and if "Cancel" is clicked action is canceled. > I found some examples that are implementing "javascript:return confirm()" function in "onSubmit" event of a form but none of such methods is functioning (?). > Is there any way to do this in PHP ? > Thanks, > Tomislav Not really. You can't open new windows in PHP. Your best bet is javascript - it works, just ensure you have javascript enabled. Or you can take them to a new "confirm" window. If they confirm then continue. If they do not, return to the previous page. >>> Here this guy is saying you can't really do it in PHP and your best bet is JavaScript. Anyone have that smiley with the gun to its head? :-| Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 Well I finally tried this. I took out the JavaScript and instead set within the index.php program, beforehand: echo ' <form action="index.php" method="post"> Would you like a time to meet?: <input type="text" name="meet_time" /> <br /> <input type="submit" /> </form> '; $mail_message = "We will be waiting to meet you at " . $_POST['meet_time'] . " ***\n\nHope to see you there soon!\n\n---------------------\nTHIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY TO THIS ADDRESS"; mail($email_address, "You Have Been Paged! ****", $mail_message); This opens up a prompt at the top of the page, which asks you for the time, and waits for Submit. Then the message is mailed. However, when the message is received, the time is still blank. Its going to be another long night........... Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 u need to post the variable before u put it in the message. $time = mysql_real_escape_string($_POST[set_time]); or whatever the name is. it shouldnt be blank at all if u are just using the form and no javascript. what do u mean prompt? just a single form? Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 I'm trying to put a PHP prompt and wait for input at that point in the PHP script, asking for the variable. Quote Link to comment Share on other sites More sharing options...
jeffery1493 Posted November 19, 2009 Author Share Posted November 19, 2009 I think I know what is happening. PHP is plowing right ahead, even though it puts up a prompt for the variable, it already passed that line and compiled and sent the mail message. So, the mail was already delivered, before you ever entered the time. No way apparently to make PHP stop at that point in the .php, and ask you for the time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.