2015-12-04 52 views
1

我寫了這個函數調用PHP腳本,但它不工作如何從按鈕點擊javascript函數調用ajax?

function finalfun(id) { 
    $.ajax({ 
     data: { 
      'id': id 
     }, 
     url: '/delete.php', 
     method: 'POST', // or GET 
     success: function (msg) { 
      alert(msg); 
     } 
    }); 

這是我的PHP腳本delete.php

<?php 
    if(isset($_POST['id'])) 
    { 
    echo ($_POST['id']); 
    } 
    else 
    { 
    echo "Id not found"; 
    } 
    ?> 

我已經wriiten的finalfun()以.php文件中,以便爲這是一個問題

+0

'數據:{的 'id':ID} ,'就夠了 – Thamilan

+0

'data:{id:id},'像這樣做 – Minato

+0

Stil L不工作 – Akki

回答

0

的test.html:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script> 
      finalfun(5); 
     function finalfun(id) { 
      $.ajax({ 
       data: { 
        'id': id 
       }, 
       url: 'delete.php', 
       method: 'POST', // or GET 
       success: function (msg) { 
        alert(msg); 
       } 
      }); 
     } 

     </script> 

delete.php:

<?php 
echo $_POST['id']; 
?> 
+0

有兩個誤區1:功能無法正常關閉,2:具有「/」 –

+0

是的,它的做工精細delete.php。 –

+0

非常感謝它的工作 – Akki

0

也許問題是與你的id,你在幹什麼id=,只是做

function finalfun(id) { 

    $.ajax({ 
    data: { 
     'id': id 
    }, 
    url: '/delete.php', 
    method: 'POST', // or GET 
    success: function (msg) { 
     alert(msg); 
    } 
    }); 
+0

感謝您的即時回覆,但它不工作。 – Akki

+0

嘗試將'URL'從'/ delete.php'改爲'delete.php' – void

+0

仍然不能正常工作 – Akki

0

試試這個:並在你的頁面中添加jQuery庫文件。

function finalfun(id) { 
    $.ajax({ 
     type: 'POST', // use here type instead of method 
     url: 'delete.php', 
     data: JSON.stringify({ id: id }), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      alert(msg); 
     } 
    }); 
} 
0
<script > 
     function show() { 
     var id = $("select[name=box1]").val(); 
     $.ajax({ 
      url: 'userviewcarajaxall.php', 
      type: 'POST', 

      data: {box1: id}, 
      success: function(data) { 
       $("#model").html(data); 
      } 
     }); 
    } 

    </script> 

呼叫使用的onclick功能

相關問題