0

I am getting result 3 div's but i want to get result for checking.

my ajax page

  <div> <!-- This is empty div and please don't remove this -->
        <div id="inner_1">
      <?php echo $val; ?>
        </div>

        <div id="inner_2">
        <?php echo $val; ?>
        </div>
    </div>

    my html page 

      <div id="div_1"></div>
        <a href="" class="button">Click Me</a>
        <div id="div_2"></div>

my javascript page is

 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js" language="javascript"></script>
        <script type="text/javascript" language="javascript">
        $( function() {

            $( '.button' ).click(function() {   
                var postData = ''"; // you can send any data to ajax file. 
                $('#div_1 , #div_2').html('<img src="ajax-loader.gif" />'); // placeholder
                $.ajax( {
                    url : 'ajax_file.php', // your ajax file
                    type : 'post',
                    data : postData,                
                    success : function( resp ) {
                        $('#div_1').html($('#inner_1' , resp).html());
                        var result=('#div_2').html($('#inner_2' , resp).html());
                        alert(result);

                        // here i want to get result
                        // but i am getting object
                        // for the purpose of validation negative or positive in my project


                    }
                });
                   return false;
            });
        });
    </script>
3
Contributors
3
Replies
20
Views
2 Years
Discussion Span
Last Post by ramsiva
0

As i understood your code. plz put dataType: 'json', in your ajax request. i dontn't know what is your ajax_file.php is returning like html or json?
if your php script returing json then you can access json like resp.Mstatus. my php script returning below json code.

 {"Mstatus":"error","Cmessage":"","msg":"Your password is invalid."}

hope it helps you.

1

From your code, I assumed your datatype returned from ajax_file.php is in html format.

success : function( resp ) {
    $('#div_1').html($('#inner_1' , resp).html());
    var result=('#div_2').html($('#inner_2' , resp).html());
    alert(result);// the result here is an object where result = $('#div_2')
}

you should use the resp for the validation.

And, I agree with @mangel.murti that using the json method is a better way to easier the validation. Example:
ajax_file.php

$return = array();
if($success){
    $return['is_success'] = true;
    $return['htmls'] = "<p>Successful message</p>";
    $return['err_msg'] = "";
}else{
    $return['is_success'] = false;
    $return['htmls'] = "<p>Some error message</p>";
    $return['err_msg'] = "Some error message";
}
echo json_encode($return);

Then the ajax code will be

$.ajax({
    url : 'ajax_file.php',
    type : 'post',
    data : postData,                
    success : function( resp ) {
        if(resp.is_success){
            $('#div_1').html($('#inner_1' , resp.htmls).html());
            var result=('#div_2').html($('#inner_2' , resp.htmls).html());
            alert(result);
        }else{
            //show the error messages and its htmls return - resp.err_msg
        }
    }
});
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.