开发者

how to collaborate between javascript and php

i am calling a javascript for image gallery and php on a single page. let me show you some code to make things clear:

PHP

echo "<form method = post action = 'user_submit.php'开发者_开发问答>";

// get possible answers using question ID
$query = "SELECT aid, atitle FROM answers WHERE qid = '$qid' ORDER BY aid ASC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());

if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_object($result)) {
echo "<div id=captionbox style='width: 110px;float:left;color:#FFF;text-align:center;'>";
echo "<a href='#' class='thumb' ><img class='thumb-img' src='images/roadies/th".$row->aid.".jpg' /> </a>";
echo "<input  type = hidden name = aid id = rd".$row->aid." value = ".$row->aid.">".$row->atitle."</input>";
        echo "</div>";
                    }

echo "<input type = hidden name = qid value = '".$qid."'>";
echo "<br/>";
echo "<input type = submit name = submit value = 'Vote!'>";
                }

  echo '</form>';

}

the above code fetches object ids and places jpeg images accordingly (thumbnails). now when i click on these thumbnails the javascript opens an overlay to display the large version. i want to pass the value of $row->aid to javascript.

then from the javascript i want to fill out a form and pass the $row->aid and the form to user_submit.php to add it to the DB.

i am new to php. please help me out.


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.


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?>'"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜