如何从php中的html中获取值?我怎样才能改变“.$x”。在 php 中得到的结果是 JavaScript 中的 var click 。目前,它仅显示值“.x”。当我点击下一步按钮时。
php文件
<?php
$con=mysqli_connect("localhost","root","password","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$x = 1;
$sql="select * from sentence where id = ".$x." limit 10";
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$output = $row[1];
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
Javascript
function next() {
if(typeof(Storage) !== "undefined") {
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = sessionStorage.clickcount;
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support web
storage...";
}
}
var click = sessionStorage.clickcount;
function nextnext(){
alert(click);
}
HTML
<center><button onclick='next()'>NEXT</button></center>
<div id="result"></div>
最佳答案
您需要使用 post/get 或 ajax 调用将值从客户端(用你的话说 - html)发送到 php 你的问题不清楚。 如果以下内容不是您所期望的,请告诉我。我会更正答案 试试这个:
<form method="GET" action="urPHPFIle.php">
<input type="hidden" value="x_value" name="x" />
<input type="click" value="send" />
</form>
单击上面的 $_GET['x'] 值将等于 x_Value 或您在输入的 value 属性中输入的值
<?php
$con=mysqli_connect("localhost","root","password","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$x = 1;
$sql="select * from sentence where id = ".$_GET['x']." limit 10";
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$output = $row[1];
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
关于javascript - 从 php 中的 html 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25438963/