I’m asking a question about conditional form field submitting using jquery and ajax in PHP, in my form there is two types of select box’s (1. reference and 2. reference_info), in reference select box there two options (1. None, and 2. Friends) if user select 2nd option (Friends) then user must select option in reference_info select box.
// Submit Form
$('#add_new_user').on('submit', function(event){
event.preventDefault();
var name = $('#name').val();
var age = $('#age').val();
var gender = $('#gender').val();
var address = $('#cddress').val();
var contact = $('#contact').val();
var reference = $('#reference').val();
var ref_info = $('#reference_info').val();
if(name != '' && age != '' && gender != '' && reference != '' && ref_info != ''){
var formData = $(this).serialize();
/*$.ajax({
url:"ajax.php",
method:"post",
data:formData,
success:function(data){
alert(data);
}
});*/
alert(formData);
}else{
alert("Please fill required fields.");
}
});
Form structure:
Reference select Box
<select name="reference" class="form-control" id="reference">
<option>Select</option>
<option value="1">None</option>
<option value="2">Friends</option>
</select>
Reference Info select Box
<select name="reference_info" class="form-control" id="reference_info">
<option>Select</option>
<option value="1">Friend 1</option>
<option value="2">Friend 2</option>
<option value="3">Friend 3</option>
</select>
2
Answers
Try something like this.do not post code image.