3

我有一个带有收音机的表单,并尝试从 jq/$.ajax 开始验证,然后在 php 中发布信息。

表单.php

<tr>
  <td class="h"><span class="txt_wht">t1</span></td>
        <input type="hidden" name="gm[0]" value="t1" />
  <td class="vs"><span class="txt_yel_sm">vs</span></td>
  <td class="a"><span class="txt_wht">t2</span></td>
        <input type="hidden" name="gm[1]" value="t2" />

  <td><input type="radio" name="g1" value="a" /></td>
  <td><input type="radio" name="g1" value="b" /></td>
  <td><input type="radio" name="g1" value="c" /></td>
  </tr>
<tr>
  <td class="h"><span class="txt_wht">t1</span></td>
        <input type="hidden" name="gm[0]" value="t1" />
  <td class="vs"><span class="txt_yel_sm">vs</span></td>
  <td class="a"><span class="txt_wht">t2</span></td>
        <input type="hidden" name="gm[1]" value="t2" />

  <td><input type="radio" name="g2" value="a" /></td>
  <td><input type="radio" name="g2" value="b" /></td>
  <td><input type="radio" name="g2" value="c" /></td>
 </tr>

验证.js

$(document).ready(function () {
    //global vars

    form.submit(function () {
        if (validate1() && validate2()) {
            //vars
            var exst_email = $('#exst_email').attr('value');
            var g1 = $("input[name='g1']:checked").attr('value');
            //more vars

            $.ajax({
                type: "post",
                url: "scripts/sbmt.php",
                data: "exst_email=" + exst_email,
                success: function (exst_rsp) {

                    //if its not exist
                    if (exst_rsp == "not_exist") {
                        valInfo.text("Error1!");
                        valInfo.addClass("error");
                        email_reg_err = true;
                    } else {
                        valInfo.text("");
                        valInfo.removeClass("error");
                        email_reg_err = false;
                    }
                    //more validation with error msgs

                    //validate 1 & validate 2 functions

和 sbmt.php

session_start();
// connect to db
require_once('conn.php');

if($stmt = $mysqli->prepare('SELECT email FROM table WHERE email=?')){
   $stmt->bind_param("s", $_POST['exst_email']); 
   $stmt->execute();
   $stmt->store_result();

   $numRows = $stmt->num_rows();
   if($numRows < 1){
       echo 'not_exist';
       exit();
   }
   $stmt->close();
}
//more validation

现在,

  1. 由于 Dreamweaver,我知道我没有输入错误或未闭合的标签。

  2. 我的验证 1 和 2 功能工作正常。

  3. 由于萤火虫,

    a) 我知道我的数据发布在 sbmt.php

    b)我没有得到回复以显示错误消息。

我正在使用完全相同的方式(不同的 js/php 文件)验证另外 3 个表单(仅在重要时使用文本区域),它们工作正常!

关于这里可能有什么问题的任何建议?

4

2 回答 2

1

在您提交功能中,您可以尝试:

form.submit(function(e){

   e.preventDefault(); // to prevent the form from submitting itself

  // you all validation code

});

笔记

I think $_POST['email'] in your PHP code should be $_POST['exst_email'], because your posted data in AJAX contains key as exst_email here data: "exst_email="+ exst_email, but not email.

So, if you PHP is right then you should send data as data: "email="+ exst_email

于 2012-07-20T05:54:31.083 回答
0

SOLVED

I don't know why but here's the fix:

I wasn't passing to sbmt.php the <input type="hidden"> values since I didn't use them at the moment.

But I desided to post them as well to see if it does any good. And it worked!

于 2012-07-20T08:35:24.213 回答