Good afternoon, I have a php code that I would like to call from an ajax function in an html page where I have a couple of buttons. Depending on the button ID displayed (id1, id2, id3, ...), I would like to trigger this kind of update script. Could you provide me with some assistance on this, please? Many thanks in advance. Most important is for me to understand how I can change a value in the database using an onclick event.
My php code :
<?Php
$id=$_POST['id'];
$mark=$_POST['mark'];
$name=$_POST['name'];
$class=$_POST['class'];
$message=''; //
$status='success'; // Set the flag
//sleep(2); // if you want any time delay to be added
if($status<>'Failed'){ // Update the table now
//$message="update student set mark=$mark, name
require "config.php"; // MySQL connection string
$count=$dbo->prepare("UPDATE student2 set mark=:mark,name=:name,class=:class WHERE id=:id");
$count->bindParam(":mark",$mark,PDO::PARAM_INT,3);
$count->bindParam(":name",$name,PDO::PARAM_STR,50);
$count->bindParam(":class",$class,PDO::PARAM_STR,9);
$count->bindParam(":id",$id,PDO::PARAM_INT,3);
if($count->execute()){
$no=$count->rowCount();
$message= " $no Record updated<br>";
}else{
$message = print_r($dbo->errorInfo());
$message .= ' database error...';
$status='Failed';
}
}else{
}// end of if else if status is success
$a = array('id'=>$id,'mark'=>$mark,'name'=>$name,'class'=>$class);
$a = array('data'=>$a,'value'=>array("status"=>"$status","message"=>"$message"));
echo json_encode($a);
?>
My html code:
<!DOCTYPE html>
<html>
<body>
<button class="laurent" id="1" type="button">Click Me!</button>
<button class="laurent" id="2" type="button">Click Me!</button>
<button class="laurent" id="3" type="button">Click Me!</button>
<script>
$("laurent").click(function(){
$.ajax({
url : "display-ajax.php",
type : "POST", // Le type de la requête HTTP, ici devenu POST
data:{"id":button_id,"mark":"8","name":"myname","class":"myclass"}
dataType : "html" });
});
</script>
</body>
</html>