Viewed   81 times

I need your help.,

How to pass the array values in query string...

<?php 
                foreach ( $Cart->getItems() as $order_code=>$quantity ) :
                $pname[]= $Cart->getItemName($order_code);
                $item[]= $Cart->getItemPrice($order_code);
                $qty[]=$quantity;
                endforeach;
                ?>
                <form action='expresscheckout.php?amt=<?php echo $total_price; ?>&item_name=<?php echo $pname; ?>&unit=<?php echo $item; ?>&quan=<?php echo $qty; ?>' METHOD='POST'>
<input type='image' name='submit' src='https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif' border='0' align='top' alt='Check out with PayPal'/>
</form>

This is my code sample.,for the above sample code i have to pass the query string on my action url that is expresscheckout.php?amt=100.,etc...values are coming under the $pname[],$item[],$qty[].

Expecting output in expresscheckout.php

expresscheckout.php?pname=product1,product2&item=item1,item2&qty=1,2,3....like this....

Thanks in Advance....

 Answers

5

Basically, you just need to use PHP's implode function to turn an array into a comma-separated list. i.e. for the $item array:

$item = array('item1', 'item2');
echo implode(',', $item);

Will output:

item1,item2

So, something like this should print a querystring similar to what you want:

echo 'pname='.implode(',', $pname).
     '&item='.implode(',', $item).
     '&qty='.implode(',', $qty);
Monday, August 8, 2022
5

Here's what I figured out:

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

  • ?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array)
  • ?cars=Saab&cars=Audi (Bad way- PHP will only register last value)
  • ?cars=Saab,Audi (Haven't tried this)

Form Examples

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
    <select multiple="multiple" name="cars[]"> 
        <option>Volvo</option> 
        <option>Saab</option> 
        <option>Mercedes</option> 
    </select>
</form>

(NOTE: In this case, it would be important to name the select control some_name[], so that the resulting request vars would be registered as an array by PHP)

... or as multiple hidden fields with the same name:

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.


UPDATE

As commenters have pointed out, this is very much framework-specific. Some examples:

Query string:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3

Rails:

"list_a": "3", 
"list_b":[
    "1",
    "2",
    "3"
  ], 
"list_c": "1,2,3"

Angular:

 "list_a": [
    "1",
    "2",
    "3"
  ],
  "list_b[]": [
    "1",
    "2",
    "3"
  ],
  "list_c": "1,2,3"

(Angular discussion)

See comments for examples in node.js, Wordpress, ASP.net


Maintaining order: One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

Tuesday, December 27, 2022
4
substr("testers", -1); // returns "s"

Or, for multibytes strings :

substr("multibyte string…", -1); // returns "…"
Wednesday, December 14, 2022
 
akidi
 
3
<?php
    header("Status: 301 Moved Permanently");
    header("Location:./content/index.html?". $_SERVER['QUERY_STRING']);
    exit;
?>
Tuesday, September 27, 2022
 
1

Your query string should rather look like this:

?formparts[]=a&formparts[]=b&formparts[]=c
Monday, September 12, 2022
 
hrzio
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :