如何使用ajax发送文本字段值

时间:2015-03-27 05:15:12

标签: php mysql ajax

我试图在页面onload()时显示mysql表值。我已经使用ajax函数来调用php mysql页面。我想要做的是,我想从ajax传递textfield值。但是它dn' t显示任何结果。这是我尝试过的。

$id=$_GET['id'];
 <input type="hidden" value="<?php echo $id;?>" id="rid" />

<script>
function showAll() {

  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("bookings").innerHTML=xmlhttp.responseText;
    }
  }
  var resname= document.getElementById("rid").value();

  xmlhttp.open("GET","ajax_all.php?res_name="+resname,true);
  xmlhttp.send();
}
</script>

在ajax_all.php中

echo $id = $_REQUEST['res_name'];

3 个答案:

答案 0 :(得分:0)

请这样做,我希望它有助于获得回复

    if($_GET['id'])
    {
       $id=$_GET['id'];}
     else {
     $id = 'null';
     }

     <input type="hidden" value="<?php echo $id;?>" id="rid" />
     <button type="button" onclick="showAll()">Change Content</button>
    <script>
    function showAll() {

      if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      } else { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("rid").innerHTML=xmlhttp.responseText;
        }
      }
      var resname= document.getElementById("rid").value();

      xmlhttp.open("GET","ajax_all.php?res_name="+resname,true);
      xmlhttp.send();
    }
    </script>

答案 1 :(得分:0)

希望这个能帮到你。

<input type="hidden" value="123" id="rid" />

您的脚本功能。

 function showAll()
    {
        var  resname = $('#rid').val();
        var data_string = "res_name=" + resname;
        $.ajax({

            url: 'ajax_all.php',
            data: data_string,
            type: post
            success:  function(html){

                  $("#bookings").innerHTML= html
            }


        });
    }

希望你已经附上jquery.min.js

For Demo click here.

答案 2 :(得分:0)

这对我有用

booking.php

    <input type="hidden" value="<?php echo $id;?>" name="rid" />

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script>
        function showAll() {

        var resname = $("input[name='rid']").val();


            $.ajax({
                url: 'ajax_all.php',
                type: 'GET',
                data: {res_name:resname},
                success: function(data) {
                   document.getElementById('bookings').innerHTML =data;
                }
            });

        }
        </script>
 <body onload="showAll()" >   
    <div id="bookings"></div>
</body>

在ajax_all.php中

$id = $_REQUEST['res_name'];

感谢all.s尤其是@jigs Virani