Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Make An Array Into A Variable

    Hello,

    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...

    HTML
    Code:
    <select name='intl_ship[]' size='5' multiple='multiple'>
    	<option>Afghanistan</option>
    	<option>Albania</option>
    	<option>Algeria</option>
    	<option>American Samoa</option>
    	<option>Andorra</option>
    	<option>Angola</option>
    	<option>Anguilla</option>
    	<option>Antarctica</option>
    	<option>Antigua and Barbuda</option>
    </select>
    PHP
    PHP Code:
    foreach ($intl_ship as $ship_con)
        {
            echo
    "$ship_con<br>";
        } 
    So if I select Afghanistan, Albania, Algeria it puts out:
    Afghanistan
    Albania
    Algeria
    If possible I would also like to submit a - at the end of each selected item.

    Thanks for the help!

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $variable serialize($_POST['intl_ship']); 

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That did work but it is ugly. Any way to only list the countries?
    Here's what it outputs:
    a:3:{i:0;s:11:"Afghanistan";i:1;s:7:"Albania";i:2;s:7:"Algeria";}

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $variable implode(','$_POST['intl_ship']); 

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No Problem at all

  • #8
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    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:

    Code:
    <option value="Afghanistan -">Afghanistan</option>
    .
    .
    .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •