简体   繁体   中英

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.

Onsubmit event handler is not working in <?php ... ?> (PHP interpreter), while it is working just fine without it. 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.

Here is just the 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" : ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM