AJAX表单验证与mysqli_query [英] ajax form validation with mysqli_query
问题描述
到目前为止,我还用纯JavaScript来验证自己的状态,但我需要添加的mysqli查询到组合。只是我没有那么大与jQuery和Ajax。我可以做一个简单的登录表单,但这是稍微复杂一点。谁能给我如何我可以添加一个jQuery / AJAX组件来验证此任何指针:
的foreach($ _ POST作为$关键=> $的){
如果(空($的)及!&安培;!$键='送'和;&安培; $键='标题'!){
$ usercheck =选择电子邮件,用户WHERE电子邮件='$的';
$ usercheck = $ DB->查询($ usercheck);
如果($ usercheck-> NUM_ROWS大于0){$ X =1;继续;}
如果($ usercheck-> NUM_ROWS == 0){$ X =2;打破;}
}
}
如果($ X ==2){$消息= $的是不是一个regestered邮件;}
如果($ X = =1){//有效 - 提交。
你可以做的就是发送 $。员额是这样的:
$。员额(test.php的,{POST1:东西,POST2:somethingelse} //这些将通过邮寄来测试发送.PHP
功能(数据){//返回的数据
执行console.log(data.return1); //这里只是登录到控制台。 **可选**
执行console.log(data.return2);
//完成过程
},JSON); //指定类型的JSON也可选
在的test.php
的foreach($ _ POST作为$关键=> $的){
如果(空($的)及!&安培;!$键='送'和;&安培; $键='标题'!){
$ usercheck =选择电子邮件,用户WHERE电子邮件='$的';
$ usercheck = $ DB->查询($ usercheck);
如果($ usercheck-> NUM_ROWS大于0){$ X =1;继续;}
如果($ usercheck-> NUM_ROWS == 0){$ X =2;打破;}
}
}
如果($ X ==2){$数据['消息'] = $的不是regestered邮件。
回声json_en code($的数据); //回声传回至$。员额.. json_en code()的情况下使用JSON的
}
如果($ X = =1){//有效 - 提交
$数据['消息'] ='有效'; //传递消息为有效职位
回声json_en code($的数据);
}
记住:
如果你要发送一个提交表单中添加事件。preventDefault()
您的JavaScript函数来手动处理的形式。 中,你可以找到更多关于它。
so far I have used pure javascript to validate my form, but I need to add a mysqli query into the mix. Only I'm not so great with jquery and ajax. I can do a simple login form but this is a bit more complicated. can anyone give me any pointers on how I could add a jquery/ajax component to validate this:
foreach($_POST as $key=> $for) {
if(!empty($for) && $key != 'send' && $key != 'title') {
$usercheck = "SELECT email FROM users WHERE email = '$for'";
$usercheck = $db->query($usercheck);
if($usercheck->num_rows > 0) {$x="1"; continue;}
if($usercheck->num_rows == 0){$x="2"; break;}
}
}
if($x == "2") {$message = $for." is not a regestered email";}
if($x == "1") { // valid - submit.
what you can do is to send $.post like this:
$.post("test.php", { "post1": "something", "post2":"somethingelse" }, // those will be sent via post to test.php
function(data){// the returned data
console.log(data.return1); // here just logging to the console. **optional**
console.log(data.return2);
// complete your process
}, "json"); // specifying the type as json also optional
in your test.php
foreach($_POST as $key=> $for) {
if(!empty($for) && $key != 'send' && $key != 'title') {
$usercheck = "SELECT email FROM users WHERE email = '$for'";
$usercheck = $db->query($usercheck);
if($usercheck->num_rows > 0) {$x="1"; continue;}
if($usercheck->num_rows == 0){$x="2"; break;}
}
}
if($x == "2") {$data['message'] = $for." is not a regestered email";
echo json_encode($data); // echo to pass back to $.post .. json_encode() in case of using json
}
if($x == "1") { // valid - submit
$data['message'] = 'valid'; // pass the message as valid post
echo json_encode($data);
}
Remember:
If you are posting a form submit to add event.preventDefault()
to your javascript function to handle the form manually. here you can find more about it.
这篇关于AJAX表单验证与mysqli_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!