Jump to content

Dropdown List Help - Writing Value To A Procedure


tooNight

Recommended Posts

Hi there,

Trying to have a selectbox that queries the database which contains two fields countryID and countryName and outputs countryID. I can get it to work ok but not when I put [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]if($form->value("country") == "$row[countryID]"){ echo "selected"; }[/quote] which needs to be in to write the value to my procedures. If someone would be so kind to point me in the right direction and tell me what syntax mistake I am making it would be a great help.

Thanks very much

The code below works but I need to insert if($form->value("country") == "$row[countryID]"){ echo "selected"; } where it says HERE, what is the best way of doing this?
[code]

<?php
$p2=mysql_query("SELECT countries.countryID, countries.countryName FROM countries") or die(mysql_error());
echo "<select name=country>";
while($row=mysql_fetch_assoc($p2)) {
echo "<option value=$row[countryID]HERE>$row[countryName]</option>";
}
echo "</select>";
?>
[/code]


My shocking attempt at it which I keep getting errors with.
[code]
<?php
$p2=mysql_query("SELECT countries.countryID, countries.countryName FROM countries") or die(mysql_error());
echo "<select name=country>";
while($row=mysql_fetch_assoc($p2)) {
echo "<option value=$row[countryID] {if($form->value("country") == "$row[countryID]"){ echo "selected"; }}>$row[countryName]</option>";
}
echo "</select>";
?>
[/code]
Link to comment
Share on other sites

try

[code]<?php
include 'db.php'; // db connect

function countryOptions($current) {
        $sql = "SELECT ID, countryName
                 FROM country
                 ORDER BY countryName";
        $str = "<option value=''>- select country -</option>\n";

        $res = mysql_query($sql) or die (mysql_error());
        while (list ($id, $name) = mysql_fetch_row($res)) {
               $selected = $id==$current ? 'SELECTED' : '';
               $str .= "<option value='$id' $selected>$name</option>\n";
        }
        return $str;
}

$country = '';

if (isset($_GET['country'])) {
    $country = $_GET['country'];
    echo "You selected $country";
}

?>
<FORM method='GET'>
<select name='country'>
        <?php echo countryOptions($country)?>
</select>

<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</FORM>[/code]
Link to comment
Share on other sites

Thanks this code should could in handy though fixed my code by changing:

so this:
[code]echo "<option value=$row[countryID] {if($form->value("country") == "$row[countryID]"){ echo "selected"; }}>$row[countryName]</option>";
} [/code]

becomes:
[code]echo "<option value=$row[countryID] ".(($form->value('country')==$row['countryID'])?'selected':'').">$row[countryName]</option>";
} [/code]

But think your way is more efficient, so maybe will implement it later if I have time.

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