2013-04-16 67 views
1

我一直在試圖弄清楚這兩天,並沒有運氣。我正在使用PHP與Javascript,我想訪問另一頁上的JavaScript變量。將Javascript或PHP變量傳遞到另一頁

這裏是我的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到另一個頁面,所以我可以編輯任務信息?

謝謝!

+0

最簡單的GET變量:「mysite的。 com/otherpage.php?taskID = 3「 – winkbrace

+0

Erm,鏈接到'page?task = 123'?這不適合你嗎? –

+0

location =「nextpage.php?task =」+ taskId – mplungjan

回答

2

只要改變這一點:

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

check this link
This one
和當然,this one瞭解什麼注射

+0

非常感謝!我忽略了它,現在我覺得很愚蠢。一直在研究準備好的陳述,並且已經有兩個準備好的插入語句。將改變選擇查詢以準備好。再次感謝! – user1536365

+0

@ user1536365:很高興我可以幫助...雖然我很欣賞你的評論,但感謝某人的SO方式是接受答案(點擊答案旁邊的大記號)......你會得到一些代表,太 –

0

喜歡這個

<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>