PHP dynamic drop-down menu for countries

So, I have a table called "Countries", and it looks like this:

---------------------------
|Country      |    Code   |
---------------------------
|Afganastan   |     AF    |
|ÅLAND ISLANDS|     AX    |
|  etc.       |     etc.  |
---------------------------

What I want to do is create a dynamic menu in which the user selects the country and is stored as a value that I can call after the user gets to submit.

I tried something here, but I'm not sure what it does, because I'm still new to PHP and HTML to the extent that I just enter things to see what happens.

In any case, I was really stuck, and I tried to use google and the search function on this site, and nothing I found worked for me ...

The code I tried is as follows:

<select>
    <?php
        $result = mysql_query('SELECT Country FROM Countries');

        echo '<select name="country">';

        while ($row = mysql_fetch_array($result))
        {
           echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }

        echo '</select>';
    ?>

   </select>

. :

.$row['name']

, , . , , .

:

<select name = 'country'>
    <?php

    include ("account.php");
    include ("connect.php");

        $result = mysql_query('SELECT Code , Country FROM Countries');
        while ($row = mysql_fetch_array($result))
            {?>

            <option value="<?php echo $row['Code']?>"><?php echo $row['Country']?></option>

            <?php}

    ?>

   </select>

include ("account.php"); include ("connect.php"); .

+3
9

-

$host = "localhost";
$user = "root";
$pass = "yourpassword";
$db   = "databasename";

// This part sets up the connection to the 
// database (so you don't need to reopen the connection
// again on the same page).
$ms = @mysql_connect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}

// Then you need to make sure the database you want
// is selected.
@mysql_select_db($db);


<form method = "POST" action = "abc.php">
<select name = 'country'>
<?php
$result = mysql_query('SELECT id , name FROM Countries');
while ($row = mysql_fetch_array($result))
    {?>
       <option value="<?php echo $row['id']?>"><?php echo $row['name']?></option>
    <?php}
?>
<input type = "submit" value = "Submit">
</form>

php

echo '<pre>';
print_r($_POST);

, . , , - .

+1

 <?php
        $result = mysql_query('SELECT * FROM Countries');
?>
<select name="country">
<?php 

        while ($row = mysql_fetch_array($result))
        {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php
        }
?>
</select>
0

:

echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';

:

echo "<option value=\"" . $row['id'] . "\">" . $row['name'] . "</option>";

script, , $row['id'] $row['name'], , , ... , / , , $row[\'id\'] $row[\'name\']

Thant .

0

-, id. ,

SELECT Country, Code FROM Countries

html

<?php

$host = "localhost";
$user = "user";  //username
$pass = "pass";  //password
$db   = "db";    //database

$con = @mysql_connect($host, $user, $pass);
if ( !$con )
{
echo "Error connecting to database.\n";
}

@mysql_select_db($db);
?>

<select name="country">
<option value="0" selected="selected">Choose..</option>
<?php
//echo '<select name = \'country\'>';

    $result = mysql_query('SELECT Country, Code FROM Countries');

    while ($row = mysql_fetch_array($result))
    {
       echo '<option value="'.$row['Code'].'">'.$row['Country'].'</option>';
    }

?>
</select>
0
  <select>
    <?php
    $result = mysql_query('SELECT Country FROM Countries');

    echo '<select name="country">';
    $row = mysql_fetch_array($result)
    for ($i=0; $i<count($row ); $i++)
    {
       echo '<option value="'.$row[$i]['id'].'">'.$row[$i]['name'].'</option>';
    }

    echo '</select>';
?>

print_r ($ _ POST); var_dump ($ _ );

0

mysql_fetch_array, , , . sprintf printf, , HTML , .

$result = mysql_query('SELECT Country, Code FROM Countries');
while ($row = mysql_fetch_array($result)) {
   printf('<option value="%1$s">%2$s</option>',
            $row['Code'], $row['Country']);
}
0

SQL "" ""; $row ['id'] $row ['name'].

:
  

    echo '<select name="country">';

    while ($row = mysql_fetch_array($result))
    {
       echo '<option value="'.$row['Code'].'">'.$row['Country'].'</option>';
    }

    echo '</select>';
?>

.

0

I realized that I was having a problem. The first thing on the page is an additional select tag, and the second is a file that was saved as an html page, not a php file. Thanks to everyone who helped me figure this out!

0
source

Try my code, I use this and it really works ... just change the values ​​...

<?php
    include ('connect.php');

    $sql = "SELECT * FROM casestatusfile";
    $result = mysql_query($sql);

    echo "<select name = 'txtCaseStatus'/>";
    echo "<option value = ''>--- Select ---</option>";
    $casestatus = $_POST['txtCaseStatus'];
    $selected = 'selected = "selected" ';
    while ($row = mysql_fetch_array($result)) {
    echo "<option " .($row['CASESTATUSCODE'] == $casestatus? $selected:''). "value='". $row['CASESTATUSCODE'] ."'>" . $row['CASESTATUS'] ."</option>";
    }
    echo "</select>";
    ?>

I am sure this works because it is the one that I use ....

0
source

All Articles