views:

59

answers:

4

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=$nt[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?.

A: 
<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.

Ignacio Vazquez-Abrams
you didnt get the point. ok let us say i should use post. then how should pass the value of the dropdown? for example a user chose female as her gender, then she tries to edit it to male, how can i echo the value gender which is female in the dropdown? same as i can echo her name after choosing the edit button. thank you.
kester martinez
can you show me the correct syntax for that?
kester martinez
... You don't know how to write an `if` statement?
Ignacio Vazquez-Abrams
A: 

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.

stunaz
you didnt get the point. ok let us say i should use post. then how should pass the value of the dropdown? for example a user chose female as her gender, then she tries to edit it to male, how can i echo the value gender which is female in the dropdown? same as i can echo her name after choosing the edit button. thank you.
kester martinez
A: 

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
}
Ruel
my bad. typo error.. you didnt get the point. ok let us say i should use post. then how should pass the value of the dropdown? for example a user chose female as her gender, then she tries to edit it to male, how can i echo the value gender which is female in the dropdown? same as i can echo her name after choosing the edit button. thank you.
kester martinez
+1  A: 
<?
    $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>";
?>
stunaz
thanks. got it now.
kester martinez