gpt4 book ai didi

javascript - 使用ajax在按钮单击上调用php函数并通过参数进行解析

转载 作者:行者123 更新时间:2023-11-28 05:11:43 25 4
gpt4 key购买 nike

我想用ajax解析参数...这可能吗?如果是这样,我该怎么办?

HTML/JS

<script>
function myAjax() {
$.ajax({
type: "POST",
url: '/ajax.php',
data:{action:'follow'},
success: function() {
console.log("called");
}
});
}
</script>
<button type='button' name='follow' id='follow' onclick='myAjax()'>Follow @<?php echo $profile ?></button>

ajax.php:

session_start();
require_once 'init.php';
if($_POST['action'] == 'follow') {
User::follow($conn, $_SESSION["name"], $profile);
// $conn is a db connection, $profile is the param I would like to parse through
} else {
echo 'no req';
}

最佳答案

function myAjax(obj) { 
$.ajax({
type: "POST",
url: '/ajax.php',
data:obj,
success: function() { console.log("called"); }
});
}

像这样使用:

  myAjax({action:" follow",id:1234});

关于javascript - 使用ajax在按钮单击上调用php函数并通过参数进行解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41322962/

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