gpt4 book ai didi

javascript - 通过ajax调用特定的php函数不起作用

转载 作者:行者123 更新时间:2023-12-02 15:36:11 25 4
gpt4 key购买 nike

我制作了一个使用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(); 函数

function test()
{
echo "inserimento avvenuto con successo!";
}

更新-更新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 文件的顶部以使其进入函数。

关于javascript - 通过ajax调用特定的php函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32909409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com