[英]why onsubmit event handler is not working in php interpreter
I am reading through Learning PHP, MySQL & Javascript 4th edition and in chapter 16 I encountered a problem. 我正在阅读学习PHP,MySQL和Javascript第4版,在第16章中遇到了一个问题。
Onsubmit event handler is not working in <?php ... ?>
(PHP interpreter), while it is working just fine without it. Onsubmit事件处理程序无法在
<?php ... ?>
(PHP解释器)中运行,而没有它,则可以正常工作。 am I missing something? 我错过了什么吗?
I removed some parts of the code to just show the problem 我删除了部分代码只是为了显示问题
<?php
echo <<<_END
<!DOCTYPE html>
<html>
<head>
<title>An Example Form</title>
<style>
.signup {
border:1px solid #999999;
font: normal 14px helvetica;
color: #444444;
}
</style>
<script>
function validate(form) {
fail = validateForename(form.forename.value)
if(fail == "") return true
else { alert(fail); return false }
}
function validateForename(field) {
return (field == "") ? "No Forename was entered.\n" : ""
}
</script>
</head>
<body>
<table border="0" cellpadding="2" cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">Signup Form</th>
<form method="post" onsubmit="return validate(this)">
<tr><td>Forename</td>
<td><input type="text" maxlength="32" name="forename"></td>
</tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Signup"></td>
</tr>
</form>
</table>
</body>
</html>
_END;
?>
with this code in use, javascript alert function is not showing any alert even if I submit an empty form. 使用此代码后,即使我提交了一个空白表格,javascript警报功能也不会显示任何警报。
Here is just the HTML/CSS/JS: 这只是HTML / CSS / JS:
<!DOCTYPE html> <html> <head> <title>An Example Form</title> <style> .signup { border: 1px solid #999999; font: normal 14px helvetica; color: #444444; } </style> <script> function validate(form) { fail = validateForename(form.forename.value) if (fail == "") return true else { alert(fail); return false } } function validateForename(field) { return (field == "") ? "No Forename was entered.\\n" : "" } </script> </head> <body> <table border="0" cellpadding="2" cellspacing="5" bgcolor="#eeeeee"> <th colspan="2" align="center">Signup Form</th> <form method="post" onsubmit="return validate(this)"> <tr> <td>Forename</td> <td><input type="text" maxlength="32" name="forename"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Signup"></td> </tr> </form> </table> </body> </html>
您必须两次对换行符\\n
进行转义,否则HTML输出将不符合预期:
return (field == "") ? "No Forename was entered.\\n" : ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.