我正在尝试创建一个表单,用户从下拉列表中选择一个选项,结果显示在单独的php页面中。但是,我遇到了从表单中获取信息的问题,并且不确定php是否正在选择值。我使用jQuery来序列化信息(只有一个变量),并尝试使用var_dump
和print_r
来查看是否存储了某些内容,但没有返回任何内容。
请帮忙吗?
形式:
<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>
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 );
});
}
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());
}
?>
关于如何克服这个问题的任何想法?提前谢谢!
答案 0 :(得分:1)
更改
$course = $_GET['course'];
到
$course = $_POST['course'];
因为您使用post方法提交数据。
<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>
您可以将表单标记包装在<div class="modal content"></div>
中。
不需要使用js。
答案 1 :(得分:0)
检查php是否将课程代码输出到optionvalue,您可以通过查看页面源代码或检查$ .post是否通过Web浏览器的控制台发送序列化数据