skip to Main Content

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


  1. Try something like this.do not post code image.

    function friends_function() {
    if(ref_info == '') {
        alert("please fill ref_info")
        return
     } 
    
     //here ajax function
    
    
    }  
    
    function none_ref_funtion() {
        //your normal ajax function
    }
    
    
    
    
    
    
    switch(reference) {
        case "friends":
        friends_function();
        break;
        case "none":
        none_ref_function();
        break;
    }
    
    Login or Signup to reply.
  2. $('#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();
    
        var errorcount = 0; 
        if(reference == 'Friends' && ref_info == '') {
          errorcount++;
          alert("Please select ref info");
        }
    
        if(errorcount == 0){
    
         if(name != '' && age != '' && gender != '' && reference != ''){
            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.");
         }
       }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search