kokeroulis Posted November 28, 2008 Share Posted November 28, 2008 Helloi want to put a form in which if a condition is true then the form will be display else it will not.The point is that if i put the form code into the <?phpif (condition) echo "form code";?> then it came out an error.How can i fix it? Link to comment Share on other sites More sharing options...
jeffman Posted November 29, 2008 Share Posted November 29, 2008 Show us the code you really want to use. We'll find a better solution that way. Link to comment Share on other sites More sharing options...
zppblood Posted December 1, 2008 Share Posted December 1, 2008 You probably didn't escape your quotes that you used to start a string.Error:echo 'O' Reilly';PHP will think I have ended the string after the letter O, and think Reilly constant (isn't defined), and I didn't concatenate (join the scalar (single value like strings and numbers) values together).So you will have to escape them by placing a \ before the character that needs to be escaped.echo 'O\' Reilly'; Link to comment Share on other sites More sharing options...
alexnofue Posted December 1, 2008 Share Posted December 1, 2008 Hi, i don't know how its this formally called, but you can code with php and html in the same file and still not being 'mixed', like this:<?php...//code...if(condition){?><form acti.....> <input .... value="<?php echo $myvar; ?>" /></form><?php}?>hope this helps Link to comment Share on other sites More sharing options...
kokeroulis Posted December 2, 2008 Author Share Posted December 2, 2008 Show us the code you really want to use. We'll find a better solution that way.here is the code<?php$m = 5;if ($m == 5) echo "<form method="post" action="test.php"><input name="name" value="" type="text"><br> <input type="submit"><br> </form>";?> Link to comment Share on other sites More sharing options...
jeffman Posted December 2, 2008 Share Posted December 2, 2008 Double quotes inside double quotes must be escaped. You could also change them to single quotes. Or you could "trap" your HTML inside a PHP condition that spans two PHP tags. This looks a little weird if you've never seen it, but it really works: <?php $m = 5; if ($m == 5) { // OPENING THE BRACES HERE CAUSES THE HTML BELOW TO BE PRINTED ONLY IF THE CONDITION IS TRUE?><form method="post" action="test.php"> <input name="name" value="" type="text"><br> <input type="submit"><br></form><?php } // AND WE CLOSE THE BRACE HERE?> This is especially useful (and easy to read) if the trapped HTML doesn't contain any PHP variables that need to be interpolated. Link to comment Share on other sites More sharing options...
kokeroulis Posted December 2, 2008 Author Share Posted December 2, 2008 Double quotes inside double quotes must be escaped. You could also change them to single quotes. Or you could "trap" your HTML inside a PHP condition that spans two PHP tags. This looks a little weird if you've never seen it, but it really works:<?php $m = 5; if ($m == 5) { // OPENING THE BRACES HERE CAUSES THE HTML BELOW TO BE PRINTED ONLY IF THE CONDITION IS TRUE?><form method="post" action="test.php"> <input name="name" value="" type="text"><br> <input type="submit"><br></form><?php } // AND WE CLOSE THE BRACE HERE?> This is especially useful (and easy to read) if the trapped HTML doesn't contain any PHP variables that need to be interpolated. thanks.And something else if i want to use the var $_POST["name"] from the above form into the second PHP tag will it work? Link to comment Share on other sites More sharing options...
jeffman Posted December 2, 2008 Share Posted December 2, 2008 Yes. All variables you declare outside of functions are global throughout the document, as are superglobals like $_POST. That is, their scope is not limited to one set of <?php ?> tags. Different tags simply demarcate portions of the script from the true HTML portions. Think of it this way: there is only one script per document, though it may come in more than one section. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.