开发者

Using jQuery in MySQL php

I am new to using Jquery using mysql and PHP.

I am using the following code to pull the data. But there is not data or error displayed.

JQUERY:

<html>
<head>
  <script>
    function doAjaxPost() {
      // get the form values
      var field_a = $("#field_a").val();

      $.ajax({
        type: "POST",
        url: "serverscript.php",
        data: "ID="+field_a,
        success: function(resp){
          $("#resposnse").html(resp);
        },
        error: function(e){
          alert('Error: ' + e);
        }
      });
    }
  </script>
</head>
<body>
  <select id="field_a">
    <option value="data_1">data_1</option>
    <option value="data_2">data_2</option>
  </select>
  <input type="button" value="Ajax Request" onClick="doAjaxPost()">
  <a href="#" onClick="doAjaxPost()">Here</a>
  <div id="resposnse">
  </div>
</body>

and now serverscript.php

<?php
  if(isset($_POST['ID'])) {
    $nm = $_POST['ID'];
    echo $nm;
    //insert your code here for the display.
    mysql_connect("localhost", "root", "pop") or die(mysql_error());
    mysql_select_db("JPro") or die(mysql_error());
    $result1 = mysql_query("select Name from results where ID = \"$nm\" ")
      开发者_JAVA技巧           or die(mysql_error());  

    // store the record of the "example" table into $row
    while($row1 = mysql_fetch_array( $result1 )) {
      $tc = $row1['Name'];
      echo $tc;
    }
  }
?>


Like Mike said, this also worked for me when I put it in the ready function and removed the button onclick. Open up your error console in Firefox and see if it is outputting anything

<?php
if(isset($_POST['ID'])){
  echo ($_POST['ID']);
  exit;
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <title> Heather Alexandra </title>
  <script type="text/javascript" src="../intranet/include/js/jQuery/jquery-1.4.1.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("#ajax").click(function(){
        $.ajax({
          type: "POST",
          url: "test.php",
          data: "ID="+$("#field_a").val(),
          success: function(resp){
            $("#resposnse").html(resp);
          },
          error: function(e){
            alert('Error: ' + e);
          }
        });
      });
    });
  </script>

  </head>

  <body>

  <div style="margin: 0 auto; width: 822px;">
  <select id="field_a">
    <option value="data_1">data_1</option>
    <option value="data_2">data_2</option>
  </select>
  <input id="ajax" type="button" value="Ajax Request">
  <div id="resposnse"></div>
  </div>
  </body>

</html>


I just did everything you did except changed the serverscript.php to:

 <?php
    if(isset($_POST['ID'])) {
    $nm = $_POST['ID'];
    echo $nm;

    }
?>

And the script worked fine. I think all you have to do is get back down to the basics and find out what is actually generating the error. Try using firebug for firefox to see if you are generating any script errors. If not try changing the serverscript.php to use $_GET and type the following address to see what you get: http://yourtestserver/serverscript.php?ID=data_1

Debugging can get tricky when you deal with ajax but if you break it down to it's individual pieces it should make it easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜