search
HomeBackend DevelopmentPHP Tutorial 使用onsubmit事件触发ajax时返回值如何返回

使用onsubmit事件触发ajax时返回值怎么返回
这个是表单

<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br />
<html xmlns="http://www.w3.org/1999/xhtml"><br />
<head><br />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br />
<title>无标题文档</title><br />
<br />
<br />
</head><br />
<br />
<body><br />
<br />
<form action="abc.php" method="post" name="form" onSubmit="return check_form()"/><br />
用户名<input type="text" name="username" id="username" /><br />
验证码<input type="text" name="identifying_code" id="identifying_code"/><br />
<input type="submit" name="login" value="登录"><br />
</form><br />
<div id="tishi"></div><br />
<script language="javascript" src="js/xmlhttprequest.js"></script><br />
<script language="javascript" src="js/js2.js"></script><br />
</body><br />
</html><br />
<br />

这个是xmlhttprequest.js页面
<br />
var xmlhttp = false;<br />
if (window.XMLHttpRequest) { 									//Mozilla、Safari等浏览器<br />
	xmlhttp=new XMLHttpRequest();<br />
} <br />
else if (window.ActiveXObject) { 								//IE浏览器<br />
	try {<br />
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");<br />
	} catch (e) {<br />
		try {<br />
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");<br />
	   } catch (e) {}<br />
	}<br />
}<br />


这个是js2.js页面里面有我标记的路线可以看到整个执行的过程。
<br />
var flag;<br />
function check_form(){<br />
		<br />
		    var username=document.getElementById('username').value;<br />
			var identifying_code=document.getElementById('identifying_code').value;<br />
			url='login_chk.php?username='+username+'&identifying_code='+identifying_code;<br />
			alert('这里第1步');<br />
			xmlhttp.open('get',url,true);<br />
			alert('这里第2步');<br />
			xmlhttp.onreadystatechange = function(){<br />
				alert('这里第5步');<br />
				if(xmlhttp.readyState == 4){<br />
					alert('这里第6步');<br />
					if(xmlhttp.status == 200){<br />
						alert('这里第7步');<br />
						msg = xmlhttp.responseText;<br />
						alert('这里第8步');<br />
<br />
						if(msg==1){<br />
							document.getElementById("tishi").innerHTML="<font color='#FF0000'>输入正确正在跳转</font>";	<br />
							flag=true;<br />
<br />
						}<br />
						else{<br />
							document.getElementById("tishi").innerHTML="<font color='#FF0000'>用户名或者密码错误</font>";<br />
							flag=false;<br />
						}<br />
						alert('这里第9步');<br />
						<br />
				    }<br />
			    }<br />
				<br />
				//alert(flag);<br />
			}<br />
			xmlhttp.send(null);<br />
			alert('这里第3步');<br />
		<br />
		<br />
		if(flag==true){<br />
		alert('这里是true');<br />
		alert(flag);<br />
		return true;<br />
		}<br />
		else {<br />
		alert('这里第4步');<br />
		alert(flag);<br />
		return false;<br />
		}<br />
<br />
}	<br />

这个是login_chk.php页面
<br />
<?php<br />
if(strcmp($_GET['username'],'abcd')==0&&strcmp($_GET['identifying_code'],'abcd')==0){<br />
$msg=1;<br />
<br />
}<br />
else{<br />
	$msg=0;<br />
	}<br />
<br />
echo $msg;<br />
<br />
?><br />
<br />



最重要的是要取得check_form的返回值,才能决定表单是否能提交。求高手更改代码取到check_form的返回值
------解决方案--------------------
xmlhttp.open('get',url,true);
这是异步通讯,所以你要定义接受返回数据的回调函数
xmlhttp.onreadystatechange = function(){

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment