0

I make an ajax call with:

index.php

$.ajax({
  type:'post',
  url:'abc.php',
  success:function(returned_data)
  {
     // returned_data  contains HTML + javascript code
     // I'd like to access the javascript variable here
     // refer to abc.php's code to see what variable
     // somehow access "new_variable" here
  }
})

abc.php

<table id="first_table">
 <tr>
   .
   .
   .
  <!-- some un-related td content -->
 </tr>
</table>
<script>
  new_variable = $('#first_table').dataTable();
  // now I want new_variable to be available in index.php page; i.e. inside the success
  // function of the ajax method
</script>

Can this be achieved? Or any alternative to achieve this?

3
  • May be you can try with document.write(new_variable); in your abc.php Commented Jun 24, 2014 at 10:28
  • It might be easier to send JSON data to the server, which would include the variable. Commented Jun 24, 2014 at 10:28
  • Nevermind. I solved it. Using console.log(new_variable) inside success function is giving me the result. Don't know why it didn't work earlier..Thanks for your time though Commented Jun 24, 2014 at 10:45

2 Answers 2

1

You could do something like this instead:

   $.ajax({
        type:'post',
        url:'abc.php',
        success:function(data)
        {
            var new_variable = $(data).find('#first_table').dataTable();
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Updated my question to reflect what the variable exactly contains
I believe that is solution is still possbile asuming that you load dataTable when Index.php is executed.
Nevermind. I solved it. Using console.log(new_variable) inside success function is giving me the result. Don't know why it didn't work earlier..Thanks for your time though
0

Surprisingly, the problem seemed to vanish by itself. I don't know what minor change I did or didn't, but simply using:

$.ajax({
  type:'post',
  url:'abc.php',
  success:function(returned_data)
  {
     console.log(new_variable);
  }
})

printed the result appropriately...which didn't happen earlier before asking this question. What's more is since it is global variable, I could access it outside this ajax function too xD

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.