I have a PHP script which connects to a MySQL database and creates an HTML dropdown list with data retrieved from it. The script works, I just don't understand how I'm supposed to use it in an HTML form.

The PHP script, named CountryList.php, looks like this:

<?php
function createCountryList() {
    $con = mysql_connect("localhost","user","pass");
    mysql_select_db('database', $con);

    $sql="SELECT Country FROM CountryList";
    $result = mysql_query($sql,$con);

    echo "<select name=country value=''>Country</option>";
    echo "<option value=0>Select Country</option>";
    echo "<option value=1></option>";

    $curvalue=2;
    while($nt=mysql_fetch_array($result)){
        echo "<option value=$curvalue>$nt[Country]</option>";
        $curvalue = $curvalue+1;
    }
    echo "</select>"; 
    mysql_close($con);
}
?>

I tried including the PHP file in the head of the HTML page with:

<?php include("CountryList.php"); ?>

And then I tried calling the function which creates the dropdown menu in a form later on:

<form action="insert.php" method="POST">
<label>Which country are you from?</label>
<?php createCountryList(); ?>
</form>

Since nothing happens, I did it wrong. Any advice? Help is appreciated.

EDIT: Bah, I knew it was something silly. The page was an HTML file, PHP didn't process it. Changing the HTML file to PHP solved everything.

share|improve this question
Please add error_reporting(E_ALL); on the first line of the script to get notified of any php related errors. What does the view source in the browser tell you? – Shoan Sep 5 '11 at 4:22
Nothing seems to happen. View source shows me my HTML page exactly as I wrote it. – SpeedBurner Sep 5 '11 at 4:35

4 Answers

What happens when you change this line

echo "<select name=country value=''>Country</option>";

to this

echo "<select name='country'>Country";
share|improve this answer
Oh, thanks for catching the typos. Still doesn't show anything but it might be some other issue now. – SpeedBurner Sep 5 '11 at 4:25

Are you sure its in the same folder. Its a good practice to do a check before

if ((include 'CountryList.php') == 'OK') {
createCountryList();
}
else{
echo "Didnt import";
}

Dont do brackets like include()

share|improve this answer
Okay, I just realized echos don't print anything on the HTML page. What could be the cause of that? – SpeedBurner Sep 5 '11 at 4:24

Try changing echo "<option value=$curvalue>$nt[Country]</option>"; to echo "<option value=$curvalue>{$nt['Country']}</option>";

Echoing out arrays requires curly brackets around the array index if inside a string. Also the index needs quotes or php will assume constant.

share|improve this answer

This is what I did in one of my HTML forms. Try and see if it works for you.

<select name="country"><?php createCountryList();?></select>

What you do here is on the page where you want the selectbox, you put in this code and call the function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.