Php 如何使提交按钮仅在输入值与mysql数据匹配时发送输入值?
我试图制作一个简单的注册页面,让用户输入用户名和电子邮件。 问题是,当我按下submit按钮而不立即进入下一页时,如何检查mysql中是否已经存在输入值(电子邮件)?如果输入值在mysql数据库中不存在,我想显示一条类似“email not registed”的消息。我正在尝试使用ajax、jquery和php,但我找不到合适的解决方案Php 如何使提交按钮仅在输入值与mysql数据匹配时发送输入值?,php,jquery,ajax,Php,Jquery,Ajax,我试图制作一个简单的注册页面,让用户输入用户名和电子邮件。 问题是,当我按下submit按钮而不立即进入下一页时,如何检查mysql中是否已经存在输入值(电子邮件)?如果输入值在mysql数据库中不存在,我想显示一条类似“email not registed”的消息。我正在尝试使用ajax、jquery和php,但我找不到合适的解决方案 //this is the script to check if the emails that the users entered matches. //I'
//this is the script to check if the emails that the users entered matches.
//I'm trying to post the values to the'checkPage.php' to check if the email exists
//The problem is how can I on move to the next page after the result have been returned?
Sorry for my bad explanation.
<script>
$('input#submitbutton').on('click',function(){
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
});
}else{
}
});enter code here
</script>
//CheckPage.php
//I want the registration page to go to the next page only if the email
//haven't been found in the mysql database.
<?php
if(isset($_POST['mail'])&&isset($_POST['mail2'])){
$mail = $_POST['mail'];
$mail2 = $_POST['mail2'];
try{
$con = mysql_connect("localhost","root",""); echo "connected";
$db = mysql_select_db("db",$con);
$query = mysql_query("select email,id from user where email ='". mysql_real_escape_string(trim($mail))."'",$con);
echo (mysql_num_rows($query)!==0) ? mysql_result($query,0,'email'):'none';
} catch (Exception $ex) {
}
}
?>
*/
//这是检查用户输入的电子邮件是否匹配的脚本。
//我试图将这些值发布到“checkPage.php”中,以检查电子邮件是否存在
//问题是,在返回结果后,我如何移动到下一页?
对不起,我解释得不好。
$('input#submitbutton')。在('click',function()上{
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
如果($.trim(邮件)==$.trim(邮件2)){
$.post('checkPage.php',{mail:mail},函数(数据){
$(“#名称数据”).text(数据);//如果电子邮件存在或不存在,则显示结果
});
}否则{
}
});在这里输入代码
//CheckPage.php
//我想注册页面转到下一页只有在电子邮件
//在mysql数据库中找不到。
*/
您可以使用这个
$('input#submitbutton').on('click',function(event){
event.preventDefault();
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
if (data !== "none") {
$('#your_form_id').submit();
}
});
}else{
}
});
尝试将javascript代码放入
$(document.ready()代码>
首先,在submit按钮上进行ajax调用,如下所示
<script>
function myformsubmit()
{
var mail=$('#mail').val();
$.post( "check.php", { mail: mail})
.done(function( data ) {
if(msg == 'error')
{
$('#diverr').html('Match found');
return false;
}
else
{
//redirect to a valid page
}
});
}
函数myformsubmit()
{
var mail=$('#mail').val();
$.post(“check.php”,{mail:mail})
.完成(功能(数据){
如果(消息=='错误')
{
$('#diver').html('Match found');
返回false;
}
其他的
{
//重定向到有效页面
}
});
}
在check.php中,您必须通过post获取数据,并通过查询返回的邮件ID进行sql select查询循环,如果找到匹配项,则返回“error”或返回空白
<script>
function myformsubmit()
{
var mail=$('#mail').val();
$.post( "check.php", { mail: mail})
.done(function( data ) {
if(msg == 'error')
{
$('#diverr').html('Match found');
return false;
}
else
{
//redirect to a valid page
}
});
}