0

从 index.html 使用表单我收到 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

索引.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>

管理器.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);
    ?>

函数.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();
    }
    ?>

函数.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 或其他东西。只是带有 javascript ajax 的 PHP - 没有插件、模组或类似的东西。

4

1 回答 1

1

1.问题是您$_POST['genre']在查询中使用,但它应该使用$genre您传递的参数值。

2.$genre = $_POST['search'];getGoodsByGenre功能中移除

3.你在其他功能上也有同样的问题

于 2012-11-23T18:11:26.640 回答