简体   繁体   English

在ajax / jquery中检入一个表单,然后提交(检查值是否在数据库中)

[英]Check in ajax/jquery a form beform submit ( check the value whether it is in database)

ajax is not yet sothin i master. 阿贾克斯还不是我掌握的东西。 I have two forms field code : name : 我有两种形式的字段代码:name:

and the submit button like : 和提交按钮,如:

<form><input type=text name=code><input type =text name=name/></form>

I would like in php/jquery to check if the code the user fill exist in a table of my db. 我想在php / jquery中检查用户填充的代码是否存在于我的数据库表中。 If it does not exits, when the user leave the textfield to fill the next one, i would like to print a message like: this code is not in the db and then clean the fied. 如果它没有退出,则当用户离开文本字段以填充下一个文本框时,我想打印一条消息,例如:此代码不在db中,然后清理该字段。 Until the user provide a valide code. 直到用户提供验证码。

I've created a simple example at http://jsfiddle.net/nickywaites/e4rhf/ that will show you have to create a jQuery ajax post request. 我在http://jsfiddle.net/nickywaites/e4rhf/创建了一个简单的示例,该示例将显示您必须创建jQuery ajax发布请求。

I'm not too familiar with php so that part of it I'll have to leave aside although you can use something along the lines of $_POST["Name"]. 我对php不太熟悉,尽管您可以沿$ _POST [“ Name”]的方式使用某些内容,但我不得不抛弃它的一部分。

Here is php example that I googled http://php4every1.com/tutorials/jquery-ajax-tutorial/ that might be better for you. 这是我用Google搜索http://php4every1.com/tutorials/jquery-ajax-tutorial/的 php示例,可能对您更好。

If your php service returns true or false for validation. 如果您的php服务返回true或false进行验证。 and the placeholder for the error is a label called 错误的占位符是一个名为

then an example (in jQuery) would be 那么一个例子(在jQuery中)将是

$(document).ready(function() {
  $("form").submit(function(e) {
    var code = $("input[name='code']");
    var error = $("#error");
    e.preventDefault();
    var form = this;
    $.getJSON('urlToPhp',
      { code: code.val() },
      function(valid) {
        if (!valid) {
          error.text(code.val() + ' is not found try another code...');
          code.val('');
        } else {
          form.submit();
        }
      }
    );

  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM