gpt4 book ai didi

php - 将 Javascript 或 PHP 变量传递到另一个页面

转载 作者:行者123 更新时间:2023-11-30 18:04:55 40 4
gpt4 key购买 nike

两天来我一直试图弄清楚这个问题,但一直没有成功。我正在使用 PHP 和 Javascript,我正在尝试访问另一个页面上的 Javascript 变量。

这是我的 Javascript 代码,它只提醒用户 taskID 是什么......

<script>
function myFunction(taskID)
{
alert("Task #" + taskID);

}
</script>

这是我的PHP

//select all tasks for this report
$tasks = mysqli_query($conn,"SELECT taskID, hoursSpent, workDone, plannedTasks FROM Tasks where reportID = $report_id AND projID = $proj_id");
$task_count = 1;
while($row1 = mysqli_fetch_array($tasks))
{
$taskID = $row1['taskID'];
//display the task information
echo "<strong id='tasks'>Task #" . $task_count . "</strong>";
echo " - <a href = '#' onclick ='myFunction($taskID)'>Edit</a><br>";


echo "Hours Spent: " . $row1['hoursSpent'] . "<br><br>";
echo "Accomplished: <br>" . $row1['workDone'] . "<br><br>";
echo "Planned for Next week: <br>" . $row1['plannedTasks'] . "<br>";
echo "<br>";
$task_count++;
}

如何将该任务 ID 传递到另一个页面以便我可以编辑任务信息?

谢谢!

最佳答案

只要改变这个:

echo " - <a href = '#' onclick ='myFunction($taskID)'>Edit</a><br>";

echo " - <a href = 'editTask.php?id=".$taskID."'>Edit</a><br>";

而你在那里。只需确保在 editTask.php 文件中检查 id:

$editID = (int) $_GET['id'];//<-- $editID will hold an int, that is the id of the record the user wanted to edit.

除此之外:很高兴看到您正在使用 mysql 的最新扩展。尽管如此,代码如下:

mysqli_query($conn,"SELECT taskID, hoursSpent, workDone, plannedTasks FROM Tasks where reportID = $report_id AND projID = $proj_id");

会让你容易受到注入(inject)攻击。请务必查看准备好的陈述。

$tasks = mysqli_query($conn, 'SELECT taskID, hoursSpent, workDone, plannedTasks FROM Tasks WHERE reportID = ? AND projID = ?');
mysqli_stmt_bind_param($tasks, 'i', $reportID);//replace i with s if should be a string etc...

一旦您的构建基于客户端数据进行查询(您将对此进行操作:使用 url 获取 id...),这是保护您自己免受常见威胁的最简单方法。

check this link
This one
当然,this one了解什么是注入(inject)

关于php - 将 Javascript 或 PHP 变量传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16043078/

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