0
我在javascript中有一個字符串數組。但是當我使用href ='mypage.php?id = var'時,我失去了這個數組。我需要保留它在$ _GET中使用它。這是代碼:Javascript var在更新頁面時發生變化
<script>
var element_selected=[];
var i = 0;
function hrefPage()
{
var pagina = "index.php?id=renew";
location.href = pagina;
}
function loadArray(value)
{
element_selected[i] = value;
i++;
}
</script>
<?php
if(isset($_GET['id']))
{
if ($_GET['id'] == "renew")
{
$selected_elements = array();
$j = 0;
for($j = 0; $j < "<script> document.write(i) </script>"; $j++)
{
$selected_elements[j] = "<script> document.write(elements_selected[j]) </script>";
echo $selected_elements[j];
}
}
}
?>
PHP首先在服務器上執行。 Aftwerwards JS在你的瀏覽器中執行。因此,您不能像在for循環中那樣在您的PHP代碼中使用JS –
您不能像這樣在PHP中編寫JavaScript,它不會被解釋。到PHP這些只是字符串。你也應該避免使用'document.write'。 –
你在哪裏打電話給hrefpage()方法 – Aparna