2016-07-23 15 views
0

午後、htmlページのajax関数から呼び出すphpコードがあります。ここにはいくつかのボタンがあります。表示されたボタンID(id1、id2、id3、...)に応じて、この種の更新スクリプトを起動したいと思います。あなたは私にこのことについて何か助けてくれますか?事前に多くの感謝。最も重要なのは、onclickイベントを使用してデータベースの値を変更する方法を理解することです。ajaxスクリプトを使用してPHPスクリプトを実行する

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

google:// jquery ajaxこの簡単なタスクの良い例がありますが、あなたがこのスクリプトを書いた場合に理解して使用することができます。 – strangeqargo

+1

申し訳ありませんが、私がこれを尋ねるなら、それはそうではありませんとにかくあなたの配慮に感謝します。参考までに:あなたに迷惑をかける前に、グーグルが最後の1時間の選択肢だった、strangeqargo。 – Laurent

+0

AJAXなしでこれを行う方法を知っていますか? –

答えて

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> 

に同じクラスを与えます。 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); 
?> 

希望します。

+0

ありがとうございました。残念ながら。私はあまり成功していませんでした。私はあなたの達成しようとしているものを見ることができるように私のHTMLページでコードを更新しました。 – Laurent

+0

@Laurent、私は私の答えを一度チェックして変更しました –

関連する問題