[英]Call specific php function through ajax don't working
我制作了一個使用ajax請求的腳本,當用戶按下index.php
頁面中的按鈕時,該腳本基本上被激活,例如:
Risorse.php
<form method='post'>
Name <input type='text' name='nome' id='nome'>
Col <input type='text' name='colore' id='colore'>
Plan <select name='planning' id='planning'><option value='p1'>p1</option></select>
<button type='submit' id='Aggiungi'>Register</button>
</form>
這是腳本:
Index.php
<script>
$("#Aggiungi").click(function(e){
e.preventDefault();
var nome = $("#nome").val();
var colore = $("#colore").val();
var planning = $("#planning").val();
$.ajax({
url: "Class/Risorse.php",
data: "function=test",
/*type: "POST",
url: "Class/Risorse.php",
data:
{
nome: nome,
colore: colore,
planning: planning
}*/
})
.done(function( msg ) {
//alert("Success");
})
.fail(function() {
alert('Problem');
});
});
Risorse.php
在類Risorse.php
我創建了test();
Risorse.php
功能
function test()
{
echo "inserimento avvenuto con successo!";
}
update-更新ajax調用哪個函數的代碼:
if(isset($_GET['function']))
{
if($_GET['function'] == 'test')
{
test(); //but this function isn't called and is located in the same file
}
}
但是沒有什么快樂的地方,如果我執行注釋的代碼,ajax也可以很好地工作。 我究竟做錯了什么?
您在$.ajax
調用中聲明了兩次data
。 php測試功能是否位於Class/Risorse.php
? 如果是這樣,您的php將不會自動輸入此功能,但它將運行php文件。 添加test();
到該php文件的頂部,以使其進入功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.