通过 jQuery Ajax 和 .data() 方法将数据发送到页面,同时转到该页面


send data to a page through jquery ajax and.data() method while going to that page

<script>
function user_pro(useron){
$.post("userprofile.php", { useron:useron } );
document.location.href="userprofile.php";
}
$(document).on('click','#userprofile',function(){
    var useron=$(this).data('id4');
    user_pro(useron);
});
</script>

当我单击ID为"UserProfile"的按钮时,我正在尝试通过jQuery ajax .post((将数据发送到页面"UserProfile.php"。 我已经存储了用户的用户名(我在 data-id4 属性中从数据库中检索到(。 我想将此用户名发送到我的用户配置文件.php页面并显示他的配置文件(如 DP,状态和所有....data('id4'(方法工作正常,因为我能够将数据存储在变量useron中。但是我无法将数据发送到用户配置文件.php .同时,当我单击id="userprofile"按钮时,我也希望被定向到该页面。

<a data-id4='".$row['username']."' id='userprofile' class='w3-btn w3-teal w3-hover-indigo'>profile</a>

这是 HTML 元素。HTML 位于 PHP 页面的 echo 标签内(这就是为什么这些引号(。

有人可以在这里帮助我吗. 提前感谢:).

这段代码有效...

function post(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
    var field = $('<input></input>');
    field.attr("type", "hidden");
    field.attr("name", key);
    field.attr("value", value);
    form.append(field);
});
// The form needs to be a part of the document in
// order for us to be able to submit it.
$(document.body).append(form);
form.submit();
}
$(document).on('click','#userprofile',function(){
    var useron=$(this).data('id4');
    post("userprofile.php",{useron:useron});
});

谢谢大家。