【问题标题】:JavaScript print and AJAX save/delete from databaseJavaScript 打印和 AJAX 从数据库中保存/删除
【发布时间】:2015-06-08 09:46:08
【问题描述】:

我创建了这个页面来从数据库中获取数据,并带有链接来打印显示的数据并在之后将其删除。

其中一个问题是 JavaScript 打印函数 window.print(); 不起作用。

还有一个问题是,打印完页面后,我想更新一下数据库,这样大家就可以看到之前已经打印过了。

另外,该功能还可以打印页面,然后立即删除数据,这样人们就不需要查看之前是否打印过。

这是从数据库中获取数据的代码:

<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>

这些是两个服务器端功能的页面:

删除.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`");
?>

【问题讨论】:

  • 什么不起作用?请提供您尝试过的详细信息以进行调试。
  • 请原谅我的写作不足,我在这里很着急,所以我只是打字没有回头。
  • 写一个正确的问题会让你得到比半成品问题更快(更好)的答案。即使“半成品”的问题需要更少的时间来写。
  • 所以这里的问题是我在这段代码上工作了整整一周(别笑,我是初学者和学生)但我根本无法开始工作。打印按钮根本不起作用
  • 这并不违反规则,但是当您不提供最少的问题描述/调试信息时,您不太可能得到解决您问题的答案,只是发布代码并希望解决它的人很少会带来任何答案。

标签: javascript php ajax


【解决方案1】:

你真的应该检查你的浏览器控制台 (F12) 看看是否有任何 JavaScript 错误。

我能发现的一个非常明显的错误是这一行,括号没有闭合。只需先检查控制台即可轻松修复这些类型的错误。

另一个错误是字符串中的变量,它应该作为?key=value对发送。

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

应该是:

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

另一个问题是上面一行调用的 URL。我注意到您提到您的 PHP 文件名为 print_table.php 而不是 printed_table.php

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 2021-10-15
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多