Home  >  Article  >  php教程  >  实例详解JavaScript与php的交互

实例详解JavaScript与php的交互

WBOY
WBOYforward
2016-06-13 12:41:531370browse

这篇文章主要介绍了JavaScript与php的交互,有一定的参考价值,感兴趣的朋友可以看看, 希望对你有所帮助!

php

<span style="font-size: 14px;">//JSon格式头:header("Content-type:application/json")//php连接数据库$conn=mysql_connect("url","username","password");<br/>//php选择数据库mysql_select_db("dbname",$conn);//设置编码mysql_query("set names utf8",$conn);<br/>//从表中取出数据$rule=mysql_query("select * from tb");//创建结果变量$result=array();<br/>//输出数组while($row=myaql_fetch_array($rule)){<br/>    array_push($result,$row)<br/>}<br/>print_r($result)//输出jsonwhile($row=myaql_fetch_assoc($rule)){    $result[]=$row}echo json_encode($result)<br/></span>

print_r用于输出数组与对象,echo 和print用于输出字符串

ajax

<span style="font-size: 14px;">//创建request对象var request;if(window.XMLHttpRequest){<br/>    request=new XMLHttpRequest()<br/>}else{<br/>    request=new ActiveXObject(Miscrosoft.XMLHTTP)<br/>}//创建参数parmavar parma;//GET请求request.open("GET",url+"?"+parma,true);<br/>request.send();//POST请求request.open("POST",url,true);<br/>requset.setRequestHeader("Content-tyep","application/x-www-form-urlencoded");<br/>request.send(prama);//结果与回复request.onreadystatechange=function(){<br/>    if(request.readyStage=4&&request.status==200){<br/>        success()<br/>    }else{<br/>        fail()<br/>    }<br/>}</span>  

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3.bootstrap教程  

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete