我想用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/