簡體   English   中英

將Javascript或PHP變量傳遞到另一個頁面

[英]Passing a Javascript or PHP Variable to another page

我已經嘗試了兩天了,並且沒有運氣。我正在將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++;
            }

如何將那個taskID傳遞到另一個頁面,以便我可以編輯任務信息?

謝謝!

只需更改此:

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");

將使您容易受到注入攻擊。 請仔細閱讀准備好的聲明。

$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 ...),這是保護自己免受非常常見威脅的最簡單方法。

檢查此鏈接
這個
當然, 這是了解什么是注射

像這樣

<script>
function myFunction(taskID) {
  location="nextpage.php?task="+taskID;
  return false;

}
</script>



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

或更簡單

echo " - <a href='nextpage.php?task='.$taskID.'">Edit</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM