Ben Za Ben Za - 3 months ago 12
Ajax Question

Needing to send a post using AJAX and receive the POST on the other page

I had this working before but I must have changed something and not actually sure what I changed because the code looks fine to me but hey, from other peoples perspectives, it will be easier to spot the issue.

I am trying to edit the information in my table that's connected to my table. The information is editable when they click the text in that row. When they click anywhere outside the text area, it executes the command and goes to my other file that receives any Posts send my the first primary page.

Here is my code :

Main Index page (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");
}
});
}


Main Index page (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>


Then for my page that receives the code (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"]);
}


?>

Any help will be MUCH appreciated !

Answer

1) Change

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

To

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

2) Change

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

To

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

Updated Code

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