Form data passed to php if statement

Click For Summary
SUMMARY

The forum discussion centers on passing form data to a PHP script and the common mistake of using a single equals sign ("=") instead of a double equals sign ("==") for comparison in an if statement. The provided HTML form submits a value to "myscript.php", but the script always outputs "Number One" due to incorrect assignment in the conditional check. To resolve this, users should utilize "==" for comparisons and can refer to PHP's settype function for type conversion if necessary.

PREREQUISITES
  • Basic understanding of HTML forms
  • Familiarity with PHP syntax and conditional statements
  • Knowledge of GET method for form data submission
  • Understanding of variable assignment versus comparison operators in programming
NEXT STEPS
  • Learn about PHP comparison operators, specifically "==" versus "="
  • Research PHP's settype function for type conversion
  • Explore best practices for handling form data in PHP
  • Investigate debugging techniques for PHP scripts
USEFUL FOR

Web developers, particularly those working with PHP and HTML forms, as well as beginners looking to understand conditional logic in PHP.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
Howdy, tonight I was playing around with trying to pass a value from a form field and use it for an "if" statement in php.
the html:
Code:
<HTML>
<HEAD>
<TITLE>Form Test</TITLE>
</HEAD>
<BODY>
<H1>Form Test</H1>

<form name="input" action="myscript.php" method="get">
Enter a 1 or a 2:
<input type="text" name="page_number_from_form">

<input type="submit" value="Submit">

</form>
</BODY>
</HTML>
myscript.php:
Code:
#!/usr/local/bin/php
<?php 
$page_number="";

if(isset($_GET["page_number_from_form"])) $page_number = $_GET["page_number_from_form"];

if($page_number="1")
{
	
	echo 'Number One.';

}
else if($page_number="2")
{

	echo 'Number Two.';
	
}
else
	echo 'No page number.';
?>

No matter what I type in (or don't type in) it always echos "Number One". Is there a way to pass an integer from the form field instead of text? Maybe a way to convert it?
Thanks!
 
Technology news on Phys.org
I don't know PHP, but it's the middle of the night, so here are two shots in the dark.

Can you use "==" rather than "=" for your "if($page_number="1")" comparisons?

To change type, google says there are two ways: http://www.php.net/manual/en/function.settype.php" (links have examples).

I don't know if this will help at all, but I figured it couldn't hurt too much. :biggrin: Good lucks.

Oh, now I think about it for a second, the first option makes sense. If "=" gets interpreted as variable assignment inside of the "if" test statement, you are setting "$page_number" to "1", which explains the behavior (to me). Anywho... bedtime.
 
Last edited by a moderator:
ah, geez. double equals again. you'd think I would learn after making that mistake in C++ about 800 times!
I knew it was going to be something embarrassingly simple.
Gracias, amiga!
And thanks for the casting links, too.
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
4K
Replies
6
Views
4K