Jump to content

Hand off JS var to PHP $var


AV1611

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

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