0

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>
5
  • 1
    google://jquery ajax, there are good examples for this simple task, I'm sure you can understand and use them if you wrote this script Commented Jul 23, 2016 at 18:04
  • 1
    Sorry, but if I ask this, it is that it is not that obvious for me ;) Thanks for your consideration anyway. FYI : Googling has been an option for the last hour before bothering you, strangeqargo. Commented Jul 23, 2016 at 18:05
  • Do you know how to do this without AJAX? Commented Jul 23, 2016 at 18:06
  • @strangeqargo : many thanks - but this is exactly where I copied/paste this example from (including the zip file) - the point where i'm lost is how can I link buttons with ids to an update? Commented Jul 23, 2016 at 18:12
  • the server-side script you call with ajax must update the database with parameters you pass to it. your task is just to provide correct data and url to jquery.ajax call. Commented Jul 23, 2016 at 18:14

1 Answer 1

1

Give same class for all the buttons

then

    <script>
        $(".laurent").click(function(){

        $.ajax({
           url : "display-ajax.php",
           type : "POST", // Le type de la requête HTTP, ici devenu POST
           data:{"id":$(this).attr('id'),"mark":"8","name":"myname","class":"myclass"},
           success:function(data){

           }

  });

    });
    </script>

In display-ajax.php

<?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); 
?>

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Unfortunately. I was not very successful. I updated the code with my html page so you can see what I am trying to achieve.
@Laurent, i have changed my answers check it once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.