0

I was trying to parse the php data values which inside the while loop to the popup model .

Sample Code

<?php
include("connect.php");

$sql = 'SELECT id FROM products WHERE tag="mixed" ORDER BY id DESC LIMIT 5';

mysql_select_db('levels');
$retval = mysql_query( $sql, $db_server );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
  $id = $row['id'];
  
  ?>

<div onclick="showDialog('#dialog6');show_data();">

  <div id="getid">
    <?php echo $id; ?>
  </div>

</div>

<?php
}
mysql_close($db_server);
?>


<script>
    function showDialog(id){
        var dialog = $(id).data('dialog');
        dialog.open();
    }

    function show_data(){
      var x = document.getElementById("getid").innerHTML;
      document.getElementById("demo").innerHTML = x;
}
</script>
        
<!-- Popup Model -->
<div data-role="dialog" id="dialog6" >
 <div id="demo"> </div>

</div>

Let's say if i have id => 1 to 10 , above code writing last 5 items from the table. which are 6 7 8 9 10 . ( it's working perfectly ) .

my requirement is to when i click the 7 it should parse 7 to the popup model. ( or let's say to the innerHTML of ).

it only parsing the first value ( 5 ) . to all onclick events when i click the each number.

PS : this is test using mysql not mysqli or pdo :) .

Thanks in Advance !

2 Answers 2

1

Try this. Hope it helps.

    $sql = 'SELECT id FROM products WHERE tag="mixed" ORDER BY id DESC LIMIT 5';

    mysql_select_db('levels');
    $retval = mysql_query( $sql, $db_server );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
      $id = $row['id'];

      ?>

    <div onclick="showDialog('#dialog6');show_data(<?php echo $id; ?>);"> <!-- Changed -->

      <div id="getid<?php echo $id; ?>"> <!-- Changed -->
        <?php echo $id; ?>
      </div>

    </div>

    <?php
    }
    mysql_close($db_server);
?>


<script>
    function showDialog(id){
       var dialog = $(id).data('dialog');
       dialog.open();
    }

    function show_data(y){ // Changed
     var x = document.getElementById("getid"+y).innerHTML; // Changed
     document.getElementById("demo").innerHTML = x;
}
</script>

<!-- Popup Model -->
<div data-role="dialog" id="dialog6" >
 <div id="demo"> </div>

</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Mike , Thank you very much for the support given and really appreciate it. It's working perfectly.
can you letting me know how to parse multiple values ? such as id , name and image ?
@RuwanRanganath, See my other answer. If you like my answer, please approve the answer.
1

Try This One.

<?php
    include("connect.php");

    $sql = 'SELECT id FROM products WHERE tag="mixed" ORDER BY id DESC LIMIT 5';

    mysql_select_db('levels');
    $retval = mysql_query( $sql, $db_server );
    if(! $retval )
    {
        die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        $id = $row['id'];

        ?>

        <div onclick="showDialog('#dialog6');show_data(<?php echo $id; ?>,'<?php echo $name; ?>','<?php echo $imgUrl; ?>');"> <!-- Changed -->

        <div id="id<?php echo $id; ?>"> <!-- Changed -->
        <?php echo $id; ?>
        </div>
        <div id="name<?php echo $name; ?>"> <!-- Changed -->
        <?php echo $id; ?>
        </div>
        <div id="image<?php echo $id; ?>"> <!-- Changed -->
            <img src="<?php echo $imgUrl; ?>" />
        </div>

        </div>

        <?php
    }
    mysql_close($db_server);
    ?>


    <script>
    function showDialog(id){
      var dialog = $(id).data('dialog');
      dialog.open();
    }

    function show_data(id, name, imgUrl){ // Changed
        document.getElementById("dispID").innerHTML = id;
        document.getElementById("dispName").innerHTML = name;
        document.getElementById("ImgDisplay").src = imgUrl;
    }
    </script>

    <!-- Popup Model -->
    <div data-role="dialog" id="dialog6" >
    <div id="demo"> <!-- Changed -->
        <div id="dispID"></div>
        <div id="dispName"></div>
        <div id="dispImg"><img id="ImgDisplay" src="" /></div>
    </div>

    </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.