开发者

passing values using get from a dropdown populated from a mysql database

开发者 https://www.devze.com 2023-01-18 14:18 出处:网络
i have problem passing data from one page to another using GET, for example i have these: <form method=post action=edit.php>

i have problem passing data from one page to another using GET, for example

i have these:

<form method=post action=edit.php>
<td><input type=text name=firstname></td>
<td>
<?
    $query="SELECT * FROM gender;
    $result = mysql_query ($query);
    echo "<select name=gender_id>";
    while($nt=mysql_fetch_array($result))
    {
    echo "<option value=$n开发者_JAVA技巧t[gender_id]>$nt[gender_name]</option>";
    }
    echo "</select>";
?>
</td>
<td><input type=submit name=edit></td>
</form>

now to pass these to edit.php using GET

if($mode=="edit")
{
$fistname=$_GET["fistname"];
$gender=$_GET["gender_id"];
<td><input type=text name=firstname value="<? echo $fistname; ?>"></td>

Above is a working code for an input type text. I know how to pass values with input type text but my problem is that HOW WOULD I DO THESE WITH A SELECT tag WHICH HAS VALUES FROM A MYSQL DATABASE?.


<?
    $query="SELECT * FROM gender;
    $result = mysql_query ($query);
    echo "<select name=gender_id>";
    while($nt=mysql_fetch_array($result))
    {
    if ($nt[gender_id]==$_POST["gender_id"])
       $selected="selected";
    else
        $selected="";
    echo "<option ".$selected."value=$nt[gender_id]>$nt[gender_name]</option>";
    }
    echo "</select>";
?>


you were using <form method=**post** action=edit.php>, in that way you should use $fistname=$_POST["fistname"]; $gender=$_POST["gender_id"];

if you want to use $fistname=$_GET["fistname"]; $gender=$_GET["gender_id"]; you should use <form method=**get** action=edit.php but in that way, the thoses values will be visible in the url.


You have a typographical error on your edit.php change $_GET['fistname'] to $_GET['firstname']. Then edit:

<form method=post action=edit.php>

and put method="get"

<form method=post action=edit.php method="get">

And to prevent errors on edit.php. Add these:

if ((isset($_GET['firstname']) && (isset($_GET['gender_id'])) {
    //your code
} else {
    //some error message
}


<form method="GET" action="edit.php">

Or use $_POST in the script instead.

EDIT, since you didn't bother putting your question in the question:

As you iterate through the options, test the value of $nt[gender_id] and add the selected attribute to the one that matches.

0

精彩评论

暂无评论...
验证码 换一张
取 消