Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

PHP Form data passed to php if statement

  1. May 24, 2007 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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 (Text):

    <TITLE>Form Test</TITLE>
    <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">

    Code (Text):


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

        echo 'Number One.';

    else if($page_number="2")

        echo 'Number Two.';
        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?
  2. jcsd
  3. May 24, 2007 #2


    User Avatar
    Gold Member

    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" [Broken] (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: May 2, 2017
  4. May 24, 2007 #3

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook