0

I have drop downs built by a PHP function pulling from MySQL tables. The drop downs work fine but the selected option isn't POSTing. I suspect the problem is with how the HTML select tag is created in PHP but I can't seem to find a way to correct it. I've searched around but nothing seems to work.

Here is the PHP function to build the drop down:

function contact_list(){
//Connection info
$dbhost = 'localhost';
$db = 'database';
$dbuser = 'user';
$dbpass = 'password';

//Define connection
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $db);

//Check connection
if (mysqli_connect_errno()) {
    echo " ERROR " . mysqli_connect_error() . " ERROR ";
        }  
 //Define search strings for drop down       
 $contact_list = "SELECT * FROM CONTACT_LIST";       

//Execute search of primary_contact table
if (!mysqli_query($con,$contact_list)) {
    die('' . mysqli_error($con));
}
//Return primary_contact results as string
$contact_result = $con->query($contact_list);

//Build drop down
echo "<select name='Primary_Contact'>";
//Loop through results
foreach ($contact_result as $row)
{
    echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
}

echo "</select><p></p>";
}

Here is a sample of the HTML. The Description text box is fine so I don't think the problem is with the form tags.

<form action="mod_output_test.php" method="POST">  
    <u>Description</u>:  <p></p>
    <input type="text" name="Description" size="48" value="<?=$Description;?>"> <p></p>
    <!-- Primary Contact -->
    <?php echo "<u>Primary Contact</u>:<p></p>Current value:&nbsp<b> " . $Primary_Contact . "</b>&nbsp New value: ";?>
    <?php contact_list() ?>
    <p></p>
    <input type="submit">
</form>
3
  • What is the actual html output? Commented Jan 22, 2015 at 15:54
  • First thing that comes to mind is there is no <form> tag, but since you didn't post the HTML, I could be wrong. Commented Jan 22, 2015 at 15:55
  • Edited to include <form> along with a text field that does work. Commented Jan 22, 2015 at 16:22

2 Answers 2

1

Change

echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";

to

echo "<option value='".$row['Id']."'>" .htmlspecialchars($row['Primary_Contact']). "</option>";

And I am guessing your dropdows is between a form tag.

in the top of the action file type this and tell us what you get

<?php
print_r($_REQUEST);
exit;
?>
Sign up to request clarification or add additional context in comments.

Comments

0
echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";

When you post like that, option value is empty. Add the id of the contact.

As they said, we see no <form> tag, we "assume" you add it before that piece. But the answer to "is not posting" is because value from <option> is empty.

2 Comments

Edited to include <form> along with a text field that does work.
Good, make sure also to add the value to the <option> tag also. If not, no value will have the variable when posting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.