简体   繁体   English

用户在搜索框中插入空值时,我如何留在同一页面上

[英]how can I stay in the same page when user insert null value in search box

I have a Header, which I include it in every page, that contain search when the user enter value it will display the result but when he/she but null value in the text box it display all the result in the database how can I prevent that and stay in the same page where he/she is 我有一个页眉,它包含在每个页面中,当用户输入值时它将包含搜索,它将显示结果,但是当他/她在文本框中显示空值时,它将在数据库中显示所有结果。并留在他/她所在的同一页面

--- HTML--- -HTML--

 <form action="search.php" method="post">
<input type="search" placeholder=" search..."name="data" style=" margin:30px 0px 0px 0px; ">
<input type="submit" style="visibility:hidden" name="search"/>
</form>

--- php --- -PHP-

$search=$_POST['data'];

$data2 = mysql_query("select * from project,person where project.projectname  LIKE '%$search%' and person.personstatus=1 and project.status='submitted' and project.personid=person.personid") or die(mysql_error());

 if (mysql_num_rows($data2)){
 echo '<div style="width:1170px;min-height:100%; margin:20px 90px 0px 0px; 
 display:inline-block; background-color:#fff;">
 <h2 style="color:rgb(58,147,231);font-size:20px;"> Project </h2> ';
   while($info = mysql_fetch_array( $data2 ))
    {
    $projectid=$info['projectid'];
    $img=$info['imgProject'];
    $proName=$info['projectname'];
    $type=$info['projecttype'];
    $person=$info['personid'];
    $personname=$info['personname'];

    echo  '<a href="project.php?projectid='.$projectid.'"><div class="note" style=" background-color:#E6E6E6; display:inline-block;width:200px; height:250px; margin-left:30px;">';
    echo '<center><h4 style="color:rgb(58,147,231);">'.$proName.'</h4>';
    echo'<img src="'.$img.'" width="200" height="160"><br>';
    echo'<b>Project name : </b>'.$personname.'<br>';
    echo'<b>Project type : </b>'.$type.'<br>';

    echo' </div></a></center>';
    }
}
else {
$errors[]="NO result found";
}
 echo '</div>'; 

I hope you can help me solve this problem 希望您能帮我解决这个问题

Validate the form using javascript 使用javascript验证表单

    <form action="search.php" method="post">
    <input id="data" type="search" placeholder=" search..."name="data" style=" margin:30px 0px 0px 0px; ">
    <input type="submit" style="visibility:hidden" name="search" onclick="javascript:validate();"/>
</form>

<script>
    function validate() {
        var check = document.getElementById("data").value;
        if (check === null || check === '') {
            return false;
        } else {
            return true;
    }
</script>

Then check if is set in your PHP code since as kpp said javascript can be disabled on the browser, wrap your code in 然后检查是否在您的PHP代码中进行了设置,因为kpp表示可以在浏览器中禁用javascript,请将您的代码包装在

if(isset($_POST['data'])){
}
 <form action="search.php" method="post" onclick="javascript:validate();">
    <input id="data" type="search" placeholder=" search..."name="data" style=" margin:30px 0px 0px 0px; ">
    <input type="submit" style="visibility:hidden" name="search"/>
</form>
<script>
    function validate() {
        var check = document.getElementById("data").value;
        if (check == '')
            return false;
       else
            return true;
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM