php,但结果必须显示在javascript中


php while but the result have to shows in javascript

我有以下代码,我想做的是用javascript在消息框中显示mysql结果,但当我点击消息框时,它只为每个按钮显示一个结果。

我希望每个按钮都有自己的消息,在数据库中。

有人知道我该怎么做吗?

    <?
    $query = "SELECT *  FROM `Points`";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)){
    ?>
    <div style="position:absolute; overflow:hidden; <?php echo $row[Pos]; ?> height:23px; z-index:0">
<button onClick="showMsgBox();" id="showBtn">Show MsgBox</button>
</div>  
<script>
            $("#showBtn").focus();
            msgBoxImagePath = "images/";
            function showMsgBox() {
                $.msgBox({
                    title: "Ads",
                    content: "<? echo $row[Ctn]; ?>",
                    type: "alert"
                });
            }
        </script>
    <?}?>

在数据库中,Pos是按钮的位置,Ctn是消息。

请帮忙。

试试这个;)

<?php
$query = "SELECT *  FROM `Points`";
$result = mysql_query($query);
/* all messages */
$messages = array();
$index = 0;
while($row = mysql_fetch_assoc($result)){
  $messages[] = $row['Ctn'];
  ?>
  <div style="position:absolute; overflow:hidden; <?php echo $row['Pos']; ?> height:23px; z-index:0">
    <button onClick="showMsgBox(<? echo $index++; ?>);" id="showBtn">Show MsgBox</button>
  </div>
  <?php
}
?>
<script>
  var messages = <?php echo json_encode($messages); ?>;
  $("#showBtn").focus();
  msgBoxImagePath = "images/";
  function showMsgBox(index){
    $.msgBox({
      title: "Ads",
      content: messages[index],
      type: "alert"
    });
  }
</script>

已使用的<?php ... ?>标签,您可以根据需要进行更改;

首先,您的id应该是唯一的,如果您的输入是循环,请确保您的id也是唯一的。请参阅下面的代码,这可能对您有用。

<?
    $query = "SELECT *  FROM 'Points'";
    $result = mysql_query($query);
    $queryCounter = 0;
    $message = array();
    while($row = mysql_fetch_assoc($result)){
    $message[$queryCounter] = $row[Ctn];
    ?>
    <div style="position:absolute; overflow:hidden; <?php echo $row[Pos]; ?> height:23px; z-index:0">
<button onClick="showMsgBox(<?php echo $queryCounter; ?>);" id="showBtn<?php echo $queryCounter; ?>">Show MsgBox</button>
</div>  
<script>
            $("#showBtn").focus();
            msgBoxImagePath = "images/";
            function showMsgBox(id) {
                $.msgBox({
                    title: "Ads",
                    content: "<? echo $message[id]; ?>",
                    type: "alert"
                });
            }
        </script>
    <? $queryCounter ++; }?>

看看这段代码,我希望它能为你工作,这里我使用了data-value html5属性来获得点击按钮时的消息。

<?php 
$query = "SELECT *  FROM `Points`";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
?>
    <div style="position:absolute; overflow:hidden; <?php echo $row['Pos']; ?> height:23px; z-index:0" data-value="<?php echo $row['Ctn']; ?>" id="showMsgBoxContent_<?php echo $row['id'];?>" >
        <button onClick="showMsgBox(<?php echo $row['id']; ?>);" id="showBtn_<?php echo $row['id']; ?>">Show MsgBox</button>
    </div>  
<?php 
}
?>
<script>
msgBoxImagePath = "images/";
function showMsgBox(id) 
{
    $("#showBtn_"+id).focus();
    var showContent = $('#showMsgBoxContent_'+id).getAttribute('data-value');
    $.msgBox({
        title: "Ads",
        content: showContent,
        type: "alert"
    });
}
</script>