gpt4 book ai didi

javascript - 如何在不使用 $_SESSION ["name"] = "<script>document.write(name)</script>"的情况下将 js 变量存储到 php 中;?

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:14 25 4
gpt4 key购买 nike

我尝试使用 $_SESSION["name"] = "document.write(name)";如果我回应它,​​它就会起作用。但是,如果我将此 SESSION 变量显示到文本框中或使用它来执行 SQL,它会显示 document.write(name)。我怎样才能正确地将它存储到一个 php 变量中?我搜索了一下,很多人说你不能转换它。但肯定有一些地方我可以使用 Javascript 变量(名称)在文本框中显示它或在 SQL 中使用它。

带有变量名的javascript代码。

<script>
var name;
$(document).ready(function()
{
$('#info tr #name').click(function()
{
name = $(this).text();
alert(name);
window.location = 'CV.php';
});
});
</script>

对于 SQL 我的代码是这样的,

<?php $_SESSION["name"] = "<script>document.write(name)</script>"; 
echo $_SESSION["name"];
$connect = mysqli_connect("localhost", "root", "","test1");
$sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $_SESSION["name"]."'";

$result = mysqli_query($connect, $sql);

while($row= mysqli_fetch_array($result))
{
$name = $row['Name'];
$number =$row['Number'];
$Expertise =$row['Expertise'];
$status =$row['Status'];
$remarks =$row['Remarks'];
}
?>

下面是我的代码,试图将我存储在 session 中的变量显示到文本框中。

<input type='text' value='" .htmlspecialchars($_SESSION['name']) . "'/>

最佳答案

<script>
var name;
$(document).ready(function()
{
$('#info tr #name').click(function()
{

name = $(this).text();
alert(name);
window.location = 'CV.php?name=' + name; // send in link

});
});
</script>

CV.PHP 内部

if(isset($_GET['name']))
{
$name = $_GET['name']; // Get it from URL
$_SESSION["name"] = $name;
}
else
{
$name = $_SESSION['name']; // Get it from session
}
$connect = mysqli_connect("localhost", "root", "","test1");
$sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $_SESSION["name"]."'";

$result = mysqli_query($connect, $sql);

while($row= mysqli_fetch_array($result))
{
$name = $row['Name'];
$number =$row['Number'];
$Expertise =$row['Expertise'];
$status =$row['Status'];
$remarks =$row['Remarks'];
} ?>

在输入框中显示:

    <input type='text' value='" .$name . "'/> // echo value as string
<input type='text' value='" .$_SESSION['name']. "'/> // if it's a page that is not CV.php

关于javascript - 如何在不使用 $_SESSION ["name"] = "&lt;script&gt;document.write(name)&lt;/script&gt;"的情况下将 js 变量存储到 php 中;?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627258/

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