Form data passed to php if statement

In summary, the conversation discusses a problem with passing and using a value from a form field in a PHP script. The suggested solution is to use "==" instead of "=" for comparisons in the if statement and to use casting methods to convert the type of the value.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
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:
  • #3
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.
 

1. What is "form data" in the context of PHP if statements?

"Form data" refers to information that is submitted by a user through an HTML form. This can include data such as text, numbers, checkboxes, and dropdown selections.

2. How is form data passed to a PHP if statement?

Form data is typically passed to a PHP if statement through the use of the $_POST or $_GET superglobals. These superglobals contain an associative array of the form data, which can then be accessed and used in the if statement.

3. Can form data be used in multiple if statements?

Yes, form data can be used in multiple if statements within a PHP script. The data is stored in the superglobals until the script is finished running, so it can be accessed and used in multiple places.

4. What happens if a required form field is left empty?

If a required form field is left empty, the data will not be passed to the PHP if statement. This can be checked using conditional statements, and appropriate actions can be taken based on the presence or absence of the data.

5. Are there any security concerns when using form data in PHP if statements?

Yes, it is important to properly sanitize and validate form data before using it in a PHP if statement. This helps prevent malicious attacks such as SQL injection or cross-site scripting. It is also important to only use form data from trusted sources.

Similar threads

  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
6K
Back
Top