Hello stackoverflow我创建了一个创建用户表单...
看起来像这样......
<form action = "createuser.php" method = "post" id = "from2">
<input type ="text" id = "namn" name="namn"placeholder = "Your name" required><br>
<input type ="email" id = "usernamet" name="username"placeholder = "Email" required><br>
<input type = "password" id = "passwordet" placeholder = "Password" name = "password" required>
<div class="g-recaptcha" data-sitekey="6LcS9hkUAAAAAK_u3cxuIsGtqI3eEdFzZ8haULa3"></div>
<input class = "lgg" type= "submit" value="Create your new account!">
我使用谷歌Recaptca使其安全......
然后我发送带有ajax的表单......看起来像这样
$('#from2').on('submit',function(){
if($('#namn, #usernamet, #passwordet').val()){
$.ajax({
type: "POST",
url: "createuser.php",
data: $('#from2').serialize(),
complete: function(data){
$('#namn, #usernamet, #passwordet').val('');
}
});
return false;
}
else{
alert("Insert values!");
}
});
然后php端看起来像这样
<?php
some google recapcha stuff up here
$response = json_decode(curl_exec($curl));
if(!$response->success){
echo "Your user was NOT created, use another email... or are you a robot?";
}
else{
insert the user in database
}
?>
我的问题是,当我的表单在同一页面上的响应为false时,如何显示回显?因此,用户知道他的帐户是否已创建?!
答案 0 :(得分:0)
您可以随时使用success
拨打error
或$.ajax
回调,如上述评论中所述。
$.ajax({
url: 'createuser.php',
type: 'POST',
data: $('#from2').serialize(),
//if successful callback:
success: function(response) {
alert(response);
//you can ofcourse append or use any alternative
},
//if failed
error: function(response) {
alert(reponse)
}
});
答案 1 :(得分:0)
为什么不直接从表单发送数据,然后使用PHP将其添加到数据库中:
<强> HTML:强>
<form action = "createuser.php" method = "post" id = "from2">
<input type ="text" id = "namn" name="namn"placeholder = "Your name" required><br>
<input type ="email" id = "usernamet" name="username"placeholder = "Email" required><br>
<input type = "password" id = "passwordet" placeholder = "Password" name = "password" required>
<div class="g-recaptcha" data-sitekey="6LcS9hkUAAAAAK_u3cxuIsGtqI3eEdFzZ8haULa3"></div>
<input class = "lgg" type= "submit" name="submit" value="Create your new account!">
</form>
注意:我在
中添加了值为“submit”的name属性<input type="submit">
PHP( createuser.php ):
<?php
...some google recapcha stuff up here...
if (isset($_POST["submit"]))
{
...do smth... (e.g echo "success";
}
else
{
echo "Registration unsuccessful";
}
?>