【问题标题】:Twice page refresh is needed to get the input data from MySQL database需要两次页面刷新才能从 MySQL 数据库中获取输入数据
【发布时间】:2015-06-12 22:22:21
【问题描述】:

当我在表单输入字段中输入新值并提交它们时,需要重新加载两次页面才能获取新值。

就像你可以在第 7 行看到 $balance_2 一样。这一行对每一行的所有行值求和,并将值记录在每一行的Balance 列中,按下提交按钮后,它会转到 update_2.php 文件以更新数据库,然后点击

<a style='left: -18%; top: 100%; position: absolute; color: white; font-family: Roboto, helvetica, arial, sans-serif;  width: 170px; font-weight: 600;' href='index_2.php'>Click here to go back</a>

update_2.php 文件中的按钮并返回主页面然后表单值不会更新,直到我再次重新加载它。

我希望当我点击Click here to go back 按钮时,应该会显示新值,并且一定不需要重新加载页面两次。

这是我的代码

$id_2 = $row['ID'];
$Budget_2 = $row['Budget'];
$Availed_in_Regions_2 = $row['Availed_in_Regions'];
$Requested_in_KBL_2 = $row['Requested_in_KBL'];
$Received_in_KBL_2 = $row['Received_in_KBL'];
$Availed_in_KBL_2 = $row['Availed_in_KBL'];

$balance_2 = $Availed_in_Regions_2 + $Requested_in_KBL_2 + $Received_in_KBL_2 + $Availed_in_KBL_2;
$con2->query("UPDATE Office_Operations f1, (SELECT SUM(balance) AS bal FROM Office_Operations ) f2 SET ytotal6_2 = bal WHERE f1.id = 1;");
$con2->query("UPDATE Office_Operations SET Balance = $balance_2 WHERE id = $id_2");


echo "<div class='calc_container'"; if($row['ID']==1) echo " style='margin-bottom:40px;'"; echo "> 

    <input type='hidden' class='id_3' name='id[]' value='".$row['ID']."'>

    <input type='text' class='budget_3' name='Budget[]' value='".$row['Budget']."'>

    <input type='text' class='avail_region_3' name='Availed_in_Regions[]' value='".$row['Availed_in_Regions']."'>

    <input type='text' class='req_kbl_3' name='Requested_in_KBL[]' value='".$row['Requested_in_KBL']."'>

    <input type='text' class='rec_kbl_3' name='Received_in_KBL[]' value='".$row['Received_in_KBL']."'>

    <input type='text' class='avail_kbl_3' name='Availed_in_KBL[]' value='".$row['Availed_in_KBL']."'>

    <input type='text' class='balance_3' name='Balance[]' value='".$row['Balance']."'>

    </div>";}

【问题讨论】:

  • 您的 update_2.php 文件是什么样的? “点击返回”的锚标记在哪里直播?
  • 这是update_2.php,点击返回更新_2.php。

标签: javascript php jquery html mysql


【解决方案1】:

您的列表页或主页是index_2.php,而您的update_2.phpClick here to go back 按钮所在的位置。

总结:

index_2.php

  • 数据列表
  • 还有表单所在的位置

update_2.php

  • Click here to go back 按钮所在的位置

当数据从您的index_2.php 提交时,它会转到update_2.php,但什么都不做,只提供返回按钮。

UPDATE query 运行的唯一时间是用户单击 Click here to go back 按钮时。

解决方案:

  • 将您的UPDATE query 放入您的update_2.php
  • 查询后使用header()函数将用户重定向回index_2.php

示例代码:

index_2.php:

<form action="update_2.php" method="POST">

  <!-- INSERT HERE YOUR INPUT FIELDS -->
  <input type="submit" name="submit">
</form>

update_2.php:

<?php

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

    /* INSERT HERE YOUR UPDATE QUERIES */
    header("LOCATION:index_2.php"); /* REDIRECT USER BACK TO index_2.php */

  } /* END OF ISSET SUBMIT */

?>

【讨论】:

  • 抱歉误会这是update_2.php,点击返回更新_2.php。
【解决方案2】:

从我的角度来看:

  1. 将所有input 标记放在使用action=""form 标记内(到同一个URL)

  1. 使用 AJAX 处理请求。当单击锚点a 时,使用onclick 然后定义进行AJAX 调用的侦听器函数,并在成功响应时更新input 字段(相应地解析响应)。见using JSjQuery

另外,最好对查询输入值进行转义。对于 MySQL,请参阅一些帖子 hereherehere

【讨论】:

  • 在此处查看 input 代码 sn-p:codepad.org/gK6icyD3。我所做的只是将输入包装在表单中,当按下按钮(新创建的)时,将请求发送到 _POST 变量中的当前 URL。
  • 我更换了它,但没有任何改变
猜你喜欢
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 2022-07-26
  • 2013-07-18
  • 1970-01-01
  • 2021-07-05
  • 2019-09-29
  • 2017-05-02
相关资源
最近更新 更多