Patrick
Patrick

Reputation: 53

Can POST and GET be combined within a single input type?

I have a form running a shopping cart style application on my site. To add items, I POST values to a form using a submit button. To remove items, I have to use a GET command.

What I want to do is to limit the selection possibilities - as you select one option, others are removed. For instance, if I have three options: Apples, Oranges, Bananas you are only able to select one.

Apples Oranges Bananas

If you select Apples, I want to post the value "Apples" whilst using a GET command to remove "Bananas" and "Oranges".

Currently I am doing this to post the values:

<form method="post">
   <fieldset>
       <input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
        <input type="hidden" name="id" value="Apples" />
        <input type="hidden" name="name" value="Apples" />
        <input type="hidden" name="color" value="red" />
        <input type="hidden" name="shape" value="round" />
        <div id="apples" >
            <input type="submit" name="my-add-button" class="add" value="&nbsp"/>&nbsp Apples
        </div>
    </fieldset>
</form> 

And to remove the items I do this:

<a href="index.php?jcartRemove[]=Bananas&jcartRemove[]=Oranges">remove Bananas and Oranges</a>  

Is there a way to do both at the same time? I have tried doing an onclick event like this:

<div id="Apples" >
    <input type="submit" name="my-add-button" class="add" value="&nbsp" onclick="location.href='index.php?jcartRemove[]=Bananas&jcartRemove[]=Oranges';" />&nbsp Apples
</div>

and I have also tried to use an action at the start of the form

But neither of these work - they will still submit the new item, but will not remove the item. Any idea of a good way to do both together?

Upvotes: 0

Views: 206

Answers (2)

Ben
Ben

Reputation: 314

You can use $_REQUEST. As per the php documentation, quoted as follows:

An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE

As above, you can then use the following hack:

<form method="post" action="foo.php?x=y">
  <input type="text" name="a" value="b" />
</form>

EDIT: If both of the GET and POST requests work individually, it is possible that your PHP is where the problem lies - You haven't posted it, so I can't see where the issue could be. You could just put together some javascript to fire the remove request then fire the add request when clicked:

jQuery("input[name|='my-add-button']").click(function() {
  var addform = jQuery(this);
  event.preventDefault();
  $.get("index.php?jcartRemove[]=Bananas&jcartRemove[]=Oranges", function(data) {
    addform.submit();
  });
});

Upvotes: 0

Marc B
Marc B

Reputation: 360732

Technically, yes, but it's a hack:

<form method="post" action="foo.php?x=y">
  <input type="text" name="a" value="b" />
</form>

If the form is set to POST, then any <input> and <textarea> within the form will go as POST data, but any query strings you place into the action's url will show up at the server as GET data:

 $_GET['x'] -> 'y'
 $_POST['a'] => 'b'
 $_POST['x'] => undefined index

But note that clicking a link that's inside a <form> does NOT submit the form. it's like clicking any other link and will just go to the new address.

Upvotes: 1

Related Questions