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>

Recommended Answers

All 3 Replies

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.

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
        }
    }
});

Thanks for giving valuable solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.