AV1611 Posted December 19, 2006 Share Posted December 19, 2006 How can I hand off a JS variable to a PHP $var? Quote Link to comment Share on other sites More sharing options...
fenway Posted December 19, 2006 Share Posted December 19, 2006 You can't... PHP runs before JS. Quote Link to comment Share on other sites More sharing options...
AV1611 Posted December 19, 2006 Author Share Posted December 19, 2006 Actually I found a way to do it. this works great...[code]<script language="JavaScript">var userFName;userFName = prompt("What is your first name?");var userLName;userLName = prompt("What is your last name?");var fullName = userFName + " " + userLName;</script><?php$MyVar2 = "<script language=javascript>document.write(fullName);</script>";echo $MyVar2;echo $MyVar2;?></html>[/code] Quote Link to comment Share on other sites More sharing options...
artacus Posted December 20, 2006 Share Posted December 20, 2006 Actually, you're not passing the JS variable to PHP. There's actually no reason to even use PHP in the example above. If this is meeting your needs, I'll leave it at that. If not, I'll discuss some strategies for getting info from JS to PHP. Quote Link to comment Share on other sites More sharing options...
fenway Posted December 20, 2006 Share Posted December 20, 2006 There are always callback ways of doing this, but AFAIK, this requires basically opening another page somehow. Quote Link to comment Share on other sites More sharing options...
dustinnoe Posted December 23, 2006 Share Posted December 23, 2006 Use ajax to send an http Request back to the server once your JS program is running on the client. Quote Link to comment Share on other sites More sharing options...
monkey_05_06 Posted December 23, 2006 Share Posted December 23, 2006 You can use JS to submit a form:[code]<?php if (!isset($_POST["fullname"])) { ?><form action = "<?php echo $_SERVER["PHP_SELF"]; ?>" name = "myform" method = "POST"> FORM <!-- Without some output JS won't recognize your form --> <input type = "hidden" name = "fullname" /></form><script language = "JavaScript"> document.myform.fullname.value = fullname; document.myform.submit();</script><?php } else { $fullname = $_POST["fullname"]; echo $fullname; echo $fullname; } ?>[/code] Quote Link to comment Share on other sites More sharing options...
AV1611 Posted December 25, 2006 Author Share Posted December 25, 2006 My problem is I only know PHP... I only dabble in JS but try not to use it because I was told many times here that a good web page shouldn't use it because many browsers have it turned off anyways. <feel free to comment - I'd like to know> the app I'm working is for a private intranet so I can use it there...I would like to learn JS, but I haven't been able to get any "traction" with it, as the manuals I read jump over the noob stuff too quick for me... I got PHP down because the doc's are WONDERFUL for PHP and now I can run with it Quote Link to comment Share on other sites More sharing options...
AV1611 Posted December 25, 2006 Author Share Posted December 25, 2006 [quote author=monkey_05_06 link=topic=119261.msg490564#msg490564 date=1166859300]You can use JS to submit a form:[code]<?php if (!isset($_POST["fullname"])) { ?><form action = "<?php echo $_SERVER["PHP_SELF"]; ?>" name = "myform" method = "POST"> FORM <!-- Without some output JS won't recognize your form --> <input type = "hidden" name = "fullname" /></form><script language = "JavaScript"> document.myform.fullname.value = fullname; document.myform.submit();</script><?php } else { $fullname = $_POST["fullname"]; echo $fullname; echo $fullname; } ?>[/code][/quote]I don't know what this is supposed to do, but all it does is display a page that says:FORM??? Quote Link to comment Share on other sites More sharing options...
dustinnoe Posted December 25, 2006 Share Posted December 25, 2006 PHP is much easier than JS anyway. Check out YUI at [url=http://developer.yahoo.com/yui]developer.yahoo.com/yui[/url] They make life with JS much easier. Once your understand how their library works there's no need to worry about cross browser issues, they have though it all through for you. Also check out [url=http://dustindiaz.com]dustindiaz.com[/url], he is a developer for Yahoo and lots of great information about YUI. Quote Link to comment Share on other sites More sharing options...
monkey_05_06 Posted December 26, 2006 Share Posted December 26, 2006 Apparently what I wrote about JS not recognizing the form is an issue with FF (1.5, I'm not sure about 2.0).Seeing as in that snippet I never set a JS var called fullname, that snippet isn't meant to be a standalone example. It should submit the form and then output...something...though I'm not sure what considering the JS var is never set. Possibly an error. Or the fact that the JS var is never set might make it crash. I don't know enough about JS to be sure.[code]<?php if (!isset($_POST["fullname"])) { ?><form action = "" name = "myform" method = "POST"> FORM <!-- Without some output JS won't recognize your form - FF 1.5 --> <input type = "hidden" name = "fullname" /></form><script language = "JavaScript"> var fullname = "John Doe"; document.myform.fullname.value = fullname; document.myform.submit();</script><?php } else { $fullname = $_POST["fullname"]; echo $fullname; echo $fullname; } ?>[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted December 27, 2006 Share Posted December 27, 2006 Same issue as on another thread.. FF requires a problem object chain to the form... e.g. document.forms.formname. 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.