Reputation: 83
I have a problem in passing 2 parameters via URL I don't know how, so I wrote this:
<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']
this var from another URL and I want to pass it again and $_GET['quantity']
quantity from form.
Upvotes: 0
Views: 21183
Reputation: 8311
You get this notice Undefined index: quantity cause you are trying to access an undefined variable.
If your page url is something like page_name.php?quantity=5
then $_GET['quantity]
is set, otherwise it isn't.
You should check if the $_GET
variable exist.
If so, use @user4035 answer to print the url.
if(isset($_GET['quantity']))
{
//html code,the url
//echo $_GET['quantity']
}
Upvotes: 0
Reputation: 2670
There's a few errors that I can see.
You can't use $_GET['quantity']
if the user is entering it in without reloading the page (Remember, PHP is not client-side). Thus, I suggest using JavaScript for that.
Javascript:
function buildUrl(a) {
var qty = document.getElementById("qty").value;
a.href = "./shopping-cart.php?part_id="+<?php echo $_GET['part_id']; ?>
+"&quantity="+qty;
}
As you no longer need to get a PHP variable from the current page, the form is obsolete and a simple link will do.
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>
Upvotes: 0
Reputation: 23749
You must use urlencode function for your parameters, and don't use the double quotes:
<a href='./shopping-cart.php?part_id=<?php echo urlencode($_GET['part_id']); ?>&quantity=<?php echo urlencode($_GET['quantity']); ?>'>
Even though, I suggest you to avoid long urls and use POST with hidden fields.
Upvotes: 1