php does not display variables

$_POST variables not displaying in PHP

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3392 Views - Last Post: 01 May 2010 - 12:54 PM Rate Topic: -----

#1 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

php does not display variables

Posted 29 April 2010 - 12:30 PM

Hey guys

Noobie John here. I now can't get my PHP script to display $_POST variables. I've researched this and as far as I can tell my code is correct. Here is the code:

HTML
[color="#0000FF"]
<html>
	<body>
		<h1> Calculation Form </h1>
		<form action="calculation.php" method="POST">
			<p>
				<b> Enter 1st number: </b>
				<input type="text" name="a">
				<b>Enter 2nd number: </b>
				<input type="text" name="b">
			</p>
			<p>
				<input type="submit" name="calculate" value="Add">
				<input type="submit" name="calculate" value="Subtract">
				<input type="submit" name="calculate" value="Multiply">
				<input type="submit" name="calculate" value="Divide">
			</p>
		</form>
	</body>
</html>	[/color]



PHP script
[color="#0000FF"]<?php
switch ($_POST[calculate])
		{
		case "Add":
			echo $_POST[a]." + ".$_POST[b]." = ".($_POST[a]+$_POST[b]);
			break;
		case 'Subtract':
			echo $_POST[a]." - ".$_POST[b]." = ".($_POST[a]-$_POST[b]);
			break;
		case 'Multiply':
			$t=$_POST[a]*$_POST[b];
			echo $_POST[a]." x ".$_POST[b]." = ".($_POST[a]*$_POST[b]);
			break;
		case 'Divide':
			$t=$_POST[a]/$_POST[b];
			echo $_POST[a]." divided by ".$_POST[b]." = ".($_POST[a]/$_POST[b]);
			break;
		}
?></color>



Here is a sample ouput when I click the "Add" button"

+ = 0

The buttons and the switch in my PHP work. If I click Multiply I get:

x = 0

Any ideas as to why this is happening.

This post has been edited by CTphpnwb: 29 April 2010 - 12:38 PM
Reason for edit:: Code tags use [ ], not <>, even though the icon is <> !


Is This A Good Question/Topic? 0
  • +

Replies To: php does not display variables

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,004
  • Joined: 08-August 08

Re: php does not display variables

Posted 29 April 2010 - 12:39 PM

Try this:
$_POST['calculate']
in place of this:
$_POST[calculate]
Was This Post Helpful? 0
  • +
  • -

#3 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: php does not display variables

Posted 30 April 2010 - 10:13 AM

Thanks, I thought I had tried that but I guess i needed to refresh my browser. Now my issue is with a line I added to it, but I'm thinking the way I did requires my PHP and HTML all in one file. If so how do I get it to work. It is the type="hidden" I'm getting blanks. Or "Number of times run : "
[b]HTML[/b]
<html>
	<body>
		<h1> Calculation Form </h1>
		<form action="calculation.php" method="POST">
			<p>
				Number of times run :
				<input type="hidden" name="counter" value="<?php echo $count; ?>">
			</p>
			<p>
				<b> Enter 1st number: </b>
				<input type="text" name="a">
				<b>Enter 2nd number: </b>
				<input type="text" name="b">
			</p>
			<p>
				<input type="submit" name="calculate" value="Add">
				<input type="submit" name="calculate" value="Subtract">
				<input type="submit" name="calculate" value="Multiply">
				<input type="submit" name="calculate" value="Divide">
			</p>
		</form>
	</body>
</html>	



PHP
<?php
$count = (isset($_POST['counter'])) ? $_POST['counter'] +1 : 1;
switch ($_POST['calculate'])
		{
		case "Add":
			echo $_POST['a']." + ".$_POST['b']." = ".($_POST['a']+$_POST['b']);
			break;
		case 'Subtract':
			echo $_POST['a']." - ".$_POST['b']." = ".($_POST['a']-$_POST['b']);
			break;
		case 'Multiply':
			$t=$_POST['a']*$_POST['b'];
			echo $_POST['a']." x ".$_POST['b']." = ".($_POST['a']*$_POST['b']);
			break;
		case 'Divide':
			$t=$_POST['a']/$_POST['b'];
			echo $_POST['a']." divided by ".$_POST['b']." = ".($_POST['a']/$_POST['b']);
			break;
		}
?>


This post has been edited by JackOfAllTrades: 30 April 2010 - 07:41 PM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,004
  • Joined: 08-August 08

Re: php does not display variables

Posted 30 April 2010 - 10:25 AM

:(
Now try this:
http://www.dreaminco...-code-tags-are/
Was This Post Helpful? 0
  • +
  • -

#5 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: php does not display variables

Posted 30 April 2010 - 12:23 PM

For some reason when I post my code the code tags for the post are showing please ignore the code tags. Let me be more specific.

HTML

<p>
Number of times run :
<input type="hidden" name="counter" value="<?php echo $count; ?>">
</p>

PHP
$count = (isset($_POST['counter'])) ? $_POST['counter'] +1 : 1;


I am not getting a result for number of times run.

This post has been edited by CTphpnwb: 30 April 2010 - 05:41 PM
Reason for edit:: Code tags use [ ] , not < >

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: php does not display variables

Posted 30 April 2010 - 12:42 PM

You do realize you're outputting the count into the html as a hidden form value, not as something visible on the page, right?
Was This Post Helpful? 0
  • +
  • -

#7 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: php does not display variables

Posted 30 April 2010 - 05:38 PM

Oops got that. I swear this book of mine that is teaching me PHP has got some major flaws. Does anyone know of a good book that teaches PHP and MySQL.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,004
  • Joined: 08-August 08

Re: php does not display variables

Posted 30 April 2010 - 05:43 PM

View Postjptenney, on 30 April 2010 - 02:23 PM, said:

For some reason when I post my code the code tags for the post are showing please ignore the code tags.

That's because you're using <code> and not [code]
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: php does not display variables

Posted 30 April 2010 - 05:46 PM

That's not a PHP issue, it's an issue with someone (you in the case) not knowing enough HTML to know that

<input type="hidden">


is a hidden element and wont be displayed out to the page :)
Was This Post Helpful? 0
  • +
  • -

#10 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: php does not display variables

Posted 30 April 2010 - 07:25 PM

View PostPsychoCoder, on 30 April 2010 - 04:46 PM, said:

That's not a PHP issue, it's an issue with someone (you in the case) not knowing enough HTML to know that

<input type="hidden">


is a hidden element and wont be displayed out to the page :)



Is a PHP issue not with someone who doesn't know enough about HTML. I'm learning PHP not HTML. It should display out of the forms hidden input.
<p>No. of times run : <?php echo $counter ?> </p>


which was brought to my attention earlier as something I overlooked. However, I still am not getting a result. My book on PHP shows that this should work but it doesn't. My book also failed to tell to put ' around an argument inside the [] of the $_POST so I'm frustrated because of a book not well proofed prior to publishing.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: php does not display variables

Posted 30 April 2010 - 07:28 PM

Can you post your full code again, just so we know we're all on the same page (to coin a phrase)?

:code:
Was This Post Helpful? 0
  • +
  • -

#12 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: php does not display variables

Posted 30 April 2010 - 07:41 PM

PHP and HTML go hand-in-hand. If you don't know HTML I strongly suggest you learn it first.

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#13 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: php does not display variables

Posted 30 April 2010 - 09:23 PM

Hi there,
maybe this is silly, but,
where did you add your php, is it on the top of the html or on the bottom?
because if php is executed after assigning the value to the counter input element, the $counter will not be initiated yet you will have a value of "" in the (counter) element, and the next post will have nothing to add to.
Was This Post Helpful? 1
  • +
  • -

#14 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: php does not display variables

Posted 30 April 2010 - 09:59 PM

View PostShaneK, on 30 April 2010 - 07:41 PM, said:

PHP and HTML go hand-in-hand. If you don't know HTML I strongly suggest you learn it first.

Yours,
Shane~



I know HTML and CSS.

View Postahmad_511, on 30 April 2010 - 09:23 PM, said:

Hi there,
maybe this is silly, but,
where did you add your php, is it on the top of the html or on the bottom?
because if php is executed after assigning the value to the counter input element, the $counter will not be initiated yet you will have a value of "" in the (counter) element, and the next post will have nothing to add to.



Good question, because that raises another issue I discover while playing around with my script. The PHP is a separate script called the from the action in the HTML form. However, I do see what you are saying. I may need to echo the $counter after the form input.
Was This Post Helpful? 0
  • +
  • -

#15 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: php does not display variables

Posted 30 April 2010 - 10:10 PM

Here is my script again as requested

HTML

<html>
	<body>
		<h1> Calculation Form </h1>
		<p>
				Number of times run : <?php echo $counter; ?>
		</p>
		<form action="calculation.php" method="POST">
				<p>
				<input type="hidden" name="counter" value="<?php echo $counter; ?>">
				<b> Enter 1st number: </b>
				<input type="text" name="a">
				<b>Enter 2nd number: </b>
				<input type="text" name="b">
			</p>
			<p>
				<input type="submit" name="calculate" value="Add">
				<input type="submit" name="calculate" value="Subtract">
				<input type="submit" name="calculate" value="Multiply">
				<input type="submit" name="calculate" value="Divide">
			</p>
		</form>
	</body>
</html>	




PHP script

<?php
$counter=(isset($_POST['counter'])) ? $_POST['counter'] +1 : 1;
switch ($_POST['calculate'])
		{
		case "Add":
			echo $_POST['a']." + ".$_POST['b']." = ".($_POST['a']+$_POST['b']);
			break;
		case 'Subtract':
			echo $_POST['a']." - ".$_POST['b']." = ".($_POST['a']-$_POST['b']);
			break;
		case 'Multiply':
			$t=$_POST['a']*$_POST['b'];
			echo $_POST['a']." x ".$_POST['b']." = ".($_POST['a']*$_POST['b']);
			break;
		case 'Divide':
			$t=$_POST['a']/$_POST['b'];
			echo $_POST['a']." divided by ".$_POST['b']." = ".($_POST['a']/$_POST['b']);
			break;
		}
?>


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2