如何使用jQuery在同一页面中将值POST到PHP

Ind*_*ndy 8 php jquery

我不知道我是不是愚蠢.我一直试图在过去1小时内解决这个问题.请帮忙!!

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery.js" type="text/javascript"></script>
</head>

<body>
<div id="box" ></div>
<div id="box2"></div>

<script type="text/javascript">
  $(function(){
  $('#box').html("Test");

    //$('#box').attr('name','Indy');
    //var a= $('#box').attr('name');
    $.post(window.location, {name: 'John'});


});

</script>
</body>
</html>
<?php
print_r($_POST);
?>
Run Code Online (Sandbox Code Playgroud)

我如何传递价值?我知道如果php在不同的文件中,这是有效的.但这不是这种情况.

Ble*_*der 16

您将不会看到请求的结果,因为$.post()所有AJAX函数都在后台运行,而不会刷新页面.

至于发布到当前页面,只需使用窗口的位置:

$.post(window.location, {name: 'John'}, function(data) {
  alert('POST was successful. Server says: ' + data);
});
Run Code Online (Sandbox Code Playgroud)