带有标签的php简单搜索框

时间:2013-08-03 01:20:37

标签: php search tabs

我正在尝试在一组标签旁边创建一个简单的搜索框,搜索框仅用于搜索手机类型,我制作了此代码,除搜索框外,每件事情都有效。我给出了这个错误: 注意:未定义索引:搜索

<form action="" method="post" name="form" > 
<body>  
<script>
$(function() {
$( "#xx" ).tabs();
});
</script>


<div id="xx" >
<ul>
<li><a href="#all">all phones</a></li>
<li><a href="#small">small</a></li>
<li><a href="#large">large</a></li>
<li><a href="#search">search:<input type="text" name="search">
</a></li>
</ul>   

<?php
include 'db.php'; 
?>

<div id="all">
<?php 
$res = mysql_query("SELECT * FROM table1");
while($row = mysql_fetch_array($res))
{         
echo 'phone name'.$row[2];
echo 'phone type'.$row[3];
echo 'phone sise'.$row[4];
}
?> 
</div>


<div id="small">   
<?php 
$res2 = mysql_query("SELECT * FROM table1 WHERE phonesize = 'small' ");
while($row = mysql_fetch_array($res2))
{         
echo 'phone name'.$row[2];
echo 'phone type'.$row[3];
echo 'phone sise'.$row[4];
}
?> 
</div>

<div id="large">   
<?php 
$res3 = mysql_query("SELECT * FROM table1 WHERE phonesize = 'large' ");
while($row = mysql_fetch_array($res3))
{         
echo 'phone name'.$row[2];
echo 'phone type'.$row[3];
echo 'phone sise'.$row[4];
}
?> 
</div>


<div id="large">   

 <?php     
$search = $_POST['search'];   
$res4 = mysql_query("SELECT * FROM table1 WHERE phonetype = '$search'");
while($row = mysql_fetch_array($res4))
{         
echo 'phone name'.$row[2];
echo 'phone type'.$row[3];
echo 'phone sise'.$row[4];
}
?> 
</div> 


</form>

2 个答案:

答案 0 :(得分:1)

第一次加载表单时,$ _POST ['search']不存在,这就是导致通知的原因。直到您实际发布该变量存在的形式。在执行用户提交表单后要执行的代码之前,应检查该变量是否存在,如:

if( isset( $_POST )) {
    // The form was posted.
    // Write your code here to execute once the user posts the form.
]

答案 1 :(得分:0)

好......... 第一次加载表单时,$ _POST ['search']不存在,这就是导致通知的原因。直到您实际发布该变量存在的形式。在执行用户提交表单后要执行的代码之前,应检查该变量是否存在,如: