Reputation: 115
I am trying to create a form where the user selects an option from a drop down list and the results are displayed in a separate php page. However, I am encountering issues with getting the information from the form and not sure if the php is picking the value up. I use jQuery to serialize the information (there is only one variable) and have attempted to see if something is being stored using var_dump
and print_r
but nothing gets returned.
Any help please?
the form:
<div class="modal fade" id="searchCModal" tabindex="-1" role="dialog" aria-labelledby="searchCModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Search By Course: </h4>
</div>
<div class="modal-body">
<form id="searchCForm" class="searchCForm">
<?php
include "db_conx.php";
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt3 = $db_conx->prepare('SELECT * FROM course_details ORDER BY course_title');
$stmt3->execute();
$courses = $stmt3->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
<div class="control-group">
<label class="control-label" for="course_details">Select Course:</label><p></p>
<select name="course">
<option value=''>Select One</option>";
<?php foreach($courses as $course): ?>
<option value="<?php echo $course['course_code'] ?>"><?php echo $course['course_title'] ?></option>
<?php endforeach ?>
</select>
</div>
</form>
</div>
<div class="modal-footer">
<div class="btn-toolbar">
<button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" class="pull-right" onclick="searchCCall();">Submit <span class="glyphicon glyphicon-saved"></button>
</div>
</div>
the jQuery:
function searchCCall() {
var data = $('#searchCForm').serialize();
$.post('Student_SearchCourse.php', data, function(response){
$("#searchCForm").html(response);
window.location.href = ('Student_SearchCourse.php');
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
the php:
<?php
include "db_conx.php";
if
(!isset ($_POST['course']))
{
$message = 'Please select a valid course';
}
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$course = $_POST['course'];
$sql = $db_conx->prepare("SELECT DISTINCT p.proposal_id, p.proposal_title, p.description, c.course_title FROM proposal p
LEFT JOIN course_details c on c.course_code = p.course_code
WHERE p.course_code = :course");
$sql->bindParam(':course', $course, PDO::PARAM_STR);
$sql->execute();
//$user_record_id = $sql->fetchColumn();
print_r($course);
$proposals = $sql->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
Any ideas on how I can overcome this issue? Thanks in advance!
Upvotes: 1
Views: 45
Reputation: 989
Change
$course = $_GET['course'];
to
$course = $_POST['course'];
because your using post method to submit data.
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" role="form" id="frmlogin" action="Student_SearchCourse.php">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Login</h4>
</div>
<div class="modal-body" >
<!-- your form element -->
</div>
<div class="modal-footer">
<div class="btn-toolbar">
<button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success" class="pull-right" >Submit <span class="glyphicon glyphicon-saved"></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
you can wrap your form tag inside <div class="modal content"></div>
.
no need to use js.
Upvotes: 1
Reputation: 1
Check if php is outputting the course code to the optionvalue you can do that by viewing the page source code or check if the $.post is sending the serialized data via the web browser's console
Upvotes: 0