在另一个文件中携带$ id变量

时间:2015-02-06 06:57:48

标签: php mysql ajax

我最近发现了这个教程http://www.infotuts.com/create-ajax-based-search-system-php-jquery/,并注意到所有结果都显示在search.php中,而search.php是通过ajax调用并显示在index.php中。每当我尝试将它集成到我的网站时,我的index.php包含一个$ id的变量,该变量显示在这个url中:( index.php?id = 1)我的问题是,我似乎无法得到它知道我的$ id是什么。我无法将search.php中的查询更改为我喜欢的结果。

$query = mysql_query("select * from userdetail where name like '{$term}%'", $connection);

这是我上面的原始查询,但是我希望它依赖于页面的ID,所以......:

$query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);

唯一的问题是,search.php不知道我的$ id变量是什么,即使我从index.php加载它也不起作用。有人可以帮忙吗?

完整的代码可以在教程中看到,但我可以在这里向你们展示。 的search.php

<?php
$connection = mysql_connect('localhost', 'root', '12345');
$db = mysql_select_db('userdata', $connection);
$term = strip_tags(substr($_POST['searchit'],0, 100));
$term = mysql_escape_string($term); // Attack Prevention
if($term=="")
echo "Enter Something to search";
else{
$query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);
$string = '';

if (mysql_num_rows($query)){
while($row = mysql_fetch_assoc($query)){
$string .= "<b>".$row['name']."</b> - ";
$string .= $row['email']."</a>";
$string .= "<br/>\n";
}

}else{
$string = "No matches found!";
}

echo $string;
}
?>

这是index.php

<html>
<head>
<title>Ajax Based Search- InfoTuts</title>
<?php
$id = ((int)$_GET["id"]);
 ?>
<style type="text/css" >
#main {
padding: 10px;
margin: 100px;
margin-left: 300px;
color: Green;
border: 1px dotted;
width: 520px;
}
#display_results {
color: red;
background: #CCCCFF;
}
</style>
<script type="text/javascript "src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<script type='text/javascript'>
$(document).ready(function(){
$("#search_results").slideUp();
$("#button_find").click(function(event){
event.preventDefault();
search_ajax_way();
});
$("#search_query").keyup(function(event){
event.preventDefault();
search_ajax_way();
});

});

function search_ajax_way(){
$("#search_results").show();
var search_this=$("#search_query").val();
$.post("search.php", {searchit : search_this}, function(data){
$("#display_results").html(data);

})
}
</script>
</head>

<body>
<div id="main">
<h1>Ajax Based Search System- InfoTuts</h1>
<form id="searchform" method="post">

<label>Enter</label>
<input type="text" name="search_query" id="search_query" placeholder="What You Are Looking For?" size="50"/>
<input type="submit" value="Search" id="button_find" />

</form>
<div id="display_results"></div>
</div>
</body>
</html> 

2 个答案:

答案 0 :(得分:1)

试试这个

在search.php文件中添加以下代码以接受index.php文件中的id

<code>
    $term = mysql_escape_string($term); // Attack Prevention
    $id = $_POST['id'];
    if(!empty($id)) {
        $query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);
    } else {
       $query = mysql_query("select * from userdetail where name like '{$term}%'", $connection);
    }
    if($term=="")
        echo "Enter Something to search";
</code>

在index.php文件中发送$ id变量值和ajax请求。

<code>
    function search_ajax_way(){
        $("#search_results").show();
        var search_this=$("#search_query").val();
        var id_val=$("#id").val();
        $.post("search.php", {searchit : search_this, id : id_val},   function(data){ $("#display_results").html(data);}
</code>

在html中为id添加输入隐藏字段

<code>
    <form id="searchform" method="post">
        <label>Enter</label>
        <input type="text" name="search_query" id="search_query" placeholder="What You Are Looking For?" size="50"/>
        <input type='hidden' value='<?php echo $id; ?>' id='id'/>
        <input type="submit" value="Search" id="button_find" />
    </form>
</code>

答案 1 :(得分:0)

in

请在表单中提及 action ='search.php',然后重定向到search.php ...然后您将获得$ id ...