2017-05-04 87 views
1

我嘗試將頁面重定向到1stlink.php以及URL參數。但是在重定向用戶之後,該變量不會顯示在頁面上。重定向頁面不顯示url值

爲什麼不能在我的重定向頁面中獲得variable的值?

<html>... <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
    <script> 
     $('#btn').click(function() { // The `$` reference here is a jQuery syntax. So we are loading jquery right before this script tag 
     var textval = $('#textcat').val(); 
     window.location = "1stlink.php?variable=" + textval; 
     }); 
    </script> 

1stlink.php

<?php 
echo $_GET['variable']; 
?> 
+0

:你總是應該添加任何到URL之前使用encodeURIComponent這個問題可能是由於你的頁面由於連接問題從另一個站點加載jquery失敗。嘗試下載jquery源代碼並讓您的網頁從您的網站加載它。此外,要獲得更清晰的想法$ _GET ['變量']測試,看看它的值是否爲空。您可以在PHP中使用is_null()或$ _GET ['variable']!= null。 – slevy1

+1

重定向是否發生?確保您將有效的值傳遞給URL。 'window.location =「1stlink.php?variable =」+ encodeURIComponent(textval);'在你的PHP頁面上,嘗試'var_dump($ _ GET)'而不是'echo'。 – miken32

+0

謝謝@ miken32 encodURIComponent的工作 – prog

回答

0

可能是因爲您傳遞到URL中的數據是無效的。

window.location = "1stlink.php?variable=" + encodeURIComponent(textval); 

(我相信jQuery的serialize功能也將照顧編碼爲你,但在此之前還沒有使用它單個元素)