我有一个表单,当我在我的无线电字段值上选择是时,会出现一个新字段来选择一个城市。城市的下拉选项从选择开始。我需要选择的第一个选项值为空。因此,仅当用户从无线电选项中选择“是”并且它验证强制用户选择值时,才会对特定字段进行验证。
表单的html部分:
<li>
<label for="printed">Printed:</label>
No<input type="radio" name="printed" value="no" class="morefrom" checked >
Yes<input type="radio" name="printed" value="yes" class="morefrom">
</li>
<li>
<div id="collectfrom">
<label for="fromstore">Collect From:</label>
<select name="fromstore">
<option value=" ">Choose from</option>
<option value="nicosia">Nicosia</option>
<option value="limassol">Limassol</option>
<option value="paphos">Paphos</option>
</select>
</div>
</li>
如果选择是,则显示选项值的jquery:
$(document).ready(function(){
$('#collectfrom').css('display','none');
$('.morefrom').click(function(){
if ($('input[name=printed]:checked').val() == "yes" ) {
$('#collectfrom').slideDown(); //Slide Down Effect
} else {
$('#collectfrom').slideUp(); //Slide Up Effect
}
});
});
在php中验证
if($printed == "yes"){
if(empty($fromstore)){ $action['result'] = 'error'; array_push($text,'You forgot your city'); }
}
显然php验证是错误的。有什么帮助吗?
新编辑:
您好,
这是完整的index.php:
//setup some variables/arrays
$action = array();
$action['result'] = null;
$text = array();
//check if the form has been submitted
if(isset($_POST['signup'])){
//cleanup the variables
//prevent mysql injection
$name = mysql_real_escape_string($_POST['name']);
$surname = mysql_real_escape_string($_POST['surname']);
$address = mysql_real_escape_string($_POST['address']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$contactnumber = mysql_real_escape_string($_POST['contactnumber']);
$email = mysql_real_escape_string($_POST['email']);
$mobile = mysql_real_escape_string($_POST['mobile']);
$hypernumber = mysql_real_escape_string($_POST['hypernumber']);
$fromstore = mysql_real_escape_string($_POST['fromstore']);
$hcardnumber = mysql_real_escape_string($_POST['hcardnumber']);
//quick/simple validation
if(empty($name)){ $action['result'] = 'error'; array_push($text,'You forgot your name'); }
if(empty($surname)){ $action['result'] = 'error'; array_push($text,'You forgot your surname'); }
if(empty($address)){ $action['result'] = 'error'; array_push($text,'You forgot your address'); }
if(empty($postcode)){ $action['result'] = 'error'; array_push($text,'You forgot your postcode'); }
if(empty($contactnumber)){ $action['result'] = 'error'; array_push($text,'You forgot your contact number'); }
if(empty($email)){ $action['result'] = 'error'; array_push($text,'You forgot your email'); }
if(empty($mobile)){ $action['result'] = 'error'; array_push($text,'You forgot your mobile'); }
if($printed == "yes"){
if(empty($_POST['fromstore'])){ $action['result'] = 'error'; array_push($text,'You forgot your city'); }
}
if($action['result'] != 'error'){
if($printed == "yes")
{
if($fromstore == "nicosia")
{
$file = file("nicosia.txt");
list($hcardnumber) = explode(",", $file[0]);
$fp = fopen("nicosia.txt", "w+");
for($i = 1; $i < sizeof($file); ++$i) {
fwrite($fp, trim($file[$i]) . "\n");
}
fclose($fp);
echo "Your card number is: $hcardnumber.";
}else if ($fromstore == "limassol")
{
$file = file("limassol.txt");
list($hcardnumber) = explode(",", $file[0]);
$fp = fopen("limassol.txt", "w+");
for($i = 1; $i < sizeof($file); ++$i) {
fwrite($fp, trim($file[$i]) . "\n");
}
fclose($fp);
echo "Your card number is: $hcardnumber.";
}else if ($fromstore == "paphos")
{
$file = file("paphos.txt");
list($hcardnumber) = explode(",", $file[0]);
$fp = fopen("paphos.txt", "w+");
for($i = 1; $i < sizeof($file); ++$i) {
fwrite($fp, trim($file[$i]) . "\n");
}
fclose($fp);
echo "Your card number is: $hcardnumber.";
}
}else if ($printed == "no"){
$file = file("all.txt");
list($hcardnumber) = explode(",", $file[0]);
$fp = fopen("all.txt", "w+");
for($i = 1; $i < sizeof($file); ++$i) {
fwrite($fp, trim($file[$i]) . "\n");
}
fclose($fp);
echo "Your card number is: $hcardnumber.";
}
//add to the database
$add = mysql_query("INSERT INTO `users` VALUES(NULL,'$name','$surname','$address','$postcode','$contactnumber','$email','$mobile','$hypernumber','$printed','$fromstore',0,'$hcardnumber')");
if($add){
//get the new user id
$userid = mysql_insert_id();
//create a random key
$key = $name . $email . date('mY');
$key = md5($key);
//add confirm row
$confirm = mysql_query("INSERT INTO `confirm` VALUES(NULL,'$userid','$key','$email')");
if($confirm){
//include the swift class
include_once 'inc/php/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'name' => $name,
'email' => $email,
'key' => $key,
'hcardnumber' => $hcardnumber);
//send the email
if(send_email($info)){
//email sent
$action['result'] = 'success';
array_push($text,'Thanks for signing up. Please check your email for confirmation!');
}else{
$action['result'] = 'error';
array_push($text,'Could not send confirm email');
}
}else{
$action['result'] = 'error';
array_push($text,'Confirm row was not added to the database. Reason: ' . mysql_error());
}
}else{
$action['result'] = 'error';
array_push($text,'User could not be added to the database. Reason: ' . mysql_error());
}
}
$action['text'] = $text;
}
?>
<?php
include 'inc/elements/header.php'; ?>
<?= show_errors($action); ?>
<form method="post" action="">
<fieldset>
<ul>
<li>
<label for="name">Name:</label>
<input type="text" name="name" />
</li>
<li>
<label for="surname">Surname:</label>
<input type="surname" name="surname" />
</li>
<li>
<label for="address">Address:</label>
<input type="address" name="address" />
</li>
<li>
<label for="postcode">Post Code:</label>
<input type="postcode" name="postcode" />
</li>
<li>
<label for="contactnumber">Contact Number:</label>
<input type="contactnumber" name="contactnumber" />
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" />
</li>
<li>
<label for="mobile">Mobile:</label>
<input type="mobile" name="mobile" />
</li>
<li>
<label for="hypernumber">Hypercard Number:</label>
<input type="hypernumber" name="hypernumber" />
</li>
<li>
Printed:
<label for="printed_no">No</label>
<input type="radio" name="printed" value="no" class="morefrom" checked="checked" id="printed_no">
<label for="printed_yes">Yes</label>
<input type="radio" name="printed" value="yes" class="morefrom">
</li>
<li>
<div id="collectfrom">
<label for="fromstore">Collect From:</label>
<select name="fromstore" id="fromstore">
<option value="">Choose from</option>
<option value="nicosia">Nicosia</option>
<option value="limassol">Limassol</option>
<option value="paphos">Paphos</option>
</select>
</div>
</li>
<li>
<input type="hidden" name="hcardnumber" value="<?php echo $hcardnumber ?>"/>
</li>
<li>
<input type="submit" value="Signup Now" class="large blue button" name="signup" />
</li>
</ul>
</fieldset>
</form>
我仍然无法获取表单进行验证并检查单选按钮是否选中是,然后检查fromstore是否已选择选项值。
答案 0 :(得分:0)
首先,$ fromstore的值总是至少是一个空格字符,并且在您的示例中永远不会为空,因为您在HTML中提供了一个文本值(即使空格仍然是文本值)。从您发送到PHP的内容中删除空格,if(empty())条件将开始正确评估。
其次,当表单输入具有关联ID时,最好使用该元素。这样,当您单击标签本身时,表单元素会自动获得焦点(更好的可访问性和可用性)。每个输入都应该有一个关联的标签,它简单地描述输入用于/表示的内容。
将您的HTML修改为更像这样:
<li>
Printed:
<label for="printed_no">No</label>
<input type="radio" name="printed" value="no" class="morefrom" checked="checked" id="printed_no">
<label for="printed_yes">Yes</label>
<input type="radio" name="printed" value="yes" class="morefrom">
</li>
<li>
<div id="collectfrom">
<label for="fromstore">Collect From:</label>
<select name="fromstore" id="fromstore">
<option>Choose from</option>
<option value="nicosia">Nicosia</option>
<option value="limassol">Limassol</option>
<option value="paphos">Paphos</option>
</select>
</div>
</li>
此外,您还没有在上面的代码段中包含该元素,您是否明确设置它并运行PHP脚本?我会假设你是,但只是想确定;)
最后,您似乎依赖于PHP的register_globals设置被启用,这是非常古老的做法,您应该考虑更新自己以使用更广泛接受的方法。现在大多数服务器都没有启用register_globals,如果你从一台服务器迁移到另一台服务器,你可能会发现你的脚本崩溃了。
请点击此处了解更多信息:
Why is REGISTER_GLOBALS so bad?
在这种情况下,请将PHP更新为:
if($_POST['printed'] == "yes"){
if(empty($_POST['fromstore'])){
$action['result'] = 'error';
/* I assume $text used below is a variable instantiated
* somewhere else in this script and isn't sent from the
* form. Thus I've left it as $text instead of $_POST['text']
*/
array_push($text,'You forgot your city');
}
}
希望这有帮助,
尼尔