[英]Ajax simple call doesn't work
我是第一次使用AJAX,我正在努力使其工作...在我看来,我有一个由控制器生成的表。 查看(vueConsigne.php)
<tbody>
<?php echo $tab_plan; ?>
</tbody>
控制器(control_vueCsn.php):
foreach($lesPlanifs as $laPlanif){
$tab_plan.= "<tr><td>".$laPlanif->getClass().
"</td><td>".$laPlanif->get("dateHeureDebut")->format('d-m-Y H:i:s').
"</td><td>".$laPlanif->get("dateHeureFin")->format('d-m-Y H:i:s').
"</td><td>"." ".
"</td><td>"."Recurrence : ".$val. " " .$unit .
"</td><td>"."n/c".
"</td><td><button name=\"suppPlaniSusp\" onclick=\"call_supp_bdd(".$laPlanif->get("id").",".$laPlanif->getClass().")\"><img src=\"../img/close_pop.png\" id=\"suppPlanif\" name=\"suppPlanBtn\" width=\"30\" height=\"30\"></button></td></tr>";
正如您在最后一行看到的那样,该生成的表具有一个按钮,单击该按钮时,我想在其上触发我的Ajax函数(call_supp_bdd)。 我想传递给函数2参数,即与表行相对应的对象的类名和ID。 这是vueConsigne.php中的ajax函数:
function call_supp_bdd(int,c)
{
if (window.XMLHttpRequest) // Objet standard
{
xmlhttp = new XMLHttpRequest(); // Firefox, Safari, ...
}
else // Internet Explorer
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","../control/suppPlanif.php?q="+int+"&c="+c,true);
xmlhttp.send();
}
最后是我的Ajax(suppPlanif.php)调用的PHP文件:
<?php
/**
* Created by PhpStorm.
* User: ymakouf
* Date: 07/08/2015
* Time: 10:14
*/
$q = intval($_GET['q']);
$c = intval($_GET['c']);
$cnx = new CNX();
$dbh = $cnx->connexion();
$req = $dbh->prepare("DELETE * FROM".$c."WHERE id =".$q);
$req->execute();
?>
我只是尝试用此说明替换它
<?php
echo "sucess";
?>
但这是行不通的。
当您加载jQuery时,请使用jquery的简单语法。 不要手动进行。
$.ajax({
url: "test.html",
type: "GET"
}).done(function( response ) {
alert( response );
});
您对响应不做任何事情 ,需要添加一个onload
回调来处理您得到的响应。
您可以按照以下方式进行操作:
function call_supp_bdd(int,c)
{
if (window.XMLHttpRequest) // Objet standard
{
xmlhttp = new XMLHttpRequest(); // Firefox, Safari, ...
}
else // Internet Explorer
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","../control/suppPlanif.php?q="+int+"&c="+c,true);
//NOTE: this code will be executed AFTER send(), it's asynchronous
xmlhttp.onload = function() {
if (xmlhttp.status === 200) {
alert('Request response: ' + xmlhttp.responseText);
}
else {
alert('Request failed. Status:' + xmlhttp.status);
}
};
xmlhttp.send();
}
或者,如果您包含jQuery ,则可以只使用$.ajax()
来简化事情:
function call_supp_bdd(int, c) {
$.ajax({
url: "../control/suppPlanif.php?q=" + int + "&c=" + c,
type: "GET"
})
.done(function (data) {
alert('Request response: ' + data);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.