使用ajax脚本运行php脚本

时间:2016-07-23 18:00:52

标签: php ajax

下午好,我有一个PHP代码,我想从html页面中的ajax函数调用,我有几个按钮。根据显示的按钮ID(id1,id2,id3,...),我想触发这种更新脚本。你能帮我提供一些帮助吗?提前谢谢了。最重要的是让我了解如何使用onclick事件更改数据库中的值。

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

我的HTML代码:

<!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>

1 个答案:

答案 0 :(得分:1)

为所有按钮提供相同的课程

然后

    <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>

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

希望它有所帮助。