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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top