0

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.

9
  • 1
    In your form add <input type="hidden" name="part_id" value="<?php echo $_GET['part_id']; ?>"/> Commented Dec 23, 2013 at 20:18
  • Are you sure that the link begins with ./? Commented Dec 23, 2013 at 20:18
  • ok I will but what about the url code is it correct ?? Commented Dec 23, 2013 at 20:20
  • yes ,the problem in the parameters in the link Commented Dec 23, 2013 at 20:21
  • Your form should also have an action attribute. And there might be a problem with the quotes. Commented Dec 23, 2013 at 20:25

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help but I still have the same problem ( ( ! ) Notice: Undefined index: quantity in C:\wamp\www)
@black This notice means, that $_GET['quantity'] element is undefined. It has nothing to do with the link. To avoid the notice, use the yannis hristofakis suggested.
0

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']
}

Comments

0

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> 

3 Comments

Could I have an explanation as to why I was down-voted?
Although you answer is very good,I think you shouldn't point him to a client side solution. He is missing fundamentally things such as POST-GET request,and an answer such as your will only make things more difficult for him. Consider that he might don't even know javascript. If you think my vote is unjustified you can edit you answer and I will undo.
@yannishristofakis that's a fair point, I'll leave it up though in case someone would like to try it this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.