Passing values from drop down menu

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barbarowa@yahoo.com

    #1

    Passing values from drop down menu

    I've coded a script to populate a drop down menu from a database but I
    can't seem to get the PHP script to pass the selected item. The
    database only has two fields, ID and ITEM. I want the user to be able
    to see all the choices and pick one. This choice would then be used
    to update another database.

    The drop down menu populates correctly. When I test for the variable
    being passed, nothing is showing up. Here is the code:

    <?php

    include("connec t.php");

    if($_POST['testform']) {

    print_r($HTTP_P OST_VARS);
    exit;
    }

    ?>
    <form name="testform" action="<?php echo $PHP_SELF; ?>" method="post">

    <?php

    $query = "SELECT * FROM mytable ORDER BY item";
    $result = mysql_query($qu ery) or die ("$text[cannotexecutequ ery]:
    $query");

    echo "<select name='id'>";
    while($row=mysq l_fetch_assoc($ result))
    {
    echo "<option value=" . $row['id'].">".$row['item']."</option>";
    }
    echo "</select>";
    echo "<input type='submit' value='Submit' name='testform' >
    <input type='reset' value='Reset' name='reset'>
    </form>";

    ?>
  • Iván Sánchez Ortega

    #2
    Re: Passing values from drop down menu

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    barbarowa@yahoo .com wrote:
    [color=blue]
    > if($_POST['testform']) {
    >
    > print_r($HTTP_P OST_VARS);
    > exit;
    > }[/color]

    And why exactly are you mixing $_POST and $HTTP_POST_VARS ?

    - --
    - ----------------------------------
    Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net


    Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
    5.1.2-1 generating this signature.
    Uptime: 00:14:01 up 2 days, 3:42, 2 users, load average: 1.00, 0.78, 0.37

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFD5obe3jc Q2mg3Pc8RArdkAJ 44JfVv2285R5z8V UEHpZV8d3Kb1gCd GXwE
    ZCfWyMkEPR4+72U 6rVGEc8U=
    =CUXn
    -----END PGP SIGNATURE-----

    Comment

    • barbarowa@yahoo.com

      #3
      Re: Passing values from drop down menu

      On Mon, 06 Feb 2006 00:14:33 +0100, Iván Sánchez Ortega
      <i.punto.sanche z--@rroba--mirame.punto.ne t> wrote:
      [color=blue]
      >-----BEGIN PGP SIGNED MESSAGE-----
      >Hash: SHA1
      >
      >barbarowa@yaho o.com wrote:
      >[color=green]
      >> if($_POST['testform']) {
      >>
      >> print_r($HTTP_P OST_VARS);
      >> exit;
      >> }[/color]
      >
      >And why exactly are you mixing $_POST and $HTTP_POST_VARS ?[/color]

      Just hoping to see what variable values are being passed.

      Comment

      • MsKitty

        #4
        Re: Passing values from drop down menu

        I tried a version of your script on my server and it worked fine - I
        also tried changing the _POST to HTTP_POST_VARS (as Iván was
        suggesting) which also worked - here is the sample:

        <?php



        if($HTTP_POST_V ARS['testform']) {

        print_r($HTTP_P OST_VARS);
        exit;
        }

        ?>
        <form name="testform" action="<?php echo $PHP_SELF; ?>" method="post">

        <?php


        print "<select name='id'>
        <option value=\"1\" >1</option>
        <option value=\"2\" >2</option>";
        echo "</select>";
        echo "<input type='submit' value='Submit' name='testform' >
        <input type='reset' value='Reset' name='reset'>
        </form>";

        ?>

        Kitty
        OpenSkyWebDesig n.com

        Comment

        • Iván Sánchez Ortega

          #5
          Re: Passing values from drop down menu

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          barbarowa@yahoo .com wrote:
          [color=blue][color=green]
          >>And why exactly are you mixing $_POST and $HTTP_POST_VARS ?[/color]
          >
          > Just hoping to see what variable values are being passed.[/color]

          Then use something like:

          <?php

          if (isset($_POST))
          {
          echo '$_POST: '; print_r($_POST) ; exit;
          }
          else if (isset($HTTP_PO ST_VARS))
          {
          echo '$HTTP_POST_VAR S: '; print_r($HTTP_P OST_VARS); exit;
          }

          ?>

          Think about:
          - - What if $_POST gets a value but $HTTP_POST_VARS does not? (and the other
          way round)
          - - What if $_POST['testform'] gets a value of 0, oran empty string, that will
          evaluate as false?


          - --
          - ----------------------------------
          Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

          La política es un acto de equilibrio entre la gente que quiere entrar y
          aquellos que no quieren salir.
          -- Jacques Benigne Bossuet. (1627-1704) Filósofo francés.

          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.2 (GNU/Linux)

          iD8DBQFD5xJY3jc Q2mg3Pc8RAhyUAJ sHjEPEvdb1bgo4e tjZRJIgHv8xVgCg hsJy
          bhKhGQRRTJ659hQ WjH2+5fk=
          =PHG+
          -----END PGP SIGNATURE-----

          Comment

          • barbarowa@yahoo.com

            #6
            Re: Passing values from drop down menu

            I think I need to clarify what my objective is. Despite the code that
            I posted which my attempt to test what is actually getting passed to
            the program from the drop down menu selection, what I REALLY want is
            to know HOW to pass the variable the correct way and then how to use
            it in a MySQL INSERT statement. I tried the code you provided and
            this is what is shown: $_POST: Array ( )

            I know an array is what I'm up against, what I don't know is how to
            deal with it. Does this help clear up what I am looking for?



            On Mon, 06 Feb 2006 10:09:43 +0100, Iván Sánchez Ortega
            <i.punto.sanche z--@rroba--mirame.punto.ne t> wrote:
            [color=blue]
            >-----BEGIN PGP SIGNED MESSAGE-----
            >Hash: SHA1
            >
            >barbarowa@yaho o.com wrote:
            >[color=green][color=darkred]
            >>>And why exactly are you mixing $_POST and $HTTP_POST_VARS ?[/color]
            >>
            >> Just hoping to see what variable values are being passed.[/color]
            >
            >Then use something like:
            >
            ><?php
            >
            >if (isset($_POST))
            >{
            > echo '$_POST: '; print_r($_POST) ; exit;
            >}
            >else if (isset($HTTP_PO ST_VARS))
            >{
            > echo '$HTTP_POST_VAR S: '; print_r($HTTP_P OST_VARS); exit;
            >}
            >
            >?>
            >
            >Think about:
            >- - What if $_POST gets a value but $HTTP_POST_VARS does not? (and the other
            >way round)
            >- - What if $_POST['testform'] gets a value of 0, oran empty string, that will
            >evaluate as false?[/color]

            Comment

            • Jerry Stuckle

              #7
              Re: Passing values from drop down menu

              barbarowa@yahoo .com wrote:[color=blue]
              > I think I need to clarify what my objective is. Despite the code that
              > I posted which my attempt to test what is actually getting passed to
              > the program from the drop down menu selection, what I REALLY want is
              > to know HOW to pass the variable the correct way and then how to use
              > it in a MySQL INSERT statement. I tried the code you provided and
              > this is what is shown: $_POST: Array ( )
              >
              > I know an array is what I'm up against, what I don't know is how to
              > deal with it. Does this help clear up what I am looking for?
              >
              >[/color]

              Looks like you did:

              echo '$_POST: '; print($_POST); exit;

              Instead of

              echo '$_POST: '; print_r($_POST) ; exit;
              ^^

              For an array, print() will print "Array()";

              print_r() will print the contents of an array.

              You should find the value from the previous form in $_POST['id'].

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Jim Michaels

                #8
                Re: Passing values from drop down menu


                <barbarowa@yaho o.com> wrote in message
                news:imoeu110k4 s4mopdc8kk0jrm4 g5tlacr34@4ax.c om...[color=blue]
                >I think I need to clarify what my objective is. Despite the code that
                > I posted which my attempt to test what is actually getting passed to
                > the program from the drop down menu selection, what I REALLY want is
                > to know HOW to pass the variable the correct way and then how to use
                > it in a MySQL INSERT statement. I tried the code you provided and
                > this is what is shown: $_POST: Array ( )[/color]

                once you've validated $_POST['id'],
                mysql_query("IN SERT INTO sometable(somec olumn,anotherco lumn)
                VALUES ('somestring',' " .
                str_replace(";" ,"",mysqlreal_e scape_string($_ POST['id'])) . "')");

                the functions should prevent SQL INJECTION. you can eliminate somecolumn
                and somestring - i just put it in for completentess of example. your string
                data should be enclosed in quotes. if it is numeric data, then don't
                surround it with quotes.
                [color=blue]
                >
                > I know an array is what I'm up against, what I don't know is how to
                > deal with it. Does this help clear up what I am looking for?
                >
                >
                >
                > On Mon, 06 Feb 2006 10:09:43 +0100, Iván Sánchez Ortega
                > <i.punto.sanche z--@rroba--mirame.punto.ne t> wrote:
                >[color=green]
                >>-----BEGIN PGP SIGNED MESSAGE-----
                >>Hash: SHA1
                >>
                >>barbarowa@yah oo.com wrote:
                >>[color=darkred]
                >>>>And why exactly are you mixing $_POST and $HTTP_POST_VARS ?
                >>>
                >>> Just hoping to see what variable values are being passed.[/color]
                >>
                >>Then use something like:
                >>
                >><?php
                >>
                >>if (isset($_POST))
                >>{
                >> echo '$_POST: '; print_r($_POST) ; exit;
                >>}
                >>else if (isset($HTTP_PO ST_VARS))
                >>{
                >> echo '$HTTP_POST_VAR S: '; print_r($HTTP_P OST_VARS); exit;
                >>}
                >>
                >>?>
                >>
                >>Think about:
                >>- - What if $_POST gets a value but $HTTP_POST_VARS does not? (and the
                >>other
                >>way round)
                >>- - What if $_POST['testform'] gets a value of 0, oran empty string, that
                >>will
                >>evaluate as false?[/color]
                >[/color]


                Comment

                • Jim Michaels

                  #9
                  Re: Passing values from drop down menu


                  <barbarowa@yaho o.com> wrote in message
                  news:imoeu110k4 s4mopdc8kk0jrm4 g5tlacr34@4ax.c om...[color=blue]
                  >I think I need to clarify what my objective is. Despite the code that
                  > I posted which my attempt to test what is actually getting passed to
                  > the program from the drop down menu selection, what I REALLY want is
                  > to know HOW to pass the variable the correct way and then how to use
                  > it in a MySQL INSERT statement. I tried the code you provided and
                  > this is what is shown: $_POST: Array ( )
                  >
                  > I know an array is what I'm up against, what I don't know is how to
                  > deal with it. Does this help clear up what I am looking for?
                  >
                  >
                  >
                  > On Mon, 06 Feb 2006 10:09:43 +0100, Iván Sánchez Ortega
                  > <i.punto.sanche z--@rroba--mirame.punto.ne t> wrote:
                  >[color=green]
                  >>-----BEGIN PGP SIGNED MESSAGE-----
                  >>Hash: SHA1
                  >>
                  >>barbarowa@yah oo.com wrote:
                  >>[color=darkred]
                  >>>>And why exactly are you mixing $_POST and $HTTP_POST_VARS ?
                  >>>
                  >>> Just hoping to see what variable values are being passed.[/color][/color][/color]

                  maybe I can save you some time. If you used name="id", $_POST['id'] will
                  contain your selected value or nothing at all. you can try using isempty()
                  on it and see if that works.

                  if your select element were a multiple-select, then you should specify
                  select's name as id[] which signals to PHP to return an array of values.
                  then you would
                  foreach ($_POST['id'] as $val) {
                  do something with $val
                  }

                  [color=blue][color=green]
                  >>
                  >>Then use something like:
                  >>
                  >><?php
                  >>
                  >>if (isset($_POST))
                  >>{
                  >> echo '$_POST: '; print_r($_POST) ; exit;
                  >>}
                  >>else if (isset($HTTP_PO ST_VARS))
                  >>{
                  >> echo '$HTTP_POST_VAR S: '; print_r($HTTP_P OST_VARS); exit;
                  >>}
                  >>
                  >>?>
                  >>
                  >>Think about:
                  >>- - What if $_POST gets a value but $HTTP_POST_VARS does not? (and the
                  >>other
                  >>way round)
                  >>- - What if $_POST['testform'] gets a value of 0, oran empty string, that
                  >>will
                  >>evaluate as false?[/color]
                  >[/color]


                  Comment

                  Working...