I have a form that when on my radio field value they select yes a new field appears to select a city. The drop down options of city starts with select from. I need the first option value which is select from to be empty. So the validation on the specific field only happens when the user selects yes from the radio option and it validates to force the user to select a value.
The html part of the form:
<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>
The jquery to show the option values if yes is selected:
$(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
}
});
});
And the validation in php
if($printed == "yes"){
if(empty($fromstore)){ $action['result'] = 'error'; array_push($text,'You forgot your city'); }
}
Obviously the php validation is wrong. Any help?
NEW EDIT:
Hi,
here is the complete 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>
I still cant get the form to validate and check if radio button checked yes then check the fromstore if option value has been selected.