Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    838
    Thanks
    172
    Thanked 90 Times in 90 Posts

    success and returned data question

    Hi guys

    Not sure if this should be in the frameworks forum or not-- so i'll start here.

    I've seen and read so many ways to do ajax that i'm confused.

    What is the correct way to use ---- success: function ()

    SO I tried this and it works but I feel it's incorrect.

    I'm sending a POST query via ajax to my php which simply checks if query exists or not.

    I echo out true or false in php (which I think is wrong but it is working)

    I tried to echo back string data such as "fail" to pass as argument in the success function but that fails which makes me feel i'm doing things wrong.

    1) how does the success function work -- ie what is it expecting back and how do you act on it.

    2) what if I want to return data back from php (I don't in this case but might do later) -- do I use json?

    This is what I have trimmed down slightly of course

    LT


    Code:
    var params = "yrans="+yrAns+"&test="+testname;   
    
         jQuery.ajax({
    
           type: "POST",
    
           url: "WrdCheck.php",
    
           data: params,			 
    
    
           success: function(data){
    
    			if (data){
                jQuery('#results').html('do success html' );
    }else {
                jQuery('#results').html('do fail html' );
      }
    }

    PHP Code:
           PHP
           
    // POST checks done
           
           
    if ($efanswer[$testname] == $answer )
         {
            
    //ok
            
    echo true;
        }
        else
        {
            
    //try again
        
    echo false;
        } 
    Last edited by low tech; 06-18-2012 at 02:42 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    So many things wrong. I humbly suggest you re-train yourself on ajax jquery.
    It's easier to learn starting with a simple $_GET type first: http://www.w3schools.com/jquery/jquery_ref_ajax.asp

    Your php file is totally wrong. First line
    Code:
    if ($efanswer[$testname] == $answer )
    You are using variables that you don't define: $efanswer, $testname, $answer.
    So this dies and never returns anything to you. Lets use a simple
    PHP Code:
    <?php
    echo 'made it';
    ?>
    as the file. This helps in writing the html part of the duo.

    Next, your params is ok IF you define the two variables that it's sending.

    Your return is normally handled
    Code:
    success: function(result){
    result not data.

    Your if statement does not work with what you are returning. But it does tell you if you get something back or not.

    And FYI you can use the '$' instead of the jQuery to call the functions.

    A new HTML file:
    Code:
    <div id="results"></div>
    
    <script src="./lib/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var testname = 'some_name';
    var yrAns = 'It is OK';
    var params = "yrans="+yrAns+"&test="+testname;
         jQuery.ajax({
           type: "POST",
           url: "test.php",
           data: params,
           success: function(result){
    			if (result == 'true'){
                jQuery('#results').html('do success html' );
    			}else {
                jQuery('#results').html('do fail html' );
      			}
    		}
    });
    </script>
    and new called PHP:
    PHP Code:
    <?php
    if ($_POST['test'] == 'some_name' )
    {
        echo 
    'true';
    }else{
        echo 
    'false';
    }
    ?>
    Hope this helps.

  • Users who have thanked sunfighter for this post:

    low tech (06-20-2012)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    838
    Thanks
    172
    Thanked 90 Times in 90 Posts
    Hi sunfighter

    Yeh it helps. Thanks.

    You are using variables that you don't define:
    I think do -- just not posted -- note // POST checks done
    My code works but I didn't understand how success works. So my code
    is working by accident really hahhaa

    Anyway, yeh i'm just learning Ajax stuff.

    So does success: function always expect true or false to be returned?
    Is that how that function works?

    For example what IF I wanted to send back data (in this case i don't, but what if) --- do you still use the success function in such a query? Or do use a different Ajax syntax?

    Hope you understand.

    Just trying to get my head around
    How to use success: and when and when not to use success:

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    So does success: function always expect true or false to be returned?
    No. When I use ajax most of the time the php file it calls querys a database and returns to results. The success: function places the returned information where I want it to go.

    You can call a text file or a html file. Your return could be in json format or html code. It could be an entirely new CSS definition for the browser page.

    Lets simplify with a simple call for information. This time with a button to initiate the request.
    Main program
    Code:
    <div id="results"><h2>Let AJAX change this text</h2></div>
    <button>Change Content</button>
    <script src="./lib/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("button").click(function(){
         $.ajax({
           url: "test.txt",
           success: function(result){
                $('#results').html(result);
    		}});
      });
    });
    </script>
    The test.txt file:
    Code:
    <table border="1">
    	<tr>
    		<td>The</td>
    		<td>world</td>
    		<td>is a</td>
    	</tr>
    	<tr>
    		<td>place</td>
    		<td>of</td>
    		<td>wonder</td>
    	</tr>
    </table>
    OK. we call a txt file which has html code and return over writes the text in a div.
    But we could have called a php file:
    test.php
    Code:
    <?php
    echo "<table>
    	<tr>
    		<td>The</td>
    		<td>world</td>
    		<td>is a</td>
    	</tr>
    	<tr>
    		<td>place</td>
    		<td>of</td>
    		<td>wonder</td>
    	</tr>
    </table>";
    ?>
    And get something that looks the same.
    The called file could have been an html file.

    Please spend some time on w3school with this

  • Users who have thanked sunfighter for this post:

    low tech (06-20-2012)

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    838
    Thanks
    172
    Thanked 90 Times in 90 Posts
    Hi sunfighter

    Excellent. That really helps a lot.

    Please spend some time on w3school with this
    Yeh i've been reading through the W3S as well as other stuff.

    I'm not so sure that the W3S is the most up to date code wise so i'm always hesitant about learning code from the site so i'm cross checking with
    http://api.jquery.com/jQuery.ajax/
    in my effort to learn.

    Will play around with it all a lot more tomorrow.

    Thanks for you input

    appreciated

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    jquery.com is the main authority on jquery and is the definite site. I like w3S cause they have a easy laid back way of bring things into the picture step by step. It's more a teaching site. Just my opinion and not worth a whole lot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •