PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Mon Nov 18, 2013 10:34 pm
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>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Mon Nov 18, 2013 10:46 pm
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
}
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Tue Nov 19, 2013 8:05 am
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>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Nov 19, 2013 2:36 pm
Does it work?
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Tue Nov 19, 2013 7:36 pm
no..it didnt work... or maybe is there any mistake?? can you help me take a look please...
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Nov 19, 2013 8:42 pm
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?
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Tue Nov 19, 2013 10:52 pm
do you mean i put the form 1st after that only the php code if statements??
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Nov 20, 2013 12:00 am
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 .
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Wed Nov 20, 2013 12:27 am
okay i know the problem already... thank for your help
tiger0
Forum Newbie
Posts: 16 Joined: Mon Oct 28, 2013 12:19 am
Post
by tiger0 » Tue Nov 26, 2013 12:59 am
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.