我有一个div标签,它构成了我网站的一个区域,我希望允许用户提交他们的电子邮件作为邮件列表的一部分。我有这样做的PHP代码,但是,我想做一些调整,以便在可用性方面做得更好。
我目前在html中搜索有问题的区域
<div id="sideimage1">
<div id="maillist">
<div class="sidebartitle">Keep Up To Date</div>
<div class="sidebartext">Sign up now to receive free newsletters and the best deals direct to your email.</div>
<form id="maillist" name="maillist" method="post" action="maillistprocess.php" style="padding-top:10px;">
<div style="text-align:left;"><input name="email" type="text" id="email" size="40" style="margin-top:5px; margin-left:15px; font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif;" value="name@example.com"/>
</div>
<div style="padding-top: 15px; text-align: left; margin-left:15px;">
<input name="submit" type="image" alt="Click To Join Our Mailing List" class="JoinNowbutton"
src="../WebImages/JoinNowButton1.jpg" onmouseover="this.src='../WebImages/JoinNowButton2.jpg'" onmouseout="this.src='../WebImages/JoinNowButton1.jpg'"/></div>
</form>
</div>
我想知道是否有人知道当用户点击加入按钮时它会如何根据我的php中的验证结果替换“sibebartext”之类的内容,例如“恭喜”或“你还没有输入过vlaue”寻找空盒子的脚本。直到我知道如何做到这一点,我只是让他们的PHP直接用户成功注册后返回主页。
我认为可能需要使用jquery,但没有太多的经验。
提前致谢。
答案 0 :(得分:1)
你可以使用jquery。可以使用以下方法完成通过AJAX而非常规请求提交表单的简单方法:
$('form').submit(function() {
var $el = $(this);
var url = $el.attr('action');
$.post(url, $el.serialize(), function(data) {
if (data=='success') {
$('.sidebartext').html('congratz');
} else {
alert('error: ' + data);
}
}).error(function() { alert('something went wrong'); });
return false;
});
然后在php中只返回'success'作为完整响应时的成功或错误消息,当它不是
答案 1 :(得分:1)
$("form#maillist").on("submit", function() {
var s = $("#email").val();
if(s=="") {
$(".sidebartext").text("Enter Some Value");
return false;
//easier to return false. Else it could get complicated with more checks and all the nested if statments (considering also checking name/addres/etc
}
$.ajax({
url : "check.php",
data : s,
cache : false,
success : function(data) {
if(data=='Completed')
$('.sidebartext').html("Congratulations!");
else
$('.sidebartext').html("Error! " + data); //allow for more information in the error then just error (perhaps invalid email or email already in use)
}
}
});
在PHP CHECK和ECHO“恭喜!”为了成功和ECHO“错误!”对于某些错误,还要将check.php更改为您的php脚本名称!
答案 2 :(得分:0)
如果一切正常,您可以使用javascript检查空白值,然后提交表单:
function checkForm(){
if(document.getElementById('email').value == ''){
document.getElementById('sidebartext').innerHTML = 'You have not entered a vlaue';
}else{
form[0].submit;
}
}
确保<div class="sidebartext">
的ID为sidebartext
。
我建议进行ajax调用以确保电子邮件实际插入到数据库中。 Here's一个例子。
ajax调用的另一个例子:
xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', '/ajax/script.php?id=' + id, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
//complete
}
else
{
//try again
}
}