Home > Blockchain >  Making a html alert using ajax, sql and php
Making a html alert using ajax, sql and php

Time:02-01

I am trying to make some code where a click of a SQL html button can display a different SQL table as a popup. I have already gotten the variable from the table to pass through using this:

echo "<td><a class='btn-floating btn-large waves-effect waves-light black' onclick='partSearch(".$product.")' value='display'><i class='material-icons'>search</i></a></td>";

The 'part search' code is as follows:

    <script type="text/javascript"> 
        function partSearch() {
            $.ajax({
            url: 'serv.php?id=<?php echo $product ?>',
            type: 'GET',
            success: function(result){
                var obj = jQuery.parseJSON(result)
                alert(obj)
            }
            })
        }
    </script> 

Even though the variable is passed through to 'serv.php', I can't manage to get the sql data to be returned as a popup using alert. All I get is either nothing or [object, Object]. This is the SQL/php code:

<?php   
    include 'includes/dbh.inc.php';
    $id = $_GET['id'];
    $result = mysqli_query($conn,"SELECT * FROM pr WHERE product_ID='".$id."'");// test this

    $rows = array();
    while($r = mysqli_fetch_assoc($result)){
        $rows[] = $r;
    }


    echo json_encode($rows);
    ?>  

Any help is appriciated

CodePudding user response:

    <script type="text/javascript"> 
            function partSearch() {
                var product_id = "<?php echo $product; ?>";
                $.ajax({
                    url: 'serv.php?id=product_id',
                    type: 'GET',
                    success: function(result){
                      alert(result);
                    }
                })
            }
   </script> 
  •  Tags:  
  • Related