Log in

View Full Version : echo selectbox value and matching array item



TheJoshMan
11-01-2008, 02:54 PM
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):


<!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:


<!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)

http://www.eight7teen.com/DD/php-test/locally.jpg


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!?!

JasonDFR
11-01-2008, 10:49 PM
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.

Nile
11-02-2008, 02:17 AM
Here:

<!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:


<!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.

TheJoshMan
11-02-2008, 02:38 AM
thanks so much man, works like a charm!

techietim
11-02-2008, 02:52 AM
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.


<!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>

Nile
11-02-2008, 03:59 AM
Lol, I told him to do that on MSN. He got a C- on his script. (by me)

JasonDFR
11-02-2008, 06:47 PM
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