Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array from html form

Status
Not open for further replies.

yebaws

Technical User
Mar 22, 2007
42
GB
I think this should be very simple, but I'm having problems as usual...

I want to build an array from checkboxes on a form. My form looks like this:

Code:
    <input name="fruit1" type="checkbox" id="fruit1" value="apples" />
  Apples
    <input name="fruit2" type="checkbox" id="fruit2" value="pears" />
  Pears

on checking the boxes and submitting the form back to the same page with submit button value "listfruits":

Code:
if (isset($_REQUEST['listfruits'])) {

$fruits = array('fruit1', 'fruit2');
echo "$fruits";
}

the result returned is "array", not "apples, pears" as I expected...
 
Hi

HTML:
<input name="[red]fruit[][/red]" type="checkbox" id="fruit1" value="apples" />
  Apples
    <input name="[red]fruit[][/red]" type="checkbox" id="fruit2" value="pears" />
  Pears
PHP:
if (isset($_REQUEST['listfruits'])) {
  [url=http://php.net/print_r/]print_r[/url]([red]$fruit[/red]);
}

Feherke.
 
1 as Jpadie points out, don't rely on register_globals being on.

That is address your form variables as $_POST['fruit1'], $_POST['fruit2'], etc...


with that said, your code works fine.

echoing out the array as you are doing will produce exactly what you are describing, just the word "array".

Since your variable fruits is now an array, you can't just echo it out like you would a string, you either address the individual components of the array, or use print_r to echo out the complete array.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for this and the point about register_globals.

I changed my code to make my post simple. What I actually want to do is put the array into a database as a single string 'apples, pears', so I need to make the arrays into a single string.... "imlode" I think, but I'm not getting the results I want from that either....
 
If you want a string, why are you putting them into an array in the first place.

Just create a concatenated string:


Though you'll have to check which ones have been checked.
Code:
$list="";

if(isset($_POST['fruit1'])){

$list.=$_POST['fruit1'] . ",";
}

if(isset($_POST['fruit2'])){

$list.=$_POST['fruit2'] . ",";
}

if(isset($_POST['fruit3'])){

$list.=$_POST['fruit3'] . ",";
}

...



echo $list;


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top