View Full Version : Make An Array Into A Variable

09-24-2005, 03:00 AM

I have a array simply constructed from a post which I need to set it up into a variable to submit the data to a database. Here's the code...


<select name='intl_ship[]' size='5' multiple='multiple'>
<option>American Samoa</option>
<option>Antigua and Barbuda</option>


foreach ($intl_ship as $ship_con)

So if I select Afghanistan, Albania, Algeria it puts out:
If possible I would also like to submit a - at the end of each selected item.

Thanks for the help!

09-24-2005, 03:20 AM
Firstly, a big security issue...

You are using $intl_ship to get the submitted data, this can lead to security issues and importable code, and is generally frowned upon. Use the $_POST global instead (shown in example below)

Im not 100% sure what your problem is here... If you want to store the array to use again, you could use:

$variable = serialize($_POST['intl_ship']);

09-24-2005, 04:09 AM
Thanks for the reply. Do you have any more info on the security issue pertiaining to not using $_POST?

I will give your solution a try.

09-24-2005, 04:15 AM
That did work but it is ugly. Any way to only list the countries?
Here's what it outputs:

09-24-2005, 04:20 AM
The security hole isnt a wide one, but it can happen. Say you have a function that takes $var as a parameter... If you forget to set $var, and then someone puts yourpage.php?var=something_bad in the URL, they can run whatever they like though your function.

Secondly, $_GET, $_POST, $_COOKIE, $_SESSION and $_SERVER are good coding practise, and you should use these and not rely on global variables.

In response to your second post, you can make the data back into an array using unserialize(), on that jumbled mess of values. But if you just want the countries listed the why not use:

$variable = implode(',', $_POST['intl_ship']);

09-24-2005, 04:28 AM
That's perfect!

Thank you sooo much for your help and letting me know about $_POST issue. I'll defintly use it from now on...

09-24-2005, 04:30 AM
No Problem at all :)

09-24-2005, 05:03 AM
If possible I would also like to submit a - at the end of each selected item.

Another option is to assign values in your option tags:

<option value="Afghanistan -">Afghanistan</option>