When i click a button

<input id='delete' type='button' name='delete' value='Delete' onclick='deletePost(<? echo $id;?>);'>

I want it to go to this function which is sent to my delete php code

function deletePost(id){
	$('.content-num' + id).hide('slow');
			$.ajax({
				type: "POST",
				url: "/func/delete.php",
				data: "id="+ id,
				success: function(){
			}});
			alert(id);
	}

Im new to javascript and ajax so i think im doing that wrong but im not sure if i made a stupid mistake on my php

<?
include ("../css/mysql.php");
session_start();
$id = htmlspecialchars(trim($_POST['id']));

$query= mysql_query("SELECT id_user FROM content WHERE id=$id");
$row= mysql_fetch_assoc($query);
$id_user= $row['id_user'];

if ($id_user==$_SESSION['iden'])
{
	$delete=mysql_query("UPDATE content SET delete='1' WHERE id=$id");
}
?>

Dani AI

Generated

Short summary: the immediate problem was the column name collision that stopped the UPDATE from running; fixed it by renaming the column and confirmed that solved the issue. As recommended, capturing the server response is the right first step when an AJAX call looks like it worked on the client but the server-side change didn’t happen.

A few follow-up tips and safer patterns to avoid the same trap later:

  • Don’t use reserved-word-like names for columns; prefer is_deleted or deleted_at for clarity. Quoting identifiers works, but renaming is simpler and clearer for maintenance.
  • Move away from the old mysql_* API — use PDO or mysqli with prepared statements and server-side validation to prevent SQL injection and handle errors reliably.
  • During debugging enable error reporting on the server and check the browser Network tab (and console) instead of relying only on alerts.

Example snippets (illustrative only):

ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
$pdo = new PDO(..., [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
$stmt = $pdo->prepare('UPDATE content SET is_deleted = 1 WHERE id = :id AND id_user = :user');
$stmt->execute([':id' => $id, ':user' => $_SESSION['iden']]);
header('Content-Type: application/json');
echo json_encode(['success' => (bool)$stmt->rowCount()]);

On the client side prefer returning structured JSON and only update the UI after a confirmed success:

$.post('/func/delete.php', { id: id }, function(res) {
  if (res.success) $('.content-num' + id).fadeOut();
  else console.error('Delete failed', res);
}, 'json').fail(function(xhr){ console.error('Server error', xhr.statusText); });

Checklist: inspect Network request/response, enable PHP errors while developing, validate/cast the incoming id, use prepared statements, return JSON with success/error, and update the UI only after a confirmed server success. This avoids masked failures and keeps client and server state consistent.

Recommended Answers

All 4 Replies

So, before commenting on any particular code, what exactly is your problem? What happens right now when you click the button?

Get a response from your php code to debug:

function deletePost(id){
	$('.content-num' + id).hide('slow');
			$.ajax({
				type: "POST",
				url: "/func/delete.php",
				data: "id="+ id,
				success: function(xhr){
alert(xhr.responseText);
			},
error: function(xhr){
alert(xhr.responseText);
}
});
			alert(id);
	}

Make sure there is a response in your php code

<?
include ("../css/mysql.php");
session_start();
$id = htmlspecialchars(trim($_POST['id']));

$query= mysql_query("SELECT id_user FROM content WHERE id=$id");
$row= mysql_fetch_assoc($query);
$id_user= $row['id_user'];

if ($id_user==$_SESSION['iden'])
{
	$delete=mysql_query("UPDATE content SET delete='1' WHERE id=$id");
echo('success');
}else{
echo( 'selected user : '.$id_user.', session'.$_SESSION['iden']);  
}
?>

I used that method and i figured it out.

$delete=mysql_query("UPDATE content SET delete='1' WHERE id=$id");

It didn't like me Updating a column named delete so i changed the name and it fixed it.

good to hear. jQuery generally won't throw errors unless you ask it to.

You should probably mark this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.