-1

I'm trying to POST a variabile "id" to another php page by using Ajax.

In the second php page called "export.php" I need to use this variable to query a MySql table and display the results in a html table.

However it doen't work seems that on export.php the variable isn't set. Can you help me?

<script>
$('#btn_report').click(function(){
    if(confirm("Are you sure?"))  {
    var id = [];
        $(':checkbox:checked').each(function(i){
    id[i] = $(this).val();
    alert (id[i]); //this is correctly printed
       });
        if(id.length === 0)    {
       alert("No record selected");
            }
    else   {                        
       $.ajax({                      
        url:'export.php',
        type:'POST',
        data:{id:id},                                               
        });

          }
    }
    else  {
    return false;
    }
});
</script>

export.php

So the problem is that the "if(isset..." return false and display the else alert on the bottom of this code.

<html lang="en">
  <head>
    <title>Export</title>
  </head>
  <body>
<?php

$connect = mysqli_connect('localhost', 'user', 'pass', 'db' );

    if(isset($_POST["id"])){
        echo 'break1';
        foreach($_POST["id"] as $id) {

            $sel = "SELECT * FROM `user_details` WHERE `Codice prodotto` = $id";
            echo $sel . "<br>";
             $res = mysqli_query($connect, $sel);
                        echo "<table border='1'>
                            <tr>
                        <th></th>
                        <th>Codice prodotto</th>
                        <th>Relazione tecnica</th>
                        <th>Modello offerto</th>
                        <th>Descrizione</th>
                        <th>Immagine</th>
                        <th>Tipologia di Ancoraggio</th>
                        <th>Design</th>
                        <th>Illuminazione</th>
                        <th>FSP</th>
                        <th>DATI TECNICI</th>
                        <th>Dissolvenza delle ombre</th>
                        </tr>";

                        while($row = mysqli_fetch_array($res))
                        {
                        echo '<tr id="' . $row['Codice prodotto'] . '">';
                        echo '<td><input type="checkbox" name="codice[]" class="delete_prodotto" value="' . $row['Codice prodotto'] . '"></td>';
                        echo "<td>" . $row['Codice prodotto'] . "</td>";
                        echo "<td>" . $row['Relazione tecnica'] . "</td>";
                        echo "<td>" . $row['Modello offerto'] . "</td>";
                        echo "<td>" . $row['Descrizione'] . "</td>";
                        echo "<td>" . $row['Immagine'] . "</td>";
                        echo "<td>" . $row['Tipologia di Ancoraggio'] . "</td>";
                        echo "<td>" . $row['Design'] . "</td>";
                        echo "<td>" . $row['Illuminazione'] . "</td>";
                        echo "<td>" . $row['FSP'] . "</td>";
                        echo "<td>" . $row['DATI TECNICI'] . "</td>";
                        echo "<td>" . $row['Dissolvenza delle ombre'] . "</td>";
                        echo "</tr>";
                        }
                        echo "</table>"; 
            }

     }
    else {
        echo "no value";
    }

?>
</body>
</html>

2 Answers2

0

please use type attribute instead of method in ajax method. which is wrong.

$('#btn_report').click(function () {
    if (confirm("Are you sure?")) {
        var id = [];
        $(':checkbox:checked').each(function (i) {
            id[i] = $(this).val();
            alert(id[i]); //this is correctly printed
        });
        if (id.length === 0) {
            alert("No record selected");
        } else {
            $.ajax({
                url: 'export.php',
                type: 'POST',
                data: {id: id},
            });

        }
    } else {
        return false;
    }
});
  • I did it. But nothing. If i open the export.php after the button click i read just my "no value" echo – Mirko Galantucci Aug 31 '19 at 12:39
  • what does `open the export.php..` this means ? – Swati Aug 31 '19 at 13:17
  • It's mean really open a browser tab. My goal is to display in a page the query result. So when i click the submit button i would like to open the export.php in a new tab and see the html with my selected value – Mirko Galantucci Aug 31 '19 at 13:26
  • then don't use `ajax` instead use `
    ` because you don't want to send data back but display that in new page .
    – Swati Aug 31 '19 at 13:35
  • @Swati i have one question rather than this, how you highlight particular text such as
    in comment.
    – Mohit Kumar Aug 31 '19 at 17:34
  • I try to explain better. I have a table that contain a numeric id, one for every row. I would like to select a row by checkbox, click a button and open a new page that display the id of selected row – Mirko Galantucci Sep 22 '19 at 09:37
0

Try this in your export.php file remove all html code because you only echo to send back the result, if you need to add html you must concatenate it with your result and then echo result.

<?php 
if(isset($_POST["id"])){
    echo 'it works';
}else{
    echo 'not worked';
}
?>

and see if you get response back in console, change your ajax code as this for testing

$.ajax({
    url: 'export.php',
    type: 'POST',
    data: {id: id},
    success: function(response) {
        console.log(response);
    }
});

More Explanation when you echo on ajax request, it will send back response to the browser. so what you should do is prepare a result string and store all the data that you need in the response after performing any kind of processes in that string and when you ready to send response you just echo that string and then exit; below i tried to fix your code, hope it helps you understand.

<?php

$connect = mysqli_connect('localhost', 'user', 'pass', 'db' );

if(isset($_POST["id"])){

    $result = '';
//      echo 'break1';
    foreach($_POST["id"] as $id) {

        $sel = "SELECT * FROM `user_details` WHERE `Codice prodotto` = $id";
//          echo $sel . "<br>";
        $res = mysqli_query($connect, $sel);
        $result .="<table border='1'>
                                                        <tr>
                                                <th></th>
                                                <th>Codice prodotto</th>
                                                <th>Relazione tecnica</th>
                                                <th>Modello offerto</th>
                                                <th>Descrizione</th>
                                                <th>Immagine</th>
                                                <th>Tipologia di Ancoraggio</th>
                                                <th>Design</th>
                                                <th>Illuminazione</th>
                                                <th>FSP</th>
                                                <th>DATI TECNICI</th>
                                                <th>Dissolvenza delle ombre</th>
                                                </tr>";

        while($row = mysqli_fetch_array($res))
        {
            $result .= '<tr id="' . $row['Codice prodotto'] . '">';
            $result .= '<td><input type="checkbox" name="codice[]" class="delete_prodotto" value="' . $row['Codice prodotto'] . '"></td>';
            $result .= "<td>" . $row['Codice prodotto'] . "</td>";
            $result .= "<td>" . $row['Relazione tecnica'] . "</td>";
            $result .= "<td>" . $row['Modello offerto'] . "</td>";
            $result .= "<td>" . $row['Descrizione'] . "</td>";
            $result .= "<td>" . $row['Immagine'] . "</td>";
            $result .= "<td>" . $row['Tipologia di Ancoraggio'] . "</td>";
            $result .= "<td>" . $row['Design'] . "</td>";
            $result .= "<td>" . $row['Illuminazione'] . "</td>";
            $result .= "<td>" . $row['FSP'] . "</td>";
            $result .= "<td>" . $row['DATI TECNICI'] . "</td>";
            $result .= "<td>" . $row['Dissolvenza delle ombre'] . "</td>";
            $result .= "</tr>";
        }
        $result. = "</table>"; 
    }

}
else {
    $result = "no value";
}

echo $result;
exit;

?>
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • I edit then code like you write me. When i click the button i see my alert with the value and is correct my nothing happened in console, Then i open export php and the echo showed is "not worked" – Mirko Galantucci Aug 31 '19 at 13:20
  • have you put success in the $.ajax ? – Umer Abbas Aug 31 '19 at 13:22
  • i copied exaclty your ajax string – Mirko Galantucci Aug 31 '19 at 13:28
  • I just created a very basic ajax tutorial for you to understand and learn ajax. https://github.com/UmerAbbas8/Basic-Understanding, We only use ajax if we want to stay on the same page in browser but also want to get or post some data from to or from the server. Have a good day. – Umer Abbas Aug 31 '19 at 14:00
  • Ok thank you. I try to explain better. I have a table that contain a numeric id, one for every row. I would like to select a row by checkbox, click a button and open a new page that display the id of selected row – Mirko Galantucci Sep 22 '19 at 09:38
  • you can do that with jquery or javascript, put button inside the row under `` and get it's id by parent's, parent id search about how to get onclick element parent id, or take a look at this [link](https://stackoverflow.com/questions/10260667/jquery-get-parent-parent-id) and then send that id to any other page using `window.location.replace("http://www.w3schools.com");` – Umer Abbas Sep 22 '19 at 11:43