0

I have this ajax code

$.ajax(
{
   type: "POST",
   url: "getData.php",
   data: ValueToPass,
   cache: false,
   success: function(html)
   {
      LastDiv.after(html);
   }
});

I am new with this Ajax thing.
This code is to load getData.php file and send variables through type POST. The variables are in this var ValueToPass = "lastid="+LastId+"&br="+br;.
Other thing this code does is return the getData.php's HTML after loading.
Probably with this. success: function(html)

How can I return this $br variable from getData.php after loading, so I can use it again through the next cycle. Cuz what happens here is that I can put the variable in the getData.php with the Ajax and working with it, but when the file getData.php is loaded, outside this file, the variable is not known(not declared). And I'm losing the counting :S

I want to return the HTML and the variable.

4 Answers 4

1

You can return json data in your php file like

$response = array ('br'=> $br, 'html'=> $html);
echo json_encode($response);

Here both html and data are returned. And this to use it in your ajax callback :

success: function(data)
{
    br = data.br;

    LastDiv.after(data.html);
}
Sign up to request clarification or add additional context in comments.

1 Comment

In the getData.php file, after getting the result for $br I put $data = array('br'=> $br, 'html'=>$html); echo json_encode($data); like you said. In the ajax I added dataType: "json", and change function(html) to function(data), but I still can't get the data.br and data.html. I'm not getting any result at all. When I posted the question, the function was working, returning the html from getData.php. But now it doesn't giving anything at all.
0

I'd consider setting a Session variable with the value from the $br variable passed via AJAX. Then when you call getData.php from another file or location, you can use the Session variable since session variables retain their value anywhere in the session.

Comments

0

You can try this to get the data from your `getData.php' :

$.ajax(
{
   type: "POST",
   url: "getData.php",
   data: { ValueToPass: ValueToPass},
   cache: false,
   success: function(data)
   {
      LastDiv.html(data);
   }
});

and in your getData.php you have to pass ValueToPass

maybe like this:

$ValueToPass = mysqli_real_escape_string($db, $_POST['ValueToPass']);

Comments

0

If I understand your question correctly, and if you want to return the $br variable then include it in a JSON object in the successs callback function. So, something like this (I'm not familiar enough with PHP so my PHP syntax might be incorrect):

    // create JSON object
    <?php
        $result = array('br' => $br, 'html' => 'htmlContent);

    echo json_encode($result);
    ?>

    // return JSON object
    $.ajax(
    {
       type: "POST",
       url: "getData.php",
       data: ValueToPass,
       cache: false,
       success: function(result)
       {
          var $br = result.br;
          LastDiv.after(result.html);
       }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.