从index.html使用表单i接收null到function.php。
index.html - 带输入的表单。
manager.php - 从表单中获取值并将其发送到function.php中的正确函数。
function.js - 获取数据的ajax。
我的功能在
停止 while($row = $result->fetchRow())
{
$title = $row[0];
echo '<tr><td>' . $title .'</td>';
}
发现零行没有显示任何内容。
sql select - 工作 数据库的结构 - https://www.dropbox.com/s/6cnnxb933yl11og/1.png
的index.html
<script src="functions.js"> </script>
<style>
table, table td { border: 1px solid #666; }
</style>
</head>
<body>
<form name="films" action="manager.php" method="POST">
<select name="type">
<option>Genre</option>
<option>Actor</option>
<option>Date</option>
</select>
<input type="text" name="search">
<input type="text" name="additional">
<input type="submit" value="Submite" onclick="getGoodsBy()">
<input type="reset">
</form>
<div id="result"></div>
manager.php
<?php
require_once 'functions.php';
$type = $_POST['type'];
$search = $_POST['search'];
$additional = $_POST['additional'];
if ($type == 'Genre')
getGoodsByGenre($search);
if ($type == 'Actor')
getGoodsByActor($search);
if ($type == 'Date')
getGoodsByDate($search, $additional);
?>
的functions.php
<?php
require_once 'DB.php';
require_once 'manager.php';
$user = 'root';
$pass = '';
$host = 'localhost';
$db_name = 'lab2';
$dsn = "mysql://$user:$pass@$host/$db_name";
function getGoodsByGenre($genre)
{
global $user;
global $pass;
global $host;
global $db_name;
global $dsn;
$db = DB::connect($dsn, true);
if (DB::isError($db)) {
die ($db->getMessage());
}
$sql = "SELECT a.name as name
FROM film as a
INNER JOIN film_genre as b ON b.FID_Film=a.ID_FILM
WHERE b.FID_Genre='".$_POST['genre']."'";
$result = $db->query($sql);
echo '<table>';
while($row = $result->fetchRow())
{
$title = $row[0];
echo '<tr><td>' . $title .'</td>';
}
echo '</table>';
$db->disconnect();
}
function getGoodsByActor($actor)
{
global $user;
global $pass;
global $host;
global $db_name;
global $dsn;
$db = DB::connect($dsn, true);
if (DB::isError($db)) {
die ($db->getMessage());
}
$sql = "SELECT a.name as name
FROM film as a
INNER JOIN film_actor as b ON b.FID_FILM=a.ID_FILM
WHERE b.FID_Actor =".$_POST['actor']."";
$result = $db->query($sql);
echo '<table>';
while($row = $result->fetchRow())
{
$name = $row[0];
echo '<tr><td>' . $name .'<td>' ;
}
echo '</table>';
$db->disconnect();
}
function getGoodsDate($startDate, $endDate)
{
global $user;
global $pass;
global $host;
global $db_name;
global $dsn;
$db = DB::connect($dsn, true);
if (DB::isError($db)) {
die ($db->getMessage());
}
$sql = "SELECT a.name as name
FROM film as a
WHERE a.date >= ".$_POST['startDate']." and a.date <=".$_POST['endDate']."";
$result = $db->query($sql);
echo '<table>';
while($row = $result->fetchRow())
{
$date = $row[0];
echo '<tr><td>' . $date . '<td>';
}
echo '</table>';
$db->disconnect();
}
?>
functions.js
function createHttpRequest()
{
var httpRequest;
if (window.XMLHttpRequest)
{
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType)
httpRequest.overrideMimeType('text/xml');
}
else if (window.ActiveXObject)
{
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest)
{
alert('Cannot create XMLHTTP instance');
return null;
}
return httpRequest;
}
function showResult(httpRequest)
{
try
{
if (httpRequest.readyState == 4)
{
if (httpRequest.status == 200)
{
var result = document.getElementById('result');
result.innerHTML = httpRequest.responseText;
}
else
{
alert('There was a problem with the request.');
}
}
}
catch( e )
{
alert('Caught exception: ' + e.description);
}
}
function getGoodsBy()
{
var type = document.forms['films'].type.value;
var search = document.forms['films'].search.value;
var additional = document.forms['films'].additional.value;
var params = encodeURI('type=' + type + '&search=' + search + '&additional=' + additional);
var httpRequest = createHttpRequest();
httpRequest.open('POST', 'manager.php', true);
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpRequest.onreadystatechange = function() { showResult(httpRequest); };
httpRequest.send(params);
}
PS:我不能使用jquery或其他东西。只需PHP使用javascript ajax - 没有插件,mod或类似的东西。
答案 0 :(得分:1)
1。
问题是您在查询中使用$_POST['genre']
但它应该使用$genre
,这是您传递的参数值。
2。
从$genre = $_POST['search'];
功能
getGoodsByGenre
3.您在其他功能方面与上述问题相同