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
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"];

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.