mortalc Posted June 26, 2010 Share Posted June 26, 2010 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 More sharing options...
23.12.2012 Posted June 26, 2010 Share Posted June 26, 2010 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 More sharing options...
wirehopper Posted June 26, 2010 Share Posted June 26, 2010 You might want to use session variables to pass data between pages, it's more secure. http://www.php.net/manual/en/intro.session.phpHidden inputs can be modified because they're sent to the client. Link to comment Share on other sites More sharing options...
mortalc Posted June 26, 2010 Author Share Posted June 26, 2010 Ok, what about in a Javascript if() function? if (<?php $_POST["name"] ?>=="Bob"){...} Link to comment Share on other sites More sharing options...
jeffman Posted June 26, 2010 Share Posted June 26, 2010 You still need an echo statement or nothing will be printed. And you really should test the variable to see if it's set, as you've been shown. Doing so is best practice. Link to comment Share on other sites More sharing options...
mortalc Posted June 26, 2010 Author Share Posted June 26, 2010 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 More sharing options...
jeffman Posted June 26, 2010 Share Posted June 26, 2010 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 More sharing options...
mortalc Posted June 26, 2010 Author Share Posted June 26, 2010 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 More sharing options...
jeffman Posted June 26, 2010 Share Posted June 26, 2010 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 More sharing options...
wirehopper Posted June 26, 2010 Share Posted June 26, 2010 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 More sharing options...
mortalc Posted June 28, 2010 Author Share Posted June 28, 2010 How about (for checking if the variable is undefined): if ('<?php !isset($_POST["name"]) || !isset($_POST["answer"]) ?>'){...} Link to comment Share on other sites More sharing options...
boen_robot Posted June 28, 2010 Share Posted June 28, 2010 Khm.... echooo?!? Link to comment Share on other sites More sharing options...
Synook Posted June 28, 2010 Share Posted June 28, 2010 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 More sharing options...
jeffman Posted June 28, 2010 Share Posted June 28, 2010 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 More sharing options...
mortalc Posted June 28, 2010 Author Share Posted June 28, 2010 Ok I'll stop.does isnotset exist?I could use that... Link to comment Share on other sites More sharing options...
boen_robot Posted June 28, 2010 Share Posted June 28, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.