PHP Form data passed to php if statement

AI Thread Summary
The discussion centers on troubleshooting a PHP script that incorrectly outputs "Number One" regardless of user input from a form. The issue arises from using a single equals sign "=" for comparison in the if statements, which assigns the value instead of checking equality. The correct approach is to use double equals "==" for comparisons. Additionally, suggestions are made to convert the input type if needed, with references to PHP's settype function for type conversion. The conversation highlights a common mistake in programming and emphasizes the importance of using the correct comparison operator.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top