【问题标题】:Needing to send a post using AJAX and receive the POST on the other page需要使用 AJAX 发送帖子并在其他页面上接收 POST
【发布时间】:2016-12-25 03:51:03
【问题描述】:

我之前有这个工作,但我必须更改了某些内容,并且实际上不确定我更改了什么,因为代码对我来说看起来不错,但是嘿,从其他人的角度来看,发现问题会更容易。

我正在尝试编辑连接到我的表的表中的信息。当他们单击该行中的文本时,该信息是可编辑的。当他们点击文本区域外的任何地方时,它会执行命令并转到我的其他文件,该文件接收任何帖子发送我的第一个主页。

这是我的代码:

主索引页面 (AJAX)

        function showEdit(editableObj) {
            $(editableObj).css("background","#FFF");
        } 

        function saveToDatabase(editableObj,column,id) {
            $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
            $.ajax({
                url: "includes/saveedit_members.php",
                type: "POST",
                data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
                success: function(data){
                    $(editableObj).css("background","#FDFDFD");
                    window.location.replace("admin_members.php");
                }        
           });
        }

主索引页面 (HTML)

<tbody>

<?php
foreach($faq as $k=>$v) {
?>
<tr class="table-row" style="text-align: left;" id="no_enter">
<td style="font-size: 11px;"><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $faq[$k]["id"]; ?>"></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'username','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["username"] != '' ? $faq[$k]["username"] : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'email','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["email"] != '' ? $faq[$k]["email"] : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'cpukey','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["cpukey"] != '' ? $faq[$k]["cpukey"] : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'ip','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["ip"] != '' ? $faq[$k]["ip"] : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'time','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["time"] != '' ? $faq[$k]["time"] : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'enabled','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["enabled"]  == 1 ? 'Yes' : 'No'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'activ_status','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["activ_status"]  == 1 ? 'Yes' : 'No'; ?></td>
<td style="font-size: 11px;" contenteditable="false" onBlur="saveToDatabase(this,'profile_picture','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["profile_picture"] != '' ? '<img src="'.$faq[$k]["profile_picture"].'" style="height: 20px; width: 20px;">' : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'userLevel','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo ($faq[$k]["userLevel"] == 1 ? '<img src="images/member_rank.png" style="height: 18px; width: 18px;" > Member' : ($faq[$k]["userLevel"] == 2 ? '<img src="images/staff_rank.png" style="height: 20px; width: 20px;"> Staff' : ($faq[$k]["userLevel"] == 3 ? '<img src="images/admin_rank.png" style="height: 20px; width: 20px;"> Admin' : 'Unknown'))) ?></td>
<td style="font-size: 11px;" contenteditable="false" onBlur="saveToDatabase(this,'register_time','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["register_time"] != '' ? $faq[$k]["register_time"] : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'account_credits','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["account_credits"] != '' ? number_format($faq[$k]["account_credits"], 2, '.', '') : 'None'; ?></td>
<td style="font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'free_gifted_credits','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo number_format($faq[$k]["free_gifted_credits"], 2, '.', '') != '' ? number_format($faq[$k]["free_gifted_credits"], 2, '.', '') : 'None'; ?></td>
</tr>
<?php
}
?>

</tbody>

那么对于我接收代码的页面(saveedit_members.php)

<?php

if(!isset($POST['column']) || $_POST["editval"] || $_POST["id"]) {
    header('Location: error-pages/index.php');
} else if(isset($POST['column']) & $_POST["editval"] & $_POST["id"]) {
    include_once('../configuration/db.php');
    $result = mysqli_query($con, "UPDATE users set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  id=".$_POST["id"]);
}

?>

我们将不胜感激!

【问题讨论】:

  • 您是否真的将未经处理的 POST 数据直接存储在数据库中?那是中世纪的。
  • 是的。什么意思?
  • 您面临什么问题?
  • $POST['column'] 应该是 $_POST['column']
  • @NanaPartykar 我的页面只是重新加载并且什么都不做。通常它会用新的编辑信息更新数据库。但这只是在页面被编辑之前用相同的信息刷新页面。

标签: php ajax post


【解决方案1】:

1)改变

if(!isset($POST['column']) || $_POST["editval"] || $_POST["id"]) {

到

if(!isset($_POST['column']) || !isset($_POST["editval"]) || !isset($_POST["id"])) {

2)改变

else if(isset($POST['column']) & $_POST["editval"] & $_POST["id"]) {

到

else if(isset($_POST['column']) && isset($_POST["editval"]) && isset($_POST["id"])) {

更新代码

<?php
if(!isset($_POST['column']) || !isset($_POST["editval"]) || !isset($_POST["id"])) {
    header('Location: error-pages/index.php');
} else if(isset($_POST['column']) && isset($_POST["editval"]) && isset($_POST["id"])) {
    include_once('../configuration/db.php');
    $result = mysqli_query($con, "UPDATE users set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  id=".$_POST["id"]);
}
?>

【讨论】:

  • 如果有帮助的话。那么,请不要忘记将此答案标记为正确答案。因为,它将帮助其他用户轻松找到此答案。 继续编码 @BenZa。如果您不知道如何标记答案。请通过此链接meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多