+1  A: 

There are many ways to pass data to JavaScript; the easier is the following:

<script>
  aid = <?php print $row->aid; ?>
  // The rest of the script
</script>

I only wrote the necessary code, without even write all the attributes for the tag SCRIPT.

kiamlaluno
are u sure this is correct?i am trying this right now. it doesnt work.
amit
it works. see my answer on this page.
fsb
+1  A: 

First of all, you don't need to echo everything. Something like this is just as valid:

while ($row = mysql_fetch_object($result)) { ?>
  <div id=captionbox style='width: 110px;float:left;color:#FFF;text-align:center;'>
  <a href='#' class='thumb' ><img class='thumb-img' src='images/roadies/th<?=$row->aid?>jpg' /> </a>
<?php ...

In this case, uses php's echo shortcut tag to echo the value of the contents. It is equivalent to <?php echo $some_variable ?> .

Second, I don't actually see any JavaScript code in your question. But to accomplish what it sounds like you want to do, you can make an HTML form with a SINGLE hidden input whose value is set by selecting one of your images, and then have that form submit to user_submit.php.

The value-setting can happen in a number of ways, which is really up to you. But suppose you wanted it to happen when the user clicked an image. Then you could set the onClick event inside the img tag, like onclick="hidden_input.value='<?=$row->aid?>'"

danben
you mean i should put the onclick action onto the anchor tags sorrounding the image? i have updated the code. can you see if i am doing something wrong?
amit
Like danben said, it's hard to say without seeing your JavaScript. If you are submitting your form to JavaScript, you do not need an onlick handler. Echo your variable into the hidden form field value and retrieve with JS.
Scott Radcliff
I see that you've edited your code to use $qid for the value, but I don't see anyplace where $qid is being set. Also, remember to quote your strings (for instance, the values of your 'name' attributes). Please consult an HTML reference for that sort of thing.
danben
Oh, $qid is passed to your page already, it looks like. No, as far as I can tell from your question, you want to select a single row and have its aid submitted to your page, right? In that case, you want to set the value of the hidden input to the selected aid, using onclick or some other method, and then submit that.
danben
+2  A: 

You can write a variable, object or array literal declaration to your javascipt file or to a <script> element in a HTML file, as kiamlaluno correctly said. but i would suggest, (1) use the var keyword and (2) consider using a separate namespace.

If you have several things to tell the JS, it's handy to put them in a PHP hash and json_encode() and then write the encoded string to your output. This takes care of the namespace and correct escaping of the data. For example:

<?php
$jsdata = array(
 'this' => "It\"s gone.\nWhat?",
 'that' => array(1,3,2),
 'another' => 0x2f );
$jsdata = json_encode($jsdata);
?>
<html>
<head></head>
<body>
<script type="text/javascript">
 var thedata = <?php echo "$jsdata;" ?>
 window.console.log(thedata);
</script>
</body>
</html>

Try that and look at the javascript console to see if it works.

EDIT: Another reason I like this approach is because you can use PHP's array handling conveniences to construct an arbitrarily complex $jsdata while not worrying about escaping issues and only consuming one global JS object name.

fsb