Ajax POST请求到PHP电子邮件验证器-如何获得电子邮件输入值



我正在为我的Joomla/Virtuemart网站的结帐页面创建一个Ajax电子邮件验证。

这个电子邮件验证器必须检查输入的电子邮件地址是否已经存在于数据库中,并显示一个实时消息。

我几乎完成了这个脚本,但似乎Ajax代码发送的post数据没有正确传输到我的php脚本。

这是我的jQuery脚本:
jQuery(document).ready(function(){
    jQuery('#confirmbtn_button').attr('disabled','');
    var emailok = false;
    var myForm = jQuery("#adminForm"), email = jQuery("#email_field"), emailInfo = jQuery("#emailInfo");
        var emaildata = jQuery('#email_field').val();
    //send ajax request to check email
    email.blur(function(){
        jQuery.ajax({
            type: "POST",
            data: {fmail:emaildata},
            url: "/CustomCodes/js/check.php",
            beforeSend: function(){
                emailInfo.html("<font color='blue'>Undersøger Email...</font>");
            },
            success: function(data){
                if(data == "invalid")
                {
                    emailok = false;
                    emailInfo.html("<font color='red'>Ugyldig Email</font>");
                }
                else if(data != "0")
                {
                    emailok = false;
                    emailInfo.html("<font color='red'>Email Eksisterer Allerede..</font>");
                }
                else
                {
                    emailok = true;
                    emailInfo.html("<font color='green'>Email OK</font>");
                }
            }
        });
    });
});
这是我的PHP验证脚本:
    <?php
$email = $_POST['fmail'];
//data connection file
//require "config.php";
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$ftpSERVERURL = JPATH_BASE;
$MainsiteURL = "http://" . $_SERVER['HTTP_HOST'];
// Load configuration
$conf =& JFactory::getConfig();
require_once( JPATH_BASE .DS.'configuration.php' );
$config = new JConfig();
$conf->loadObject($config);
$host = $conf->getValue('config.host');
$user = $conf->getValue('config.user');
$password = $conf->getValue('config.password');
$database = $conf->getValue('config.db');
 // Connects to your Database 
 mysql_connect($host, $user, $password) or die(mysql_error()); 
 mysql_select_db($database) or die(mysql_error()); 

if(eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email))
{
        $sql = "select * from jos_users where email='$email'";
        $rsd = mysql_query($sql);
        $msg = mysql_num_rows($rsd); //returns 0 if not already exist
}
else
{
        $msg = "invalid";
}
echo $msg;
?>
因此,我需要确保正确检索email值,使用PHP脚本中类似的代码:

$email = $_POST['fmail'];

表单中电子邮件输入字段的HTML标记如下:

<input onfocus="inputclear(this)" autocomplete="off" type="text" id="email_field" name="email" size="30" value="" class="required" maxlength="100" aria-required="true" required="required" aria-invalid="false">
我希望有人能帮我解决这个问题。这似乎是一个非常小的问题,但当涉及到Ajax和jQuery
时,我是一个非常新手。

感谢Dru为我指出了正确的方向,我弄清楚了如何改变我的jQuery代码,使其工作。这是更新后的jQuery:

jQuery(document).ready(function(){
    //jQuery('#confirmbtn_button').attr('disabled','');
    var emailok = false;
    var myForm = jQuery("#adminForm"), email = jQuery("#email_field"), emailInfo = jQuery("#emailInfo");
    //send ajax request to check email
    email.blur(function(){
    var emaildata = jQuery('#email_field').val();
        jQuery.ajax({
            type: "POST",
            data: {fmail:emaildata},
            url: "/CustomCodes/js/check.php",
            beforeSend: function(){
                emailInfo.html("");
            },
            success: function(data){
                if(data == "invalid")
                {
                    emailok = false;
                    emailInfo.html('<div class="invalid" style="width: 292px;color: #F00;position: relative;top: -12px;margin-bottom: -14px;left: 10px;">Den indtastede Email: "'+ emaildata + '" - er ugyldig!!!</div>');
                    jQuery('#email_field').val('');
                }
                else if(data != "0")
                {
                    emailok = false;
                    emailInfo.html("<div class='alreadyexist' style='width: 292px;color: #F00;position: relative;top: -12px;margin-bottom: -14px;left: 10px;'>Email: "+ emaildata + ", findes allerede i systemet. Log ind ovenfor.</div>");
                    jQuery('#email_field').val('');
                }
                /*
                else
                {
                    emailok = true;
                    emailInfo.html("<font color='green'>Email OK.</font>");
                }
                */
            }
        });
    });
});

正如你所看到的,我只需要移动"var emaildata = jQuery('#email_field').val();",这样它就被放在"email.blur(function(){"之后,现在我可以通过调用"$email = $_POST['fmail'];"在PHP脚本(check.php)中检索数据。

最新更新