skip to Main Content

i am trying to send the id to another page then select the data by that id from the other page so it works but for only 1 data like if i have server 1 and server 2 server 1 will work but server 2 wont work can any one tell me what is the problem here is my codes

PHP

<?php
$get = $data->show(" SELECT * FROM servers where film_name='avengers' ");
foreach ($get as $row) {
 $id=$row['server_id'];
 $name=$row['server_name'];
 $link=$row['link'];
 $movie=$row['film_name'];
?>
<button type="button" id="btn"  value="<?php echo $id ?>" class="btn btn-warning"><?php echo "$name"; ?></button> 
<?php } ?>

ajax

    <script type="text/javascript">
    $(document).ready(function (){

        $('#btn').click(function (){

var serverid = $('#btn').val();


$.ajax({
    url:"../../control/operation/view_movie.php",
    method:"POST",
    data:{serverid:serverid},
    success:function(data)
    {
$("#show").html(data);
    }
});

        });
    });
</script>

viewmovie page

    <?php

    $id=$_POST['serverid'];
    $getuser = $data->getdata("SELECT * FROM servers WHERE server_id='$id'");
    $link=$getuser['link'];
    $name=$getuser['film_name'];

     ?>
  <iframe class="embed-responsive-item" src="<?php echo $link ?>" allowfullscreen></iframe>

it is not like when i load the first one the other wont load even if i didnt load the first one the others wont work
thanks for helping
img for detail

2

Answers


  1. You have to check that required params are available on the second page.

    <?php
        if(!empty($_POST['serverid'])){
        $id=$_POST['serverid'];
        $getuser = $data->getdata("SELECT * FROM servers WHERE server_id='$id'");
        $link=$getuser['link'];
        $name=$getuser['film_name'];
    ?>
         <iframe class="embed-responsive-item" src="<?php echo $link ?>" allowfullscreen></iframe>
    
    <?php }else{
    ?>
    <iframe class="embed-responsive-item" allowfullscreen> No data</iframe>
    <?php } ?>
    
    Login or Signup to reply.
  2. The id is a number and you are identifying it as a string in the query.

    So change this: $getuser = $data->getdata("SELECT * FROM servers WHERE server_id='$id'");

    To this: $getuser = $data->getdata("SELECT * FROM servers WHERE server_id=$id");

    just remove the quotations (‘ ‘).


    Solution 2

    For the button use this:

    <button type="button" value="<?php echo $id ?>" class="btn btn-warning" onclick="getmovie(<?php echo $id ?>)"><?php echo "$name"; ?></button>

    where onclick="getmovie(<?php echo $id ?>)" is the funtion to view the movie and we are sending the id as parameter.
    And the ajax is as following:

    <script type="text/javascript">
    
        function getmovie(serverid){
            $.ajax({
            url:"../../control/operation/view_movie.php",
            method:"POST",
            data:{serverid:serverid},
            success:function(data)
                {
                    $("#show").html(data);
                }
            });
        }
        </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search