从 HTML 获取数据并将其保存为 PHP 变量

Get data from HTML and save it as a PHP variable

我已经开始学习 php 我有一个 question.Let 说我有以下 html 代码:

<p id='tobeChanged'>I wil be changed throughout the execution<p>

该段落不是 static.Its 内容,用户可以使用按钮更改内容,该按钮会产生一个随机数并替换段落 html。 例如。来自

p id='tobeChanged'>I wil be changed throughout the execution<p>

<p id='tobeChanged'>42<p><!--changed with a button-->

现在我的 question.Is 可以将新产生的值传递给 php 变量吗?如果可能的话,我想要一个详细的解释。 我也不想使用表格(如果可能的话)。 提前致谢

您需要在该按钮点击时触发一个 AJAX 请求,该请求会将该值发送到服务器,使 php 读取它。

你可以这样做(你需要在页面上包含 jQuery):

$.post("/saveVariable.php",{randNum:randomNum},function(data){alert("Data saved successfully");})

在PHP结束时,你会得到

中的值
$_POST['randNum']

也许这会有所帮助。