将确认对话框添加到PHP代码无响应

时间:2015-08-12 10:12:54

标签: javascript php html

我在单独的javascript文件中有一个用于表单提交的确认对话框。该文件的链接包含在php文档中。但确认框没有响应。代码如下。

javascript代码。

function getConfirm() {
  var retVal = confirm("Do you want to continue ?");
  if (retVal == true) {
    document.write("User wants to continue!");
    return true;
  } else {
    Document.write("User does not want to continue!");
    return false;
  }
}

下面是php代码

<?php require "config.php"; $requests=a rray(); $sql="SELECT activity_type FROM activity" ; foreach ($dbo->query($sql) as $row) { $requests[] = $row['activity_type']; } ?>

<!DOCTYPE html>

<html>

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="layout.css">
  <script language="javascript" type="text/javascript" src="/templates/myjavascript.js"></script>

  <title>Title of the document</title>
</head>
<div>

  <body>
    <?php include( '/templates/header.php'); ?>


    <section id="sidebar">

    </section>

    <section id="content">


      <form class="form" action="insert.php" method="post" name="access_form">

        <ul>

          <li>
            <h2>Please Fill The Form</h2>

          </li>



          <li>
            <label for="firstname">First Name</label>
            <input name="firstname" id="keyword" type="text" placeholder="type first name (required)" required />

          </li>

          <li>
            <label for="lastname">Last Name</label>
            <input name="lastname" id="lastname" type="text" placeholder="type second name (required)" required />
          </li>

          <li>

            <label for="request" id="officallabel">Type of Request</label>
            <input name="request" id="request" list="request1" />
            <datalist id="request1">
              <?php foreach ($requests as $request): ?>
              <option value="<?php echo $request; ?>" />
              <?php endforeach; ?>
            </datalist>
          </li>

          <li>
            <label for="purposebuttons" id="officallabel">Purpose</label>
            <div class="radio">
              <input type="radio" name="purposebuttons" id="official" value="Official" />
              <label id="official" for="official">Official</label>

              <input type="radio" name="purposebuttons" id="unofficial" checked="checked" value="Unofficial" />
              <label id="unofficial" for="unofficial">Unofficial</label>
            </div>
          </li>

          <li>
            <label for="personbuttons" id="officallabel">Person</label>
            <div class="radio">
              <input type="radio" name="personbuttons" id="staff" checked="checked" value="Staff" />
              <label id="staff" for="staff">Staff</label>

              <input type="radio" name="personbuttons" id="consultant" value="Consultant" />
              <label id="consultant" for="consultant">Consultant</label>
            </div>
          </li>



          <li>
            <label for="description">Description</label>
            <textarea name="description" id="description" placeholder="type description (required)" required></textarea>
          </li>

          <li>
            <label for="date-time">Access Date And Time</label>
            <input name="date-time" type="datetime-local" id="date-time" />
          </li>

          <p>
            <input type="reset" class="reset" />
            <input type="submit" class="submit" onclick="getConfirm();" />
          </p>

    </section>


    </ul>
    </form>

    <aside></aside>
    <span id="allowance"></span>

    <?php include( '/templates/footer.php'); ?>
  </body>
</div>

</html>

请协助代码。

1 个答案:

答案 0 :(得分:0)

在表单标签上使用onsubmit而不是onclick on submit按钮来触发提交事件。

<!DOCTYPE html>

<html>

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="layout.css">
  <script language="javascript" type="text/javascript">
  function getConfirm() {
  var retVal = confirm("Do you want to continue ?");
  if (retVal == true) {
    document.write("User wants to continue!");
    return true;
  } else {
    Document.write("User does not want to continue!");
    return false;
  }
}
  </script>

  <title>Title of the document</title>
</head>
<div>

  <body>
    <?php include( '/templates/header.php'); ?>


    <section id="sidebar">

    </section>

    <section id="content">


      <form class="form" action="insert.php" method="post" name="access_form" onSubmit="return getConfirm();">

        <ul>

          <li>
            <h2>Please Fill The Form</h2>

          </li>



          <li>
            <label for="firstname">First Name</label>
            <input name="firstname" id="keyword" type="text" placeholder="type first name (required)" required />

          </li>

          <li>
            <label for="lastname">Last Name</label>
            <input name="lastname" id="lastname" type="text" placeholder="type second name (required)" required />
          </li>

          <li>

            <label for="request" id="officallabel">Type of Request</label>
            <input name="request" id="request" list="request1" />
            <datalist id="request1">
              <?php foreach ($requests as $request): ?>
              <option value="<?php echo $request; ?>" />
              <?php endforeach; ?>
            </datalist>
          </li>

          <li>
            <label for="purposebuttons" id="officallabel">Purpose</label>
            <div class="radio">
              <input type="radio" name="purposebuttons" id="official" value="Official" />
              <label id="official" for="official">Official</label>

              <input type="radio" name="purposebuttons" id="unofficial" checked="checked" value="Unofficial" />
              <label id="unofficial" for="unofficial">Unofficial</label>
            </div>
          </li>

          <li>
            <label for="personbuttons" id="officallabel">Person</label>
            <div class="radio">
              <input type="radio" name="personbuttons" id="staff" checked="checked" value="Staff" />
              <label id="staff" for="staff">Staff</label>

              <input type="radio" name="personbuttons" id="consultant" value="Consultant" />
              <label id="consultant" for="consultant">Consultant</label>
            </div>
          </li>



          <li>
            <label for="description">Description</label>
            <textarea name="description" id="description" placeholder="type description (required)" required></textarea>
          </li>

          <li>
            <label for="date-time">Access Date And Time</label>
            <input name="date-time" type="datetime-local" id="date-time" />
          </li>

          <p>
            <input type="reset" class="reset" />
            <input type="submit" class="submit" />
          </p>

    </section>


    </ul>
    </form>

    <aside></aside>
    <span id="allowance"></span>

  </body>
</div>

</html>