Beginner's problem

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

Post Reply
cheng412
Forum Newbie
Posts: 11
Joined: Sun Nov 17, 2013 11:18 pm

Beginner's problem

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner's problem

Post 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

}
cheng412
Forum Newbie
Posts: 11
Joined: Sun Nov 17, 2013 11:18 pm

Re: Beginner's problem

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner's problem

Post by requinix »

Does it work?
cheng412
Forum Newbie
Posts: 11
Joined: Sun Nov 17, 2013 11:18 pm

Re: Beginner's problem

Post by cheng412 »

no..it didnt work... or maybe is there any mistake?? can you help me take a look please... :) :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner's problem

Post 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?
cheng412
Forum Newbie
Posts: 11
Joined: Sun Nov 17, 2013 11:18 pm

Re: Beginner's problem

Post by cheng412 »

do you mean i put the form 1st after that only the php code if statements??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner's problem

Post by requinix »

Code: Select all

$booDisplay=0;
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

Code: Select all

$booDisplay=1;
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

Re: Beginner's problem

Post by cheng412 »

okay i know the problem already... thank for your help :)
tiger0
Forum Newbie
Posts: 16
Joined: Mon Oct 28, 2013 12:19 am

Re: Beginner's problem

Post 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.
Post Reply