Jump to content

POST variables in a form


mortalc

Recommended Posts

Is it possible to set the value of a hidden input to a post variable passed to the webpage? Like this:

//Old webage<form method="post" action="newwebsite.php">Name: <input type="text" name="name" /><input type="submit" value="submit /></form>//New webpage<form><input type="hidden" value=<?php $_POST["name"] ?> /></form>

Link to comment
Share on other sites

Try

<input type="hidden" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" />

Your code was missing the echo. The IF is there only because it would produce an undefined variable warning if for some reason $_POST['name'] was not defined.

Link to comment
Share on other sites

OK. But what if the javascript is testing whether the $_POST["name"] is undefined?So:

if (<?php echo $_POST["name"]; ?>==undefined){window.alert("You have not entered your name!")}

Link to comment
Share on other sites

Doesn't change anything. Attempting to access a variable in PHP that has not been set will generate a warning. If you have error-reporting turned off (which is the default setting for many hosts), you will not realize that a warning has been generated, but it has.You can still run your test in JavaScript. These are independent operations taking place in different contexts.For the JavaScript statement to work, you'll need quotation marks to surround the printed value. It will be easier to visualize if you split this into separate statements:

var myName = "<?php if(isset($_POST['name'])) echo $_POST['name']; ?>"; // notice the quotation marksif (myName == ""){   window.alert("You have not entered your name!")}

Link to comment
Share on other sites

Ok.Final Question (hopefully).If I'm writing to a HTML document with fwrite():will this work for setting the ID of a div tag:

 fwrite ($file, "<div id='$_POST["name"]'>");

Link to comment
Share on other sites

Close, but you may have problems with the nested quotation marks. Try this:fwrite ($file, "<div id=\"$_POST['name']\">");And consider using file_put_contents instead of fopen, fwrite, fclose. It's just simpler. Available only in PHP 5+, though.You may also want to assign your file data to a variable instead of passing a string literal to your write function. Your code will be easier to maintain.

Link to comment
Share on other sites

Don't forget that PHP executes and finishes before javascript. Also, if you're going to use javascript to test the values, you should test them before sending them to the server. You'll have to test them at the server, too, for security.

Link to comment
Share on other sites

How about (for checking if the variable is undefined):

if ('<?php !isset($_POST["name"]) || !isset($_POST["answer"]) ?>'){...}

Link to comment
Share on other sites

I suggest you just stop trying to combine PHP and JavaScript like that - it just makes things really confusing.

Link to comment
Share on other sites

Have you tried any of this stuff? The results might surprise you. Even if you add echo statements as boen suggests, the stuff that gets printed may look weird. Consider:

if ('<?php echo (!isset($_POST["name"]) || !isset($_POST["answer"]) ) ?>')

When this gets printed, it will probably look like this if the conditional returns true:

if ('1')

or this if it returns false:

if ('')

Neither of those is likely to be useful.

Link to comment
Share on other sites

Ok I'll stop.does isnotset exist?I could use that...
No, but you've already seen how you could create it:
!isset($variable)

Or do you mean in JavaScript context? Just specify the name of the JavaScript variable, i.e.

if (variable)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...