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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

[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

???
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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