doukuangxun5382 2015-06-08 09:46
浏览 32

JavaScript打印和AJAX从数据库保存/删除

I have created this page to get data from the database, with links to print the shown data and delete it afterwards.

One of the problems is that the JavaScript print function window.print(); wont work.

Another problem is that after printing the page, I would like to update the database, so people can see that it has been printed before.

Alternatively, the function could also print the page and then immediately deletes data, so people won't need to see if it has been printed before or not.

This is the code for getting the data from the database:

<html>
<header>
<script>
function print_table(id)
{
   //print your document
   window.print();
   //send your data
   xmlhttp=new XMLHttpRequest();
   xmlhttp.open("GET","http://localhost/Stage/printed_table.php" + id;
   xmlhttp.send();
}
</script>
</header>
<body>

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depits";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Query the database
$resultSet = $conn->query("SELECT * FROM orders");

// Count the returned rows
if($resultSet->num_rows != 0){
// Turn the results into an Array
 while($rows = $resultSet->fetch_assoc())
 {
   $id = $rows['id'];
   $naam = $rows['naam'];   
   $achternaam = $rows['achternaam'];
   $email = $rows['email'];
   $telefoon = $rows['telefoon'];   
   $bestelling = $rows['bestelling'];   

   echo "<p>Name: $naam $achternaam<br />Email: $email<br />Telefoon: $telefoon<br /> Bestelling: $bestelling<br /> <a href='delete.php?del=$id'>Delete</a> <input type='button' onclick='print_table($id)' value='Print Table' /> </p>";
 }
// Display the results 
}else{
   echo "Geen bestellingen";
}
?>
</body>
</html>

and these are the pages for the two server-side functions:

delete.php

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depits";

        // Get ID
        $id = $_GET['del'];
        $sql= "DELETE FROM orders WHERE id=" . $id . "";
        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        // Query the database
        $resultSet = $conn->query($sql) or die("Failed".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=http://localhost/Stage%201/bestellingen.php'>";

?>

print_table.php

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "depits";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Query the database
$id = $_GET['id'];
$resultSet = $conn->query("UPDATE `orders` SET `printed` = 1  WHERE `id` = `$id`");
?>
  • 写回答

1条回答 默认 最新

  • duangang79177 2015-06-08 10:00
    关注

    You should really check your browser console (F12) to see if there are any JavaScript errors.

    One really glaring error I could spot is this line, where the brackets aren't closed. These type of errors could be easily fixed just by checking the console first.

    Another error is the variable in the string, it should be sent as a ?key=value pair.

    xmlhttp.open("GET","http://localhost/Stage/printed_table.php" + id;
    

    should be:

    xmlhttp.open("GET","http://localhost/Stage/printed_table.php?id=" + id, true);
    

    Another problem would be the URL the above line is calling. I notice you mentioned that your PHP file name is called print_table.php instead of printed_table.php.

    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度