![](/img/trans.png)
[英]How can I pass an object with multiple values as parameters in an url with PHP
[英]How I can pass multiple parameters to URL in php
我不知道如何通過URL傳遞2個參數時遇到問題,所以我這樣寫:
<form method="get" >
<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
</form>
<br><br>
<a href='./shopping-cart.php?part_id="<?php echo $_GET['part_id']; ?>"&quantity="<?php echo $_GET['quantity']; ?>"'>
<img src="add_to_shopping_cart.png">
</a>
$_GET['part_id']
此變量來自另一個URL,我想再次傳遞它,並從表單中傳遞$_GET['quantity']
數量。
您必須對參數使用urlencode函數,並且不要使用雙引號:
<a href='./shopping-cart.php?part_id=<?php echo urlencode($_GET['part_id']); ?>&quantity=<?php echo urlencode($_GET['quantity']); ?>'>
即使如此,我還是建議您避免使用長網址,並在隱藏字段中使用POST。
我可以看到一些錯誤。
如果用戶在不重新加載頁面的情況下輸入$_GET['quantity']
則不能使用它(請記住,PHP不是客戶端)。 因此,我建議為此使用JavaScript。
使用Javascript:
function buildUrl(a) {
var qty = document.getElementById("qty").value;
a.href = "./shopping-cart.php?part_id="+<?php echo $_GET['part_id']; ?>
+"&quantity="+qty;
}
由於您不再需要從當前頁面獲取PHP變量,因此該表格已過時,並且可以通過簡單的鏈接進行操作。
HTML:
<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
<a href='#' onclick="buildUrl(this);"> //onclick attribute triggers the JavaScript
<img src="add_to_shopping_cart.png">
</a>
您會收到以下通知: 未定義索引:數量,原因是您嘗試訪問未定義的變量。 如果您的頁面網址類似於page_name.php?quantity=5
則設置$_GET['quantity]
,否則不設置。
您應該檢查$_GET
變量是否存在。 如果是這樣,請使用@ user4035答案打印URL。
if(isset($_GET['quantity']))
{
//html code,the url
//echo $_GET['quantity']
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.