Jump to content


Photo

Php Form [Solved]


  • Please log in to reply
12 replies to this topic

#1 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 July 2006 - 06:58 PM

I'm trying to get a drop down menu to work, but I can't seem to get it to work at all. Its got to be in the <?php ?> code as its to get values dynamically.

The list is going to be filled with 2 fields from a MySQL database. The Name field is going to be the text shown in the drop down menu, the value is going to be the value from ID colum in the database.

<?php
$result = mysql_query("SELECT * FROM client")
or die(mysql_error());  



echo '<form name="form1" method="post" action="">';
echo '<select name="select">';
//make some short variables
$ID=$row['ID'];//suppy the correct field names below
$Name=$row['Name'];
while($row = mysql_fetch_array( $result )) {
echo '<option value="' . $ID . '">' . $Name . '</option>'; 
} 
echo "</select>";
echo "</form>";

?>

Although the menu is filled with the correct number of blank entries, but the every option is a " and every value is ' ' Does anyone know what I'm doing wrong?

#2 jvrothjr

jvrothjr
  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts
  • LocationOhio

Posted 12 July 2006 - 07:09 PM

$ID=$row['ID']; & $Name=$row['Name']; Needs to be within the loop to change the data.

<?php
$result = mysql_query("SELECT * FROM client")
or die(mysql_error());  

echo '<form name="form1" method="post" action="">';
echo '<select name="select">';
//make some short variables
while($row = mysql_fetch_array( $result )) {

$ID=$row['ID'];//suppy the correct field names below
$Name=$row['Name'];


echo '<option value="' . $ID . '">' . $Name . '</option>'; 
} 
echo "</select>";
echo "</form>";

?>

If you understand everything you know nothing!

http://rcchjr.awardspace.com/

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 July 2006 - 07:10 PM

These two lines:
<?php
$ID=$row['ID'];//suppy the correct field names below
$Name=$row['Name'];
?>
need to be inside the while loop.

Ken

#4 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 July 2006 - 07:15 PM

LOL, it always seems so obvious after someone points it out to you!

Thanks guys!

#5 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 July 2006 - 03:11 PM

Right I've got a problem linking from this.

I have set up the next page which which has the 
$ID=$HTTP_POST_VARS['ID'];

To get the value from the above, the only problem is it doesnt get the value.

Any Ideas?

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 03:14 PM

Use the superglobal array $_POST instead:
<?php $ID = $_POST['ID']; ?>

Ken

#7 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 July 2006 - 03:15 PM

Yehh I tryed that one as well, but it doesnt get a value either.

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 03:20 PM

Put
<?php
echo '<pre>$_GET: ' . print_r($_GET,true) . '</pre>';
echo '<pre>$_POST: ' . print_r($_POST,true) . '</pre>';
?>
at the start of your script to see what is being passed to it.

Ken

#9 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 July 2006 - 03:38 PM

It returns the following

Warning: Wrong parameter count for print_r() in \\www\ViewDetails.php on line 16

$_GET:


Warning: Wrong parameter count for print_r() in \\www\ViewDetails.php.php on line 17

$_POST:


Do I have to give a parameter for that to work?

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 03:49 PM

What version of PHP are you running? Did you insert the lines exactly as I showed?

If the version of PHP is less than 4.3.0, you would need:
<?php
echo '<pre>';
echo '$_GET: ';
print_r ($_GET);
echo '$_POST: ';
print_r($_POST);
echo '</pre>';
?>

Ken

#11 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 July 2006 - 06:35 PM

Its seems to be working.

$_GET: Array
(
)
$_POST: Array
(
    [select] => 2
)

Is what it returns, but surely that would mean that the $_POST should have worked?

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 07:19 PM

The problem here is that you were looking for the value in the wrong index, You were looking for $_POST['ID'] when the value is in $_POST['select'].

Ken

#13 Bob_

Bob_
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 July 2006 - 08:16 PM

Fantastic, that done the trick.  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users