0

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

1
  • i do not understand the problem what $gender contain ? Commented Oct 5, 2010 at 5:20

4 Answers 4

1
<?
    $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>";
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

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

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
}

1 Comment

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

2 Comments

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.
... You don't know how to write an if statement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.