3

I have the following code and what I'm trying to do is to show the mysql result in the message box with javascript, but when I click on the message box it shows me only one result for every button.

I want every button have his own message which is in database.

Does anybody have an idea on how I can do it?

    <?
    $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>

    <?}?>

In the database Pos is the position of the button and Ctn is the message.

Please help.

2
  • did your php support the shorthand tag???? You use it for PHP. <? ?>, try using <?php ?>. Commented Mar 30, 2016 at 6:22
  • if you want to use shorthand you can do it like this <?=$row['ctn'];?> Commented Mar 30, 2016 at 6:23

3 Answers 3

1

Try this ;)

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

Used <?php ... ?> tags you can change as per your need;

Sign up to request clarification or add additional context in comments.

Comments

0

Firstly, you id should be unique, if your input is the loop make sure your id is also unique. Please see code below this might work for you.

<?
    $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 ++; }?>

Comments

0

check out this code, i hope it will work for you, here i have used data-value html5 attribute to get the message when button is clicked.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.