Reputation: 131
I am currently tring to create a form wherein there is three dropdown option boxes and one submit button. All three of the dropdown boxes are populated from the database and I would like the selected options to be included into a new query and printed. This example only shows one dropdown
PHP Code
// Create connection
$con=mysqli_connect('', '', '', '');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$course_dropdown ="";
$query_course = "SELECT * FROM course";
$result_course = mysqli_query($con,$query_course) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_course))
{
$course_dropdown .= "<option value='{$row['CourseName']}'{$row['CourseName']} </option>";
}
Above is the code that is used to create the dropdown lists
HTML
<form="index.php" method="post">
<select name="Course"><?php echo $course_dropdown; ?></select>
<input name="button" value="Submit" type="submit">
I am at a loss over what way to proceed, I have tried various different techniques but cannot come up with an answer.
Latest attempt
$course = mysqli_real_escape_string($con, $_POST['Course']);
$query = mysqli_query($con,"SELECT * FROM course_module WHERE CourseName = $course");
this brought an error
Notice: Undefined index: Course in C:\Users\seanin\Desktop\xampp\htdocs\index.php on line 33
So have edited as suggested and stil have an error, may be missing something small.
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$course_dropdown ="";
$query_course = "SELECT * FROM course";
$result_course = mysqli_query($con,$query_course) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_course))
{
$course_dropdown .= "<option value='{$row['CourseName']}'>{$row['CourseName']}</option>";
}
if ($_POST['button'] == 'Submit') {
$course = mysqli_real_escape_string($con, $_POST['Course']);
$query = mysqli_query($con,"SELECT * FROM course_module WHERE CourseName = $course");
}
Still have this error
Notice: Undefined index: button in C:\Users\seanin\Desktop\xampp\htdocs\index.php on line 30
submit button issue
Nearly done, thanks for all the help so far.
What do I need to do to get the results and print them???
Upvotes: 2
Views: 3122
Reputation: 5389
I reckon that you are trying to access 'Course' in the following line and it is not defined:
$course = mysqli_real_escape_string($con, $_POST['Course']);
Are you able to submit the page? There is an error in your HTML form: <form="index.php"
is not a valid HTML tag so you are not able to submit the page, that is if you posted the exact code you are using. Your form should be:
<form action="index.php" method="post">
<select name="Course"><?php echo $course_dropdown; ?></select>
<input name="button" value="Submit" type="submit">
</form> <!-- and don't forget the closing tag -->
You can check whether the page was submitted or not by doing something like this:
if ($_POST['button'] == 'Submit') {
$course = mysqli_real_escape_string($con, $_POST['Course']);
// please note the missing single quotes, and please read the first line of my answer
$query = mysqli_query($con,"SELECT * FROM course_module WHERE CourseName = '$course'");
}
There is also an invalid HTML syntax in the following line:
$course_dropdown .= "<option value='{$row['CourseName']}'{$row['CourseName']} </option>";
The format for <option>
is: <option value="value">label</option>
.
Upvotes: 2
Reputation: 627
The reason why your dropdown is not working is missing " > " replace the line inside while loop with this
$course_dropdown .= "<option value='{$row['CourseName']}'>{$row['CourseName']}</option>";
Upvotes: 2