ラジオ フィールドの値で [はい] を選択すると、都市を選択するための新しいフィールドが表示されるフォームがあります。都市のドロップ ダウン オプションは、select from で始まります。選択した最初のオプション値を空にする必要があります。したがって、特定のフィールドの検証は、ユーザーがラジオ オプションから [はい] を選択した場合にのみ行われ、ユーザーに値の選択を強制するために検証されます。
フォームの 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 で確認することはまだできません。