Jump to content

[SOLVED] Been stuck forever- passing variables between PHP and JavaScript


jeffery1493

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

 

 

Link to comment
Share on other sites

>>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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']

?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?  :-|

 

 

Link to comment
Share on other sites

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...........

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.