我是新手,我正在编写注册表,并试图找出为什么我不能要求“名称”字段。电子邮件和其他一切似乎工作正常。我错过了什么? THX
这就是我对 signup.js
所拥有的$(document).ready(function() {
$("#signup-form").submit(function(e){
e.preventDefault();
var $form = $(this),
name = $form.find('input[name="name"]').val(),
email = $form.find('input[name="email"]').val(),
url = $form.attr('action');
$.post(url, {name:name, email:email},
function(data) {
if(data)
{
if(data=="Some fields are missing.")
{
$("#status").text("Please fill in your name and email.");
$("#status").css("color", "red");
}
else if(data=="Invalid name.")
{
$("#status").text("Please fill in your name.");
$("#status").css("color", "red");
}
else if(data=="Invalid email address.")
{
$("#status").text("Please check your email address.");
$("#status").css("color", "red");
}
else if(data=="Invalid list ID.")
{
$("#status").text("Your list ID is invalid.");
$("#status").css("color", "red");
}
else if(data=="Already subscribed.")
{
$("#status").text("You're already subscribed!");
$("#status").css("color", "red");
}
else
{
$("#status").text("You're subscribed! Please check your email for confirmation.");
$("#status").css("color", "green");
}
}
else
{
alert("Sorry, unable to subscribe. Please try again later!");
}
}
);
});
});
html 文件包含:
<form action="signup.php" method="POST" accept-charset="utf-8" name="signup-form" id="signup-form" class="sign_form" >
<label for="Silver">Get Notified</label>
<div>
<input type="text" name="name" value="" placeholder="Full Name" class="name" />
<input type="text" name="email" value="" placeholder="Email" class="email" />
<input type="submit" value="➜" id="submit-btn" class="submit" />
</div>
<label id="status"></label>
</form>
并且 signup.php 文件为:
<?php
$submit_url = 'http://www.domain.com/signup';
$list = 'list010101';
//POST variables
$name = $_POST['name'];
$email = $_POST['email'];
//subscribe
$postdata = http_build_query(
array(
'name' => $name,
'email' => $email,
'list' => $list,
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($submit_url.'/subscribe', false, $context);
echo $result;
&GT;
答案 0 :(得分:0)
我认为您应该在发布之前进行验证,因为它似乎是在发布到服务器然后检查值。在将数据发送到服务器之前进行验证是个好主意。
类似的东西:
$("#signup-form").submit(function(e){
e.preventDefault();
//use ids to find the form field e.g. <input type='text' id='name'>
var name = $("#name").val();
var email = $("#email").val();
//this is just checking if its an empty string, add your own validation here
if (email == "") return false;
if (name == "") return false;
//return true if validation passes for the form to submit or do your ajax post here
return true;
您也可以使用此插件jquery validation