PDA

View Full Version : form redirect



lord22
08-05-2008, 10:22 AM
Hi I have a simple login (user and password) form.
This form also includes this multiple menu:


<select name="selectgrade" id="selectgrade">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>


How can I do that each option will redirect the user to another url?

Thanks

Jesdisciple
08-05-2008, 09:43 PM
Use the onclick attribute of each <option> to set window.location.href.

lord22
08-05-2008, 09:54 PM
but I should redirect the user only when he clicked the button (and he passes the validation..)

mburt
08-05-2008, 09:57 PM
<select name="selectgrade" id="selectgrade" onchange="window.location.href=this['options'][this['selectedIndex']]['value']">
<option></option>
<option value="http://www.google.ca/">A</option>
<option value="http://www.yahoo.com/">B</option>
<option>C</option>
<option>D</option>
</select>

mburt
08-05-2008, 10:00 PM
Actually, after re-reading your post, why don't you just do this with $_POST["selectgrade"] on the validation page? That is, assuming you are using a form.

lord22
08-05-2008, 11:42 PM
This is exatcly what I've tried to do, I just don't know how to get the information from the select menu

rangana
08-06-2008, 12:58 AM
This is working:


<?php echo $_POST['selectgrade']; ?>
<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
<select name="selectgrade" id="selectgrade">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
<input type="submit" value="Submit">
</form>


Now, what's the problem on it? What validation are you expecting?

Hope that keeps you going.

Jesdisciple
08-06-2008, 01:13 AM
Yeah, sorry I didn't look at the "PHP" at the top of the page. I was trying to answer a JavaScript question you didn't have.

motormichael12
08-06-2008, 02:01 AM
The $_POST data will echo the value of the selected index, so you need a value tag in each option.


<?php echo $_POST['selectgrade']; ?>
<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
<select name="selectgrade" id="selectgrade">
<option value="option_a">A</option>
<option value="option_b">B</option>
<option value="option_c">C</option>
<option value="option_d">D</option>
</select>
<input type="submit" value="Submit">
</form>

lord22
08-06-2008, 01:58 PM
Hi

can someone tell what's wrong in here:

$sql="INSERT INTO grades (selectgrade)
VALUES
('$_POST['selectgrade']')";

?

It wrote me that there is some syntax error there.

Jesdisciple
08-06-2008, 02:41 PM
What's the exact error message? The parens look wrong to me, but I can't tell you for sure.

lord22
08-06-2008, 03:30 PM
MY FORM:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>database first try</title>
</head>

<body>
<form method="post" action="add2.php">
<table>
<tr>
<td>link url:</td>
<td><input name="link_url" type="text" /></td>
</tr>
<tr>
<td>link name:</td>
<td><input name="link_name" type="text" /></td>
</tr>
<tr>
<td>choose:</td>
<td>
<select name="selectgrade">
<option value="option_a">A</option>
<option value="option_b">B</option>
<option value="option_c">C</option>
<option value="option_d">D</option>
</select>
</td>
</tr>
</table>
<input type="submit" value="sumbit" />
</form>
</body>
</html>



add2.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>database first try</title>
</head>

<body>
<?php

$connection = mysql_connect("localhost",
"XXX",
"XXX");
if (!$connection)
die('Could not connect: ' . mysql_error());

mysql_select_db("XXX", $connection);

$sql="INSERT INTO links (link_url, link_name, selectgrade)
VALUES
('$_POST[link_url]','$_POST[link_name]','$_POST['selectgrade']')";

if (!mysql_query($sql,$connection))
die('Error: ' . mysql_error());


echo "1 record added";

mysql_close($connection);
?>

</body>
</html>



What I've tried to do is to get the selected value and insert it into a database.

Jesdisciple
08-06-2008, 03:42 PM
Thank you for posting the entire source, but what's the error message?

lord22
08-06-2008, 04:04 PM
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Jesdisciple
08-06-2008, 04:18 PM
The code you originally said was causing the problem was
$sql="INSERT INTO grades (selectgrade)
VALUES
('$_POST['selectgrade']')";But if that line became this
$sql="INSERT INTO links (link_url, link_name, selectgrade)
VALUES
('$_POST[link_url]','$_POST[link_name]','$_POST['selectgrade']')";then change it to this
$sql="INSERT INTO links (link_url, link_name, selectgrade)
VALUES
('{$_POST['link_url']}','{$_POST['link_name']}','{$_POST['selectgrade']}')";

See the second code-section at http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.

lord22
08-06-2008, 04:40 PM
Hi thank you,
now there are no errors, but still it doesn't insert it in the right way. All the values from the select menu turned into 0 in the database.

Jesdisciple
08-06-2008, 04:49 PM
Try this anywhere in that script and report the output:
var_dump(isset($_POST['selectgrade']));
var_dump($_POST['selectgrade']);

motormichael12
08-06-2008, 06:16 PM
I think it might have something to do with your single quotes. Here is the way it would be saved.


$sql="INSERT INTO grades (selectgrade) VALUES ('$_POST['selectgrade']')";

Try this:

$grade = $_POST['selectgrade'];
$sql="INSERT INTO grades (selectgrade) VALUES ('{$grade}')";

Also as to the problem with changing to 0, make sure that the field in the DB is set to text/varchar and not int.

lord22
08-07-2008, 05:07 AM
Thank you very much!!
Now it is working :)