为了账号安全,请及时绑定邮箱和手机立即绑定

如果条件为真,检查验证脚本不返回

如果条件为真,检查验证脚本不返回

PHP
慕妹3146593 2023-09-08 14:20:45
我有一个包含复选框的表单:<form action="tienda3.php">    <div class="form-group">        <label for="email">Email address:</label>        <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email to confirm the order">    </div>    <div class="form-group">        <div class="checkbox">            <label><input type="checkbox" id="TOS" value="This"> I certify that I am of legal age and I have read and agree to the                 <a href="../terms.php" target="_blank">Terms of use</a> and                <a href="../privacy.php" target="_blank"> Privacy Policy </a>of Sdocks LLC</label>            </div>        </div>        <button type="submit" onclick="validate()" class="btn btn-primary">Submit</button>    </form>我需要验证用户是否选中复选框以将表单发布到 tienda3.php。我使用此脚本来验证用户是否已选中该复选框:<script type=text/javascript>    function validate(){        if (document.getElementById('TOS').checked){            alert("checked") ;        }else{            alert("You didn't check it! Let me check it for you.");            return true;        }    }</script>如果选中该复选框,则表单将发布到 tienda3.php,否则必须显示警报,通知用户必须选中该复选框才能继续该过程。就我而言,表单始终发布到 tienda3.php。该脚本检测复选框是否被选中,但在这两种情况下,表单始终打开文件 tienda3.php
查看完整描述

2 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

我建议您进行以下更改:


/* replace this:

 * <form action="tienda3.php"> */


 <form action="tienda3.php" onsubmit="return validate(event)" >


 /* replace this:

  * <button type="submit" onclick="validate()" class="btn btn-primary">Submit</button> */

<button type="submit" class="btn btn-primary">Submit</button>


/* and replace the validate() function with: */

function validate(event){

  if (document.getElementById('TOS').checked){

    alert("checked") ;

    return true;

  } else {

    event.preventDefault();

    alert("You didn't check it! Let me check it for you.");

    return false;

  }

}

让我知道它是否按预期工作。

查看完整回答
反对 回复 2023-09-08
?
绝地无双

TA贡献1946条经验 获得超4个赞

我更喜欢使用ajax请求而不是表单操作


超文本标记语言


<form>

    <div class="form-group">

       <label for="email">Email address:</label>

       <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email to confirm the order">

   </div>


   <div class="form-group">

        <div class="checkbox">

            <label><input type="checkbox" id="TOS" value="This"> I certify that I am of legal age and I have read and agree to the 

            <a href="../terms.php" target="_blank">Terms of use</a> and

            <a href="../privacy.php" target="_blank"> Privacy Policy </a>of Sdocks LLC</label>

        </div>

    </div>

 <button type="button" onclick="SubmitRequest()" class="btn btn-primary">Submit</button>

</form>

js


function SubmitRequest() {

  if (document.getElementById('TOS').checked){

  var postObj = {

      email: $('#email').val(),

  };

  $.ajax({

      url: "/tienda3.php",

      data: JSON.stringify(postObj),

      type: "POST",

      contentType: "application/json;charset=utf-8",

      dataType: "json",

      success: function (result) {

       console.log(result)

      },

      error: function (errormessage) {

          console.log(errormessage);

      }

  });

}

});


查看完整回答
反对 回复 2023-09-08
  • 2 回答
  • 0 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号