2014-05-04 23 views
1

我想將用戶重定向到我的網站的移動版本。將php變量發佈到使用javascript的移動網站 - 空白變量

重定向工作正常,但PHP變量沒有通過。

我使用了以下內容:

<script> 
if ($(window).width() < 480 || $(window).height() < 480) { 
var searchproducts = "<?php $_GET['search']?>"; 
var category = "<?php $_GET['cat']?>"; 

var page="http://m.website.com/search.php?search="+searchproducts+"&cat="+category; 
document.location.href=page; 

} 
</script> 

我也曾嘗試:

<script> 
if ($(window).width() < 480 || $(window).height() < 480) { 
window.location.replace("http://m.website.com/search.php?search=<?php $_GET['search']?>&cat=<?php $_GET['cat']?>"); 

} 
</script> 

誰能幫助?

感謝

回答

0

您需要添加echo聲明..

var searchproducts = "<?php echo isset($_GET['search']) ? $_GET['search'] : 'Nothing'; ?>"; 
var category  = "<?php echo isset($_GET['cat']) ? $_GET['cat'] : 'Nothing'; ?>"; 

此外,使用isset結構如圖所示。

+2

謝謝,工作。 – user3312792

+1

@ user3312792,另外,使用'isset'構造來避免'未定義的索引'通知。 –

0

你沒有迴應變量。像這樣:

<?php echo $_GET['cat']?>