使用jquery访问json数据(由php返回)

时间:2013-12-11 15:59:21

标签: post json

问题。我的html:

<html>
<head>
<title>
 Runners
</title>
</head>
<body>
 <button id="printrunners"> Print Runners</button>
 <div id="printrunnershere"></div>
<script src="printrunners1.js"></script>
</body>
</html>

javascript在这里:

$(document).ready(function()
  {
      $("#printrunners").click(function()
         {


 $.getJSON("printrunners.php?action=getrunners", 

function(json)  //also tried without "?action=getrunner" line
               {
               $.each(json.runners,function()
                  {
                   var info= 'Name:  '+this['fname']+' '+ this['lname']+'   Gender: '+this['gender']+'     Finish Time: '+this['finish_time'];
                   $("#printrunnershere").append(info);
                  });

           });
       });

});

printrunners.php这里:

<?php
if($_GET)  //also tried without it with respect to "?action=getrunner"
 {
   if($_GET['action']=="getrunners") // also tried without it w.r.t "?action=getrunner"
     {
        mysql_connect("localhost","runner_db_user","runner_db_user_password");
        mysql_select_db("runner_info");
        $result=mysql_query("select  * from runner");
        $runners=array();
        while($row=mysql_fetch_array($result,MYSQL_ASSOC))
         {
            array_push($runners, array('fname' => $row['fname'], 'lname' => $row['lname'], 'gender' => $row['gender'], 'finish_time' => $row['finish_time']));
         }
      echo json_encode(array("runners" =>$runners));
     }
 }

?>

Q2。我也使用post()在它插入的同一个数据库中插入一行,但无法访问php程序返回的json数据:

javascript在这里:

$(document).ready(function()
  {
      $('#add').click(function()
    {

      var data=$("#form1 :input").serializeArray();

      $.post($("#form1").attr('action'),data,function(json){

            if(json.status=='success')
             {

               alert (json.message);
             }
             if(json.status=='fail')
              {
                alert(json.message);
               }
        },"json");
     });


    $("#form1").submit(function()
       {
          return false;
       });

   });

这是php:

<?php


   $fname=htmlspecialchars($_POST['fname']);
   $lname=htmlspecialchars($_POST["lname"]);
   $gender=htmlspecialchars($_POST["gender"]);
   $finish_time=htmlspecialchars($_POST["finish_time"]);



  $query="insert into runners (fname,lname,gender,finish_time) values('$fname','$lname','$gender','$finish_time')" ;

  $result=db_connection($query);

if($result)
   {
    $msg="Runner  " .fname."".lname."added successfully";
success($msg);
   }
  else
   {

     fail("insert failed");
   }
exit;




function fail($message) 
  {
    die(json_encode(array('status' => 'fail', 'message' => $message)));
  } 
function success($message)
  { 
    die(json_encode(array('status' => 'success', 'message' => $message)));
  }

function db_connection($query1)
 {
  mysql_connect("localhost","runner_db_user","runner_db_password")
    OR die(fail('Could not connect to database.'));

   mysql_select_db("runner_info");

   return  mysql_query($query1);

 }

?>

ITS FRONT END(html页面):

<!DOCTYPE html>
<html>
<head>
<title>
 add runner
</title>
<script src="jquery-2.0.3.js" type="text/javascript"></script>
<script src="jsonuse.js" type="text/javascript"></script>

</head>
<body>
 <form id="addrunnerform" name="addrunnerform" method="post" action="addrunner.php">
   First Name: <input type="text" name="fname" id="name"></br>
   Last Name: <input type="text" name="lname" id="lname"></br>
   Gender: <input type="radio" name="gender" id="male" value="m" checked>Male
           <input type="radio" name="gender" id="female" value="f">Female</br>
   Finish Time: <input type="text" id="finish_time" name="finish_time"></br>
 <button type="submit" id="add" name="add">ADD</button>

  </form>

</body>
</html>

0 个答案:

没有答案