Reputation: 9064
I have a drop down list which is dynamically populated from a MySQL database :
<select name= "studentnames" >
<option value="0">Select a Student</option>
$RSStudentName = mysql_query( "SELECT StudentName, StudentID FROM Students " );
while($StudentNameList = mysql_fetch_object($RStudentName))
{
<option value = "<?php echo $StudentNameList->StudentID ;?> " >
<?php
echo $StudentNameList->StudentName ;
?>
</option>
<?php
}
?>
</select>
The drop down gets filled these items as below :
Student1
Student2
Student3
Student4
Student Table is as :
SID | SNAME
S001 | Student1
S002 | Student2
S003 | Student3
Now when the users selects a student name from the drop down list, i want to get the value of that option , and insert it into a table. How to do this?
That is, if user selects Student3
in the drop down list, then i want to get S003
in some variable and insert it in a table? Please help with how to get the value attribute in a variable.
Could this be done?
$sid = $_POST('studentnames').value;
Upvotes: 0
Views: 2110
Reputation: 69
$Getname = $_POST['studentnames'];
This gets the value of what the user selects
then sql should get like
$query = "INSERT INTO Members (studentnames)VALUES('$Getname')"; $result = mysql_query($query);
That SQL will add the $Getname on to the Members table
Upvotes: 0
Reputation: 8616
If it's a simpel form post: the selected value will be in:
$_POST['studentnames']
In your code, use the ID as VALUE, and the name just as text
<option value="[id field]">[studentname]</option>
then
$_POST['studentnames']
will contain the is, might be better then naming it simply 'student'...
Upvotes: 1
Reputation: 7294
Your form tag should have a method="post" attribute. Then you can access the data using:
$value = $_POST['studentnames'];
Remember that accepting raw $_POST data opens your project up to various injection security issues. Make sure you clean and validate the data before it goes anywhere near the database or is displayed back to the user.
Upvotes: 1
Reputation: 566
Post it with a form, and get the value like this:
<?php
$somevariable = $_POST["studentnames"];
?>
Upvotes: 0