gpt4 book ai didi

javascript - 无法将值传递给 PHP 变量

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

我一直在尝试从 javascript 将值传递给 php 变量。当来自另一个 php 文件的 url 时,我能够传递数据,但无法在同一页面上传递数据。

这是 JavaScript 代码:

<script>

$(document).ready(function(){
$(document).on('click', '#getUser', function(e){
e.preventDefault();
var uid = $(this).data('id'); // get id of clicked row

$.ajax({
url: 'user.php',
type: 'POST',
data: 'id='+uid,
success: function(data) {
// the following will be executed when the request has been completed
alert('Variable id has been sent successfully!');
}
})
});
});
</script>

最佳答案

由于您在 ajax 中使用 POST 类型。所以

data: 'id='+uid,

需要

data: {"id":uid}, //or you can do data: {id:uid},

在您的 user.php 页面上:-

<?php
echo $_POST['id'];//check what it outputs in your console

关于javascript - 无法将值传递给 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45346672/

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