How can I make jQuery get the PHP parameter from mysql_fetch_array()? Below is part of my code, any suggestion would be welcomed.

PHP

$query=mysql_query($sql) or die(mysql_error());
    while($list=mysql_fetch_array($query)){
            print"
<div id=\"#$list[id]\" class=\"f\">

jQuery

switch(id) {
case '#$list[id]':

(I'm trying to use Fancybox with this method http://stackoverflow.com/a/7844043/1575921)

share|improve this question

80% accept rate
feedback

2 Answers

up vote 2 down vote accepted

why not use $.ajax() for this?

http://api.jquery.com/jQuery.ajax/

$.ajax({
  url: 'yourphpcode.php',
  success: function(data) {
    // all your $list variables are saved in data
    // so you can work from here with your data array
  }
});

php:

$query = mysql_query($sql) or die(mysql_error());
    while($list = mysql_fetch_array($query)){
            echo $list['id'];
}

It's most likely you get something like this back from php:

1
2
3
4
5
6
7
etc.

with javascript you can split this with:

dataArray = data.split(ā€\nā€œ);

so your final code would be:

 $.ajax({
      url: 'yourphpcode.php',
      success: function(data) {
        dataArray = data.split("\n");
      }
 });
share|improve this answer
feedback

You can not use PHP vars from within your JS Code, it's a completely different environment. The PHP code runs on the server, the JS code in the browser.

You can however use PHP to print the content of a variable, it is possible to 'produce' the JS code in PHP.

echo "<script type=\"text/javascript\">
  var myvar = '".$list[id]."';
</script>";

Afterwards you can use myvar in the JS code

switch(id) {
case myvar:
share|improve this answer
thanks, i will try!!! – user1575921 9 hours ago
This will only work on initialization of your script. When you want to refresh your $list[id]'s later on in the script, this won't work. – Angelo A 9 hours ago
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.