Home > php教程 > php手册 > body text

实例详解JavaScript与php的交互

WBOY
Release: 2018-10-26 17:31:55
forward
1369 people have browsed it

这篇文章主要介绍了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>
Copy after login

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>  
Copy after login

【相关教程推荐】

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

2. php从入门到精通  

3.bootstrap教程  

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!