7 Replies - 2340 Views - Last Post: 22 February 2011 - 01:40 AM Rate Topic: -----

#1 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 05:05 PM

Hey guys, I want to create a simple form where the user enters their name and it is posted back at them.

I'm using boolean at the start to check if they've already posted their name (so if they have, I print the name, if they haven't, I tell them to), but I don't know how to toggle the boolean in the HTML submit event. I have no grasp of HTML and how it interacts with PHP, so any advice would be great.

Hopefully my code demonstrates what I'm trying to do.

My sincerest apologies if I've posted this in the wrong forum.

<?php
$submitted = False;
If($submitted == True)
	{echo "Your name is ".$_POST["name"];}
Else
	{echo "Please post your below.";}
?>

<html>
<body>

<form action="learning.php" method="post">
Name: <input type="text" name="name"/>
<input type="submit" $submitted = True/>
</form>

</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Managing PHP Boolean in HTML tags

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 05:17 PM

HTML doesnít really interact with PHP. it just sends it some variables determined by the submitted <form>. it does it via the form controlís value attribute. e.g. if you have <input name="foo" value="bar"> PHP receives it as $_POST['foo'] => "bar" (if itís a post request).

PHP doesnít interact with HTML either as it just assembles the mark-up and sends it to the browser.

as a result, HTML only passes strings to PHP (no booleans, no numbers, no objects, etc.) and PHP can only output strings to the browser. that PHP can receive form values as arrays is just a simple trick relying on PHPís array defining possibilities.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,766
  • Joined: 08-August 08

Re: Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 05:18 PM

PHP runs on the server and HTML is processed by the browser. MIxing them is a good way to run into trouble. As an example, this line:
<input type="submit" $submitted = True/>


tries to mix PHP and HTML, causing a problem in the browser.

See this.
Was This Post Helpful? 1
  • +
  • -

#4 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Re: Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 05:27 PM

So what you're saying, Dormilich, is that there's no way to control the boolean in the submit event? In that case, do you have a suggestion as to how I can manage the boolean elsewhere so as to achieve my desired outcome?

(And thanks for the explanation, both of you. That answers a lot of questions!!)
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,766
  • Joined: 08-August 08

Re: Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 05:32 PM

if(isset($_POST['name'])) {
  // display hello
} else {
  // display form
}

Was This Post Helpful? 1
  • +
  • -

#6 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Re: Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 05:44 PM

Awesome! That's got it working!

So just to confirm, this:

if(isset($_POST['name']))


Basically means "if we've been given a value for 'name' by POST"?

^ If that's the case, that solves the boolean problem.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,766
  • Joined: 08-August 08

Re: Managing PHP Boolean in HTML tags

Posted 21 February 2011 - 07:00 PM

$_POST is a super global array, but it's still an array. If $_POST['name'] has been set to a value then we can assume the form/browser/user has sent us something.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: Managing PHP Boolean in HTML tags

Posted 22 February 2011 - 01:40 AM

if you want to transform a form value into boolean, thatís also possible (though not what you wanted originally).
$boolean = filter_input(FILTER_POST, "foo", FILTER_VALIDATE_BOOLEAN);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1