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

input value data is not passing using jquery/ajax to Php

本文关键字:ajax Php jquery 数据 输入      更新时间:2023-09-26

我正试图从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'];

请使用以下代码:

$(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);
        }
    });
});

});

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

你可以做的一件事是在表单外创建一个按钮和一个文本字段,并像这样声明其onClick属性:

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

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