Jump to content

get selected from html select menu


CyberShot

Recommended Posts

I have a test database, I have two names in the database which get returned just fine in my html dropdown. I am trying to figure out how to figure out which item in the list was selected so that I can return information from another table based on the selected id. This is what I am trying now but I don't know how to proceed or if it is correct

 


$result = $mysql->query("SELECT * FROM names") or die($mysql->error);
?>
<select>
<?php
if($result){
while($row = $result->fetch_object()){
	$id = $row->nameID;
	$name = $row->firstName . " " . $row->lastName;
	?>

		<option value"<?php $id ?>"><?php echo $name ?></option>

	<?php
}?>
</select>
<?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/
Share on other sites

okay, i did that like so

 

$list = $_POST["listNames"];
if($result){
echo "<select name=\"listNames\">";
while($row = $result->fetch_object()){
	$id = $row->namesID;
	$name = $row->firstName . " " . $row->lastName;

	echo "<option value\"$id\">$name </option>";

}
echo "</select>";

echo $list;
} 

 

but I get an error saying Undefined index: listNames  on this line

 

$list = $_POST["listNames"];

nope. I thought because I didn't have the select list in a form tag. but it's still not working.

 

<form method="post" name="form2" action="">
<?php

if($result){
echo "<select name=\"listNames\">";
while($row = $result->fetch_object()){
	$id = $row->namesID;
	$name = $row->firstName . " " . $row->lastName;

	echo "<option value\"$id\">$name </option>";

}
echo "</select>";

echo $list;
} 
?>
  </form>

I am working on that now. What would you suggest I do? I want this select box to be in a different location on my page. So it is outside the main form which means I made a second form that when you push the submit button goes to my formdata.php page. But I already have a isset function for the first submit button. How would you suggest handling the second submit button on this page?

okay, I did that. But I am coming up empty. I know the button works because I am getting the success echoed

 

my form

<form method="post" name="form2" action="formData.php">
<?php

if($result){
echo "<select name=\"listNames\">";
while($row = $result->fetch_object()){
	$id = $row->namesID;
	$name = $row->firstName . " " . $row->lastName;

	echo "<option value\"$id\">$name </option>";

}
echo "</select>";
} 
?>
<input type="submit" name="form2" value="submit">
  </form>

 

and my formData.php

if(isset($_POST['form2'])){
$list = $_POST["listNames"];
echo $list;
echo "Success";
}

First you should ask yourself if there's a reason you actually need to have two forms on the same page. Then, if there truly is:

 

I nearly always include a hidden form field to use solely to see if the form has been submitted. You could do the same, giving each of them a different value.

 

<input type="hidden" name="submitted" value="form1">, and another one for form 2.

then

if( isset($_POST['submitted']) ) {
     if( $_POST['submitted'] == 'form1' ) {
          // do the form 1 stuff.
     }
     if( $_POST['submitted'] == 'form2' ) {
          // do the form 2 stuff.
     }
}

okay, I made all those changes. Good catch. I am still coming up empty. it's not echoing out the list. Which I am doing simply to test that the code is working

 

here is both scripts just in case

<form method="post" action="formData.php" id="form1">
<table width="350" border="0">
<tr>
	<td>First Name:</td>
	<td><input type="text" name="firstName" id="first" /></td>
</tr>
<tr>
	<td>Last Name:</td>
	<td><input type="text" name="lastName" id="last" /></td>
</tr>
<tr>
	<td>Company Name:</td>
	<td><input type="text" name="companyName" id="company" /></td>
</tr>
</table>

<br />
<br />

<table width="350" border="0">
<tr>
	<td>Home Phone:</td>
	<td><input type="text" name="homePhone" id="homePhone" /></td>
</tr>
<tr>
	<td>Cell Phone:</td>
	<td><input type="text" name="cellPhone" id="cellPhone" /></td>
</tr>
<tr>
	<td>Company Phone:</td>
	<td><input type="text" name="companyPhone" id="companyPhone" /></td>
</tr>
<tr>
	<td>Amount Owed:</td>
	<td><input type="text" name="amount" id="amount" /></td>
</tr>
<tr>
	<td> </td>
	<input type="hidden" name="submitted" value="form1">
	<td><input type="submit" value="submit" name="submit" id="submit"/></td>
</tr>
</table>
</form>

 

the second form

<form method="post" name="form2" action="formData.php">
<?php

if($result){
echo "<select name=\"listNames\">";
while($row = $result->fetch_object()){
	$id = $row->namesID;
	$name = $row->firstName . " " . $row->lastName;

	echo "<option value=\"{$id}\">$name </option>";

}
echo "</select>";
} 
?>
<input type="hidden" name="submitted" value="form2">
<input type="submit" name="form2" value="submit">
</form>

 

and the action page

if(isset($_POST['submitted'])){
if($_POST['submitted'] == 'form1')
	if(isset($_POST['submit'])){
	$firstName = $_POST['firstName'];
	$lastName = $_POST['lastName'];
	$companyName = $_POST['companyName'];
	$homePhone = $_POST['homePhone'];
	$cellPhone = $_POST['cellPhone'];
	$companyPhone = $_POST['companyPhone'];

	//checking the values are filled
	//echo $firstName. " " . $lastName . " " . $companyName . " " . $homePhone . " " . $cellPhone . " " . $companyPhone;

	$mysql->query("INSERT INTO names(firstName,lastName,companyName)values('$firstName','$lastName','$companyName')");
}  if( $_POST['submitted'] == 'form2' ){
		$list = $_POST["listNames"];
		echo $list;
		echo "Success";
	}
}

 

Thanks for the help

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.