views:

455

answers:

3

i want to send a php variable $thread_id to php file using jquery ajax, so the php file can get all posts for $thread_id and echo it back to main file.

it doesnt work when i type:

$.get("ajaxcall_reply.php", thread_id: $thread_id, function(data) { $("#threads").html(data); });

how should i type?

A: 

Do you know what $thread_id is outputting? Try putting it into a variable of its own and looking at the output before putting it in the get function. It might have symbols or things that are messing up your javascript syntax. Do you have an example output? Additionally the get method returns the XMLHttpRequest object (data) so you might want to look into setting the type of data to be returned to callback function: "xml", "html", "script", "json", "jsonp", or "text".

Ambrosia
A: 

Try this:

$.get("ajaxcall_reply.php", {thread_id: $thread_id}, function(data) { $("#threads").html(data); });
sheats
you'd actually need to set the variable in javascript first, so`var thread_id = <?= $thread_id ?>`and then use that variable in the ajax call, like so:`$.get("ajaxcall_reply.php", {thread_id: thread_id}, function(data) { $("#threads").html(data); });`
jnunn
A: 
  <script>
  $.get(url, { get_var: '<?php echo $phpvar ?>' }, function(data) { alert(data); });
  </script>

at the URL:

  <?php
  echo $_GET['get_var'];
  ?>
Allyn
obviously, fill in the pieces with your own functionality. I was just showing you the syntax for it.
Allyn
this works but i noticed that the main problem is that the $thread_id is empty.in the mina file it looks like this: $thread_id = $_GET['id']; echo "<script type='text/javascript' src='static/js/reply.js'></script>";but the value of $thread_id doesnt come along when im using your jquery code.
fayer
but i skipped the line: var php = $phpvar;cause then it didnt work.
fayer