Page 1 of 1
Beginner's problem
Posted: Mon Nov 18, 2013 10:34 pm
by cheng412
basically, this is my new form i tried to make... but here is a problem, i don't know how to hide form by using bool statement... ermmm if you feel free please give me some comments on my code... i'm definitely a new beginner with this...
Code: Select all
<html>
<body>
<?php
$booFirstname=0;
$firstname="";
if(isset($_POST["submit"])){
if($_POST["firstname"]== NULL) {
$booFirstname=1;}
else {
$firstname=$_POST["firstname"];}
}
?>
<form method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>
<p>First Name:<input type="text" name="firstname">
<?php if($booFirstname) echo "Please select a title" ?>
</p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
Re: Beginner's problem
Posted: Mon Nov 18, 2013 10:46 pm
by requinix
Think not so much "hide the form" but more of "don't output it". Like
Code: Select all
<?php
if (/* show the form */) { // or "if (!hide the form)"
?>
the form
<?php
}
Re: Beginner's problem
Posted: Tue Nov 19, 2013 8:05 am
by cheng412
so is the code like this?
Code: Select all
<html>
<body>
<?php
$booDisplay=0;
$booFirstname=0;
$firstname="";
if(isset($_POST["submit"])){
$booDisplay=1;
if($_POST["firstname"]== NULL) {
$booFirstname=1;}
else {
$firstname=$_POST["firstname"];}
}
if ($booDisplay) {
?>
<form method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>
<p>First Name:<input type="text" name="firstname">
<?php if($booFirstname) echo "Please select a title" ?>
</p>
<p><input type="submit" name="submit"></p>
</form>
<?php
}
?>
</body>
</html>
Re: Beginner's problem
Posted: Tue Nov 19, 2013 2:36 pm
by requinix
Does it work?
Re: Beginner's problem
Posted: Tue Nov 19, 2013 7:36 pm
by cheng412
no..it didnt work... or maybe is there any mistake?? can you help me take a look please...

Re: Beginner's problem
Posted: Tue Nov 19, 2013 8:42 pm
by requinix
Right now you have it set to not show the form by default, and will only show the form once the form has been submitted. Backwards?
Re: Beginner's problem
Posted: Tue Nov 19, 2013 10:52 pm
by cheng412
do you mean i put the form 1st after that only the php code if statements??
Re: Beginner's problem
Posted: Wed Nov 20, 2013 12:00 am
by requinix
The default is to not show the form.
Code: Select all
if(isset($_POST["submit"])){
$booDisplay=1;
If they clicked the button to submit the form, then you decide to show the form.
That's backwards. It should be
The default is to show the form.
Code: Select all
if(isset($_POST["submit"])){
$booDisplay=0;
If they clicked the button to submit the form, then you decide to not show the form.
Once that works, then you can modify your logic to show the form if there was a problem with the name: with the above changes, if the firstname is empty then $booFirstname=1 but you've still decided not to show the form. Really what you should be doing is having a default of showing the form and only hiding it if they've clicked the button
and all the input is valid.
Re: Beginner's problem
Posted: Wed Nov 20, 2013 12:27 am
by cheng412
okay i know the problem already... thank for your help

Re: Beginner's problem
Posted: Tue Nov 26, 2013 12:59 am
by tiger0
Great info, requinix.
I really appreciate the way you provide support in this forum. Your support's been always very clear and easy to understand from novice to expert.