2016-07-23 2 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 시간 동안 선택 사항이었습니다. – 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, 내 대답을 한 번 확인해 봤습니다. –

관련 문제