php - Jquery 没有验证我的表单

标签 php jquery

我想使用 jquery 验证我的表单(字段“name1”是必需的,“email”也是必需的,并且应该采用电子邮件格式。我按照教程进行操作,但无法使其工作。我该如何修复它?

提前谢谢您。

我的代码:

     <html>
        <head>
   <script src="/webvanta/js/jquery/1.4/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/webvanta/js/jquery/1.4/plugins/form/jquery.form.js" type="text/javascript"></script>
<script src="/webvanta/js/jquery/1.4/plugins/validate/jquery.validate.min.js" type="text/javascript"></script>
        </head>
        <body>
        <script type="text/javascript">
          $(document).ready(function() {
           $("#myform").validate({
           rules: {
             name1: "required",
             email: {
               required: true,
               email: true
             }
           },
           messages: {
             name1: "Please specify your name",
             email: {
               required: "We need your email address to contact you",
               email: "Your email address must be in the format of name@domain.com"
             }
           }
        })
        });

        </script>

        <?php include 'header.php'; ?>

        <form id = "myform" action="picture.php" method="get">
        Picture name: <input name="name1" class="required"   /><br>
        Email: <input name="email" class="required"  />

        <input type="submit" value="Click" />
        </form>
        <br>




        </body>
        </html> 

最佳答案

一个非常简单的ajax示例:

   $("#submit").click( function() { 

    var str = $("#your-form").serialize();


    $.ajax({
      type: "POST",
      url: "url-to-your-file.php",
      data: str,
      success: function(msg) {

          if(msg=="1") {//let's say 1 means error, do stuff here}
          if(success=="0){//success, do stuff here}
          }
    });


    });

在您的 php 文件中检查并验证输入。如果没问题,则 echo 0,否则 echo 1 - 只是作为示例,以使其非常简单。

关于php - Jquery 没有验证我的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11932972/

相关文章:

jquery - 如何获取具有多个表单输入的变量

javascript - 不允许在 textarea 中换行

PHP 多个客户端访问同一文件

php - 如何在 php xampp 中启用或安装 XLST

php - PHP 函数中 return 语句之后的代码

javascript - 使用自定义标签值 jquery 获取元素 id

javascript - Slick Carousel 上的 Twitter Bootstrap 工具提示

php - SQL> PHP>数组>输出为HTML-很多条件

post - PHP 设置? PHP 没有收到 POST 数据

javascript - jquery clone() 内部是否使用 documentfragment ?