Results 1 to 7 of 7

Thread: echo selectbox value and matching array item

  1. #1
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Default echo selectbox value and matching array item

    Ok, so i'm technically still a virgin when it comes to PHP. I am trying to learn on my own, but I keep getting stuck on this script I'm trying to write.

    Here's what I'm trying to do:

    Page 1:
    has a dropdown box where user can select a value
    also has an array which is used to fill the select box

    Page 2:
    has another array of items which correspond to the Page 1 array
    has processing script to handle the submitted info and match the corresponding items then echo outcome


    For some reason, everytime I submit the value from the dropdown box it simply echos ALL items in the array rather than just the one that corresponds.

    Here's an example (Page 1):
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
     <head>
      <title> Blah </title>
    </head>
    <body>
    <h2>Number Matcher</h2>
    <form method="post" action="script.php">
    <label>Please select a number:</label>
    <select name="number">
    <?php
    $numbers 
    = array(0=> "One""Two""Three""Four""Five""Six""Seven""Eight""Nine""Ten");
    for (
    $counter=0$counter sizeof($numbers); $counter++)
        {
            echo 
    "<option>$numbers[$counter]</option>";
        }
            echo 
    "</select>";
                for (
    $counter=0$counter sizeof($numbers); $counter++)
        {
            echo 
    "<input type=hidden name=hidden[] value=$numbers[$counter]>";
        }
            echo 
    "<input type=submit name=submit value=Submit></form>";
    ?>
    </body>
    </html>

    And here's Page 2:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
     <head>
      <title> Blah </title>
    </head>
    <body>
    <?php
    $digit 
    $_POST['number'];
    $numero = array (0=> "Uno""Dos""Tres""Cuatro""Cinco""Seis""Siete""Ocho""Nueve""Diez");
    for (
    $counter=0$counter sizeof($numero); $counter++)
    {
    if (
    $hidden[$counter] == $numbers[$counter])
    {
    echo 
    "The number <strong>$digit</strong> is the same as <em><strong>$numero[$counter]</strong></em> in Spanish.";
    }
    }
    ?>

    </body>
    </html>

    And if you need a working link to see the outcome problems... then here it is too: http://www.eight7teen.com/DD/php-test/numbers.php




    Also, I forgot to mention that when I test this locally on my machine... When I submit the data from the first page, it takes me to the second page and prints the entire array... (see below)




    However, when I submit the data from the first page on my server... It takes me to the second page but it's completely blank. What is going on? The files are the exact same on the server as they are on my local machine!?!
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I'll look at this more later, but for starters it doesn't look like your hidden input on page one is being assigned to the $hidden variable on page two.

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
     <head>
      <title> Blah </title>
    </head>
    <body>
    <h2>Number Matcher</h2>
    <form method="post" action="script.php">
    <label>Please select a number:</label>
    <select name="number">
    <?php
    $numbers 
    = array("One""Two""Three""Four""Five""Six""Seven""Eight""Nine""Ten");
    for (
    $counter=0$counter sizeof($numbers); $counter++)
        {
            echo 
    "<option value='$counter'>$numbers[$counter]</option>";
        }
            echo 
    "</select>";
            echo 
    "<input type=submit name=submit value=Submit></form>";
    ?>
    </body>
    </html>
    script.php:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
     <head>
      <title> Blah </title>
    </head>
    <body>
    <?php
    $digit 
    $_POST['number'];
    $numbers = array("One""Two""Three""Four""Five""Six""Seven""Eight""Nine""Ten");
    $numero = array ("Uno""Dos""Tres""Cuatro""Cinco""Seis""Siete""Ocho""Nueve""Diez");
    $var "The number <strong>$numbers[$digit]</strong> is the same as <em><strong>$numero[$digit]</strong></em> in Spanish.";
    echo 
    $var;
    ?>

    </body>
    </html>
    I took out your hidden field, and the for loop surrounding it. Gave the options a value of the for $counter variable. Then in script.php I put both arrays and took the for loop out, then I called the correct array by the $digit post variable.
    Jeremy | jfein.net

  4. The Following User Says Thank You to Nile For This Useful Post:

    TheJoshMan (11-02-2008)

  5. #4
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Default

    thanks so much man, works like a charm!
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

  6. #5
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    If you want, you can put this all on one page so you don't have to update each page when you add new numbers. I also fixed up the HTML.
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
     <head>
      <title> Blah </title>
    </head>
    <body>
    <h2>Number Matcher</h2>
    <?php
    $numbers 
    = array('One''Two''Three''Four''Five''Six''Seven''Eight''Nine''Ten');
    if(!isset(
    $_POST['submit'])){
      echo 
    '<form method="post" action="">
    <label>Please select a number:</label>
    <select name="number">'
    ;
      for(
    $counter=0$counter sizeof($numbers); $counter++)
        echo 
    '<option value="' $counter '">' $numbers[$counter] . '</option>';
      echo 
    '</select>
    <input type="submit" name="submit" value="Submit"></form>'
    ;
    }else{
      
    $numero = array('Uno''Dos''Tres''Cuatro''Cinco''Seis''Siete''Ocho''Nueve''Diez');
      
    $digit $_POST['number'];
      echo 
    'The number <strong>' $numbers[$digit] . '</strong> is the same as <em><strong>' $numero[$digit] . '</strong></em> in Spanish.';
    }
    ?>
    </body>
    </html>

  7. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Lol, I told him to do that on MSN. He got a C- on his script. (by me)
    Jeremy | jfein.net

  8. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    http://www.php.net/manual/en/function.array-map.php

    Check out example 3.

    I stumbled across this today while looking for help on something I am doing.

    Pretty funny coincidence.

    Jason

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •