dsw7547 2019-05-25 18:40
浏览 126
已采纳

为什么onsubmit事件处理程序不能在PHP解释器中工作

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.
" : ""
                    }
                </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.
" : ""
    }
  </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>

</div>
  • 写回答

1条回答 默认 最新

  • dtyrxmoj20617 2019-05-25 18:46
    关注

    You have to escape the newline twice or your HTML output will not look like intended:

    return (field == "") ? "No Forename was entered.\
    " : ""
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?