2016-08-18 90 views
2

我之前有這個工作,但我必須改變一些事情,而不是真的知道我改變了什麼,因爲代碼對我來說很好,但嘿,從其他人的角度來看,發現問題會更容易。需要使用AJAX發送帖子並在其他頁面上接收POST

我想編輯我的表中連接到我的表的信息。這些信息在點擊該行中的文本時是可編輯的。當他們點擊文本區域以外的任何地方時,它會執行該命令並轉到另一個接收任何帖子的文件發送我的第一個主頁面。

這裏是我的代碼:

主索引頁(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"]); 
} 

?>

任何幫助將非常感激!

+0

是你真正直接存儲在數據庫中unsanitized POST數據?那是中世紀的。 – Cagy79

+0

是的。你什麼意思 ? –

+0

你面臨的問題是什麼? –

回答

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"]); 
} 
?> 
+0

如果有幫助。然後,請不要忘記標記此答案爲正確答案。因爲它將幫助其他用戶輕鬆找到答案。 *保持編碼* @BenZa。如果你不知道如何標記答案。請通過此鏈接http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

相關問題