简体   繁体   English

自动刷新DIV标记内的PHP中的表单不起作用

[英]Form in a PHP inside auto-refresh DIV tag won't work

I have the following two pages. 我有以下两页。 The index.php includes purchases_pending.php through PHP. index.php包括通过PHP的Purchases_pending.php。 The first time the page loads the form works perfectly but when the DIV(#pp) is refreshed after 10 seconds the FORM does not work. 页面第一次加载该表单时效果很好,但是在10秒钟后刷新DIV(#pp)时,该表单不起作用。 Is there a solution for this? 有解决方案吗? Or if I can do it through AJAX, can somebody help me with the code. 或者,如果我可以通过AJAX做到这一点,那么有人可以帮助我编写代码。 As you have seen my codes you'll probably know Im a beginner. 如您所见,我可能会认识Im。 Thanks in advance. 提前致谢。

index.php index.php

<script type="text/javascript">
function update(){
    $('#pp').load('/status/purchases_pending.php');
}
setInterval( update, 10000 );
</script>
<div class="content">
<div class="content_left">
<p>
<h3>Pending Purchases</h3>
<div class="user_content"><div id="pp"><?php include ($purchases_pending); ?></div></div>
</p>

purchases_pending.php Purchases_pending.php

<?php
$CONFIG = $_SERVER["DOCUMENT_ROOT"]. "/config/";
include($CONFIG. "pages.php");
include($db);

$sql = "SELECT * FROM account WHERE status='Pending' AND type='Purchase'";
$query = mysqli_query($conn, $sql);

if (mysqli_num_rows($query) > 0) {

echo '<table>';
echo '<tr> <th align="left">Party</th> <th>Box</th> <th>Rate</th> <th>Status</th> </tr>';
while ($result = mysqli_fetch_assoc($query)) {

$id = $result['id'];
$party = $result['party'];
$box = $result['box'];
$rate = $result['rate'];
$amount = $result['amount'];
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>' . $party . '</td>';
echo '<td align="center">' . $box . '</td>';
echo '<td align="right">' . $rate . '</td>';
echo '<td align="center"><input type="checkbox" name="status[]" id="status[]" value="' . $id . '" /></td>';
echo '</tr>';
}
echo '</table>';
echo '<input type="submit" name="submit" value="Completed" class="input" />';
echo '</form>';
}
else {
    echo 'Hooray...No pending purchases';
}

if(isset($_POST['submit']) && $_POST['submit']!="") {
$status = $_POST['status'];
$count = count($_POST['status']);
for($i=0;$i<$count;$i++) {
$update_purchases = "UPDATE account SET status='Completed', user_confirm='$user_confirm' ,user_confirm_time=current_timestamp WHERE id='$status[$i]'";
$query_purchases = mysqli_query($conn, $update_purchases);
}
header ('location:/');
}
?>

JS load needs to be in document ready in order to work. JS加载需要准备好文件才能正常工作。

Short hand is 短手是

$(function() { 

    var update = function() {              

       $('#pp').load('/status/purchases_pending.php');

       setTimeout(update, 10000);
    }

    update();

});

Move this code to index.php as this no longer exist in the page because it gets removed when replacing div #pp 将此代码移至index.php,因为该代码在页面中不再存在,因为替换div #pp时它将被删除

if(isset($_POST['submit']) && $_POST['submit']!="") {
    $status = $_POST['status'];
    $count = count($_POST['status']);
    for($i=0;$i<$count;$i++) {
    $update_purchases = "UPDATE account SET status='Completed', user_confirm='$user_confirm' ,user_confirm_time=current_timestamp WHERE id='$status[$i]'";
    $query_purchases = mysqli_query($conn, $update_purchases);
}

To explain php renders from the server as html, it reads top to bottom. 为了将来自服务器的php渲染解释为html,它从上到下读取。 So when the js inserts it, it is not inserting the code, but rather the rendering of the code. 因此,当js插入它时,它不是在插入代码,而是在插入代码。 Ajax is used to send data, but on load no data is being sent other than give me a form. Ajax用于发送数据,但在加载时,除了给我提供表格外,没有其他数据正在发送。 The post doesn't exist because it is already rendered. 该帖子不存在,因为它已被渲染。 When using an include it is "inserted" as part of the code to be executed and thus works. 当使用include时,它将作为要执行的代码的一部分“插入”,因此可以正常工作。

load the JS function outside the jquery 在jQuery外部加载JS函数

<script>
  function update(){
    $('#pp').load('/status/purchases_pending.php');
  }
</script>

 $( document ).ready(function() {   
    setInterval( update, 10000 );

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM