簡體   English   中英

Ajax簡單調用不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM