1

我正在创建一个 MySQL 查询,当用户从更多下拉列表中选择选项时将执行该查询。

我想要的是,在选择下拉列表选项时,应在同一页面上使用 ajax/javascript 自动执行与该选项相关的查询。因为我在同一页面上有 html 和 php 代码。

早些时候我使用下拉列表的表单提交选项,但由于下拉选项的数量超过五个来过滤结果,因此查询变得难以实现。这就是为什么我想单独细化每个下拉列表的结果。

任何帮助表示赞赏。提前致谢!

我的下拉列表 HTML 代码是:

<p>
<label for="experience">Experience :</label>
<select id="experience" name="experience">
    <option value="" selected="selected">All</option>
    <option value="Fresher">Fresher</option>
    <option value="Experienced">Experienced</option>
</select>
</p>

用于执行相关查询的 PHP 代码是:

<?php
if (isset($_GET['exp'])) {

    switch ($_GET['exp']) {

        case 'Experienced':
        $query = "SELECT job_seekers.ID, job_seekers.Name, job_seekers.Skills, job_seekers.Experience FROM job_seekers where job_seekers.Experience!='Fresher'";
        break;

    case 'Fresher':
        $query = "SELECT job_seekers.ID, job_seekers.Name, job_seekers.Skills, job_seekers.Experience FROM job_seekers where job_seekers.Experience='Fresher'";
        break;

    default:
        $query = "SELECT job_seekers.ID, job_seekers.Name, job_seekers.Skills, job_seekers.Experience FROM job_seekers";

    }
}
$result = mysql_query($query) or die(mysql_error());

echo "<ul class=\"candidates\">";

while($row = mysql_fetch_row($result))
{
    echo "<li>";
    echo "<p> <b>ID:</b> <u>$row[0]</u> </p>";
    echo "<p> <b>Name :</b> $row[1] </p>";
    echo "<p> <b>Key Skills:</b> $row[2] </p>";
        echo "<p> <b>Experience:</b> $row[3] </p>";
    echo "</li>";
}

    echo "</ul>";               

?>
4

3 回答 3

4

当你想通过 AJAX 调用一个 php 脚本时,你应该使用 Jquery 提供的 $.ajax

http://api.jquery.com

所以你可以像这样使用它:

$.ajax({
    url: 'ajax.php',
    data: {
        //put parameters here such as which dropdown you are using
    },
    success: function(response) {
        //javascript and jquery code to edit your lists goes in here.
        //use response to refer to what was echoed in your php script
    }
});

这样,您将拥有动态下拉列表和您想要的精致结果

于 2012-08-31T07:29:36.430 回答
0

您不能在同一页面上重新执行 PHP 部分。而是使用 Ajax 请求来执行操作。

于 2012-08-31T07:40:43.667 回答
0
<?php
if (isset($_GET['experience'])) {

    echo $_GET['experience'];
        /* do mysql operations and echo the result here */
    exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
function myFunction(value)
{
    if(value!="All")
    {
        $.ajax(
        {
            type: "GET",
            url: '<?php echo $_SERVER['PHP_SELF']; ?>',
            data: { experience: value},
            success: function(data) {
                $('#resultDiv').html(data);
        }
    });
    }
    else
    {
        $('#resultDiv').html("Please select a value.");
    }
}
</script>
</head>

<body>
<p>
<label for="experience">Experience :</label>
<select id="experience" name="experience" onChange="myFunction(this.value)">
    <option value="All" selected="selected">All</option>
    <option value="Fresher">Fresher</option>
    <option value="Experienced">Experienced</option>
</select>
</p>
<div id="resultDiv">
Please select a value.
</div>
</body>
</html>
于 2012-08-31T09:17:17.180 回答