PHP中遇到AJAX的问题
php ajax php ajax
<?phpheader("Content-Type:html/text;Charset=utf-8");$name = $_POST['name'];$email = $_POST['email'];$comment = $_POST['comment'];$fh = @fopen("14-6.txt","ab");fwrite($fh,"姓名:".$name,strlen($name));fwrite($fh,"email:".$email,strlen($email));fwrite($fh,"评论:".$comment,strlen($comment));fclose($fh);echo 1;?>
function $(id){ return document.getElementById(id);}function addcomment(){ var url = "14-6.php"; var status = document.getElementById("divmsg"); status.value="正在提交》》》"; var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value; xmlhttp_request.onreadystatechange = function(){ if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息 alert (xmlhttp_request.responseText); if(xmlhttp_request.responseText == "1"){ status.value = "发表成功!!"; $("name").value=""; $("email").value=""; $("comment").value=""; }else{ status.value = "发表失败!请重新发表!"; } } } xmlhttp_request.open("POST",url,true); xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息 xmlhttp_request.send(param);}
不知道为什么,这个responseText返回的是一大面的html代码.
回复讨论(解决方案)
上面就是alert(xmlhttp_request.responseText)返回的东西
你请求的是当前页面吗
那不是错误信息吗?
14-6.php 第 4 行的 $_POST['email'] 没有定义
var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value;
应写作
var param = "name="+$("name").value+" &email="+$("email").value+" &comment="+$("comment").value;
我的是从一个html文件转到php文件的。
html代码在下面。我的一共才两个文件,一个html文件,一个php文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Insert title here</title><script type="text/javascript">var xmlhttp_request = false; //定义变量try{ if(window.ActiveXObject){ //判断浏览器是否是ActiveXObject对象 for(var i=5;i;i--){ //根据IE浏览器版本使用不同方法创建 try{ if(i==2){ xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP"); }else{ xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP."+i+".0"); xmlhttp_request.setRequestHeader("Content-Type","text/xml"); xmlhttp_request.setRequestHeader("Charset","utf-8"); } break; }catch(e){ xmlhttp_request = false; } } }else if(window.XMLHttpRequest){ //判断浏览器是否支持XMLHttpRequest对象 xmlhttp_request = new XMLHttpRequest(); if(xmlhttp_request.overrideMimeType){ xmlhttp_request.overrideMimeType('text/xml'); } }}catch(e){ xmlhttp_request = false;}function $(id){ return document.getElementById(id);}function addcomment(){ var url = "14-6.php"; var status = document.getElementById("divmsg"); status.value="正在提交...."; var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value; xmlhttp_request.onreadystatechange = function(){ if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息 alert(xmlhttp_request.responseText); if(xmlhttp_request.responseText == "1"){ status.value = "发表成功!!"; $("name").value=""; $("email").value=""; $("comment").value=""; }else{ status.value = "发表失败!请重新发表!"; } } } xmlhttp_request.open("POST",url,true); xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息 xmlhttp_request.send(param);}</script></head><body><form name="form" id="form" method="post" action=""> <p> </p> <p> </p> <p> </p> <p> </p> <table width="400" height="168" border="0" align="center" cellpadding="2" cellspacing="0"> <caption>发表评论</caption> <tr> <td >用户名:</td> <td align="center"> <lable> <input type="text" name="name" id="name" size="30" maxlength="20"/> </lable> </td> </tr> <tr> <td >email:</td> <td align="center"> <lable> <input type="text" name="email" id="email" size="30" maxlength="20"> </lable> </td> </tr> <tr> <td>评论:</td> <td align="center"> <textarea name="comment" id="comment" rows="6" cols="30"></textarea> </td> </tr> <tr> <td colspan="2" align="center"> <lable> <input type="button" name="submit" value="发表" id="submit" onclick="addcomment();"/> </lable> <lable> <input id="divmsg" name="divmsg" value="" readonly /> </lable> </td> </tr> </table> </form></body></html>
问题已解决,就是版主说的错误。提交时三个不同的变量之间用&相隔,因为刚刚接触AJAX,所以对POST提交不是很了解。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
