首页 > 代码库 > jquery下php与ajax的互传数据自我总结

jquery下php与ajax的互传数据自我总结

研究了一整天的json数据与ajax的数据传输,现在进行一个小的自我总结,仅供参考

1.关于ajax的认识

     $.ajax(),是jquery下包装好的一个函数。http://www.w3school.com.cn/jquery/ajax_ajax.asp

    使用其跟php段进行通信。

<script type="text/javascript">
  //jquery直接进行ajax的包装调用
       $(function(){     
         
            $("#subbtn").click(                     
                    function(){
                       //单引号+value没有括号
                     var name=document.getElementById(‘name‘).value;                  
                        var email=document.getElementById(‘email‘).value;
                        var content=document.getElementById(‘content‘).value;                                                            
                        var params={"name":name,"email":email,"content":content};                             
                        var url="deal.php";
                        $.ajax({
                            type:"post",
                            url:url,
                            data:params,
                            dataType:"json",
                            success:function(json){
                                alert(json);
                                 var back="<ul>";
                                 for(var i=0;i<(json.length);i++)
                                 {
                                     for(var j=0;j<json[i].length;j++)
                                         back+="<li>"+json[i][j]+"</li>";
                                 }         
                                  back+="</ul>"
                                 $("#backdata").html(back);
                                 $("#backdata").css({color:"green"});
                            }
                        });
                    });
        }

                
       );

</script>

 

PHP段代码:

$name=$_POST[‘name‘];
$email=$_POST[‘email‘];
$content=$_POST[‘content‘];
$id=time();
//echo json_encode($_POST);
$conn=mysqli_connect(‘localhost‘, ‘root‘,‘‘, ‘guestbook‘);
mysqli_query($conn,"SET NAMES ‘utf8‘");
$sql="insert into liuyan values(‘$id‘,‘$name‘,‘$content‘,‘$email‘)";
if(mysqli_query($conn, $sql))
{   
  $ar=array();
  $one=array();
  $tow=array();
  $one[0]="test1";
  $one[1]="test2";  
  $tow[0]="#1";
  $tow[1]="#2";  
  $ar[0]=$one;
  $ar[1]=$tow;
  echo json_encode($ar);//按照json格式进行返回
}else
{
 echo ‘DFDFDFDFDFD‘;
 
}
 mysqli_close($conn);

 

 

json的数据格式参考:http://baike.baidu.com/link?url=DexXfOk-Dt2rCSYz3oe8FhYXGAsHH5Jh_MUm3YopvoRT80-le_iXQoIMtdOj5jWU4FLRVPRLB6Xuca9xzezebq

?
1
2
3
4
5
6
7
8
9
10
11
12
13
varpeople={"programmers":[{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
{"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
{"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
],
"authors":[
{"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"},
{"firstName":"Tad","lastName":"Williams","genre":"fantasy"},
{"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"}
],
"musicians":[
{"firstName":"Eric","lastName":"Clapton","instrument":"guitar"},
{"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"}
]}

jquery下php与ajax的互传数据自我总结