输入值数据未使用jquery / ajax传递给Php

时间:2014-05-31 05:53:35

标签: javascript jquery ajax

我正在尝试从Mysql数据库获取结果,但我收到警告消息,因为它没有将 html字段值传递给jquery / ajax方法。我认为问题就在这一行data : SearchValue,。因此,在我的php页面中,它没有获得$search = $_POST['SearchValue'];值并显示警告消息。

有人能告诉我的代码有什么问题吗?谢谢。

Html页面:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#display").click(function() {                
     var SearchValue = $('#txt_name').val();

      $.ajax({    //create an ajax request to load_page.php
            type: "POST",
            url: "doSearch.php",             
            data : SearchValue,             
            dataType: "html",   //expect html to be returned                
            success: function(response){                    
                $("#responsecontainer").html(response); 
                //alert(response);
            }

        });
    });
});
</script>

<h3 align="center">Manage Student Details</h3>
    <table border="1" align="center">
       <tr>
           <td> <input type="text" name="search" id="txt_name" /> </td>
           <td> <input type="button" id="display" value="Display All Data" /> </td>
       </tr>
    </table>
<div id="responsecontainer" align="center">

Php页面:

$search = $_POST['SearchValue'];

2 个答案:

答案 0 :(得分:0)

您正在通过原生方法提交表单。这意味着如果您点击提交按钮,页面将重新加载(因为在您的情况下,您还没有包含表单操作)。

您可以做的一件事是在表单外部创建一个按钮和一个文本字段,并声明其onClick属性,如下所示:

<input type="text" id="search">
<button onclick="doSearch(document.getElementById('search').value)"> Search <button>

OR

<input type="text" id="search">
<button onclick="doSearch($("#search").val())"> Search <button>

答案 1 :(得分:0)

请使用以下代码:

$(document).ready(function() {
$("#display").click(function() {                
 var SearchValue = $('#txt_name').val();
 var str = $( "#form_id" ).serialize();
  $.ajax({    
        //create an ajax request to load_page.php
        type: "POST",
        url: "doSearch.php?searchValue="+SearchValue,             
        data : str,             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response); 
            //alert(response);
        }

    });
});

});

相关问题