0
<script>
    $(document).ready(function() {

      $("#btnSubmit").live('click',function(){     
        var sum = '0';
        $("[id^=FormData_][id$=_c_data]").each(function(){

             var c_data = $(this).val();
              var required = $(this).attr("data-required");
              var label = $(this).attr("data-label");
              if(required == '1'){

                if(c_data == ""){
                    sum += '1';
                }
            }

        });

        if(sum == "0"){

        $("[id^=FormData_][id$=_c_data]").each(function(){
              var c_data = $(this).val();
            var admin = $(this).attr("data-admin");
            var form = $(this).attr("data-form");
            var component = $(this).attr("date-component");
            var unic = $(this).attr("data-unic");
             var user = $(this).attr("data-user");
              var url = "<?php echo  Yii::app()->createUrl('formdata/admin&id='.$form_id);?>";
            if(c_data == ""){

                var site_url = "<?php echo Yii::app()->createUrl('/formdata/deleteDetail' ); ?>";
                jQuery.ajax({
                    type: "POST",
                    url: site_url,
                    data: {new_value:c_data,admin:admin,form:form,component:component,unic:unic,user:user},
                    cache: false,
                    async: false,
                    success: function(response){

                    }
                });
            } else {

                var site_url = "<?php echo Yii::app()->createUrl('/formdata/updateDetailValue' ); ?>";
                jQuery.ajax({
                    type: "POST",
                    url: site_url,
                    data: {new_value:c_data,admin:admin,form:form,component:component,unic:unic,user:user},
                    cache: false,
                    async: false,
                    success: function(response){     
                    }
                });
            } 
        });
        window.location = "http://www.example.com";

        }else {
            if(sum != ""){

                bootbox.dialog({
                        message: 'Please Fill All Required Field !',
                        title: 'Alert',
                        buttons: {
                              main: {
                                label: 'OK',
                                className: 'blue'                        
                              }
                            }
                    });
                  return false;
                }  
        }
     });     
    });
</script>

in this script window.location = "http://www.example.com"; is not working. But I check alert message it is working fine. why its not working in if condition. I need to redirect page when each function was completed. please any one help me:-((((((((((((((((((((((((((((

RonyLoud
  • 2,408
  • 2
  • 20
  • 25
Prabhakaran
  • 60
  • 2
  • 12

3 Answers3

0

Try this.,

window.location.href = 'http://www.google.com';

This may work for you.

Window.location.href and Window.open () methods in JavaScript

Community
  • 1
  • 1
ganeshreddy
  • 33
  • 1
  • 6
0

jQuery is not necessary, and window.location.replace(url) will best simulate an HTTP redirect.

still you want to do this with jQuery use this $(location).attr('href', 'url')

0

If I got your question correct, you want to redirect the user when all your ajax requests, within your each function, are completed. For this, you can create an array that will hold the success status of each ajax request, and depending on this array you may do your redirection task.

Add below few snippets to your existing code:

  1. In your #btnSubmit click function (Though, I recommend you use .on() delegation method)

    var ajax_succ_arr = []; // success status container
    var this_ajax_succ = false; // flag
    
  2. In you success function of both ajax calls (within your each function).

        if(c_data == ""){
            ...
            jQuery.ajax({
                ...
                success: function(response){
                  if(response == "1"){
                    this_ajax_succ = true; // set true if required response is received
                  }
                }
            });
            ajax_succ_arr.push(this_ajax_succ); // push it to the success array
         } else {
             ...
            jQuery.ajax({
                ...
                success: function(response){
                  if(response == "1"){
                    this_ajax_succ = true; // set true if required response is received
                  }
                }
            });
            ajax_succ_arr.push(this_ajax_succ); // push it to the success array
        }
    
  3. And finally your redirection. Put this just after each function ends.

    if(ajax_succ_arr.indexOf(false)<0){ // if all statuses are ok
      window.location="http://www.example.com";
    }
    

Hope this helps.

Yogesh Mistry
  • 2,082
  • 15
  • 19