我正在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;
?>
所以基本上我需要确保正确地检索电子邮件值,使用与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来说,我是一个非常小的问题
答案 0 :(得分:1)
感谢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) p>