Reputation: 153
My friend and I are making a website that compiles news stories based on your interests. Is there and easy way to take the checkbox data and make an array out of the selected checkboxes? Here is our form
<form action="signup.php" method="POST">
Name: <input type="text" name="name" /> <br />
Username: <input type="text" name="username"> <br />
Password: <input type="password" name="pwd" /> <br />
Email: <input type="text" name="email" /> <br />
<p>By filling out this we will be able to find news articles that will interest you</p> <br />
Politics<input type="checkbox" name="interest[]" value="Politics" /> <br />
Entertainment<input type="checkbox" name="interest[]" value="Entertainment" /> <br />
Tech <input type="checkbox" name="interest[]" value="Tech" /> <br />
Health<input type="checkbox" name="interest[]" value="Health" /> <br />
Living<input type="checkbox" name="interest[]" value="Living" /> <br />
Travel <input type="checkbox" name="interest[]" value="Travel" /> <br />
World<input type="checkbox" name="interest[]" value="World" /> <br />
Leisure<input type="checkbox" name="interest[]" value="Leisure" /> <br />
Finance<input type="checkbox" name="interest[]" value="Finance" /> <br />
Celebrity Gossip<input type="checkbox" name="interest[]" value="Gossip" /> <br />
Movies<input type="checkbox" name="interest[]" value="Movies" /> <br />
Sports<input type="checkbox" name="interest[]" value="Sports" /> <br />
<input type="submit" value="Submit">
</form>
how would we make a php array using this data?
Upvotes: 13
Views: 91011
Reputation: 1824
The following is a general routine to handle array variables sent to a page, that are located among regular name/value variables.
Example php:
<?php
/*
Summary: Output variables pushed to the page. Handle arrays that have been sent.
Remarks: $_REQUEST handles posts or gets.
*/
echo '<pre>';
foreach($_REQUEST as $name => $value){
if (is_array($value)) {
echo "$name:<br />";
// Assign array to something more mnemonic
$items = $value;
foreach ($items as $item) {
echo " $item<br />";
}
} else {
echo "$name: $value<br />";
}
}
echo '</pre>';
?>
Example Markup:
<form method="post"
enctype="application/x-www-form-urlencoded"
action="forms-process.php">
<label>Customer name: <input name="customerName" /></label>
<fieldset>
<legend> Pizza Toppings </legend>
<label> <input type="checkbox" name="toppings[]" value="bacon" /> Bacon </label>
<label> <input type="checkbox" name="toppings[]" value="cheese" /> Extra Cheese </label>
<label> <input type="checkbox" name="toppings[]" value="onion" /> Onion </label>
</fieldset>
<label><button>Submit order</button></label>
</form>
Example Output:
customerName: John
toppings:
bacon
cheese
Upvotes: 0
Reputation: 1
<form action="hitungmakan.php" method="post"><center>
<table border="1" width="400" cellpadding="3">
<tr><td colspan="5" align="center">Menu Makan dan Minum</td></tr>
<tr><td align="center">Makanan</td> <tdalign="center">Minuman</td></tr>
<tr>
<td><input name="makanan[]" type="checkbox" value="nasgor">nasi goreng $.7000<br>
<input name="makanan[]" type="checkbox" value="wuduk">wuduk $.6000<br>
<input name="makanan[]" type="checkbox" value="pecel">pecel $.9000</td>
<td><input name="minuman[]" type="checkbox" value="tehbotol">teh botol $.3000<br>
<input name="minuman[]" type="checkbox" value="campur">es campur $.7000<br>
<input name="minuman[]" type="checkbox" value="jeruk">es jeruk $.6000</td>
</tr>
<input type="submit" value="Total" name="total">
<input type="reset" value="Batal">
Upvotes: -2
Reputation: 921
hey, I have made it easy to create checkboxes as well as radio buttons in any php form. Only thing is I am using Codeigniter MVC framework.
Here is the function definition that you can insert in your common-model or any helper file.
function createOptions($fieldName, $labelsArray=array(), $selectedOption, $fieldType,$valuesArray = array()) {
$returnString = '';
if(count($valuesArray)!=count($labelsArray))
$valuesArray=$lebelsArray;
if ($fieldType === 'checkbox') {
for ($i=0;$i<count($labelsArray);$i++) {
$returnString.='   <input type="checkbox" name=' . $fieldName.' value='.$valuesArray[$i].' id='.$valuesArray[$i];
if(in_array($valuesArray[$i], $selectedOption)){
$returnString.=' checked="checked" ';
}
$returnString.=' />  <label>'.$labelsArray[$i].'</label>';
}
}
if ($fieldType === 'radio') {
for ($i=0;$i<count($labelsArray);$i++) {
$returnString.='  <input type="radio" name=' . $fieldName.' value='.$valuesArray[$i].' id='.$valuesArray[$i];
if($valuesArray[$i]== $selectedOption)
$returnString.=' checked="checked" ';
$returnString.=' /><label>'.$labelsArray[$i].'</label>';
}
}
return $returnString;
}
And, you have to call this function in view file as,
<?php
echo $this->common_model->createOptions('userHobbies[]', $hobbyOptions, $userHobbies, 'checkbox'); ?>
First parameter is name of checkbox field or radio field, which is always gonna be same for all options for both cases. Second is labels array, Third is selected options which will show those options as checked while loading the form. Fourth is type of field that will be a string as 'checkbox' or 'radio'. Fifth will be values array, which, if present, will contain values for labels in the same order as that of labels. If its absent, labels array will be teated as values array.
Upvotes: 1
Reputation: 1618
//options[] makes it an array
<form method="get">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>
You can access this array by $_GET['options']
Try Print_r( $_GET['options'])
; to see the values in it.
Upvotes: 0
Reputation: 1026
The best way I found to do this (at least for me) was to convert the checkbox values into an array to manipulate it the way I wanted with implode and explode:
<form action="thispage.php" method="post">
(the previous fields here)
<input type="checkbox" name="interests[]" value="Politics
<input type="checkbox" name="interests[]" value="Entertainment
<input type="checkbox" name="interests[]" value="Tech
<input type="checkbox" name="interests[]" value="Health
<input type="checkbox" name="interests[]" value="Living
<input type="checkbox" name="interests[]" value="Travel
<input type="checkbox" name="interests[]" value="World
etc...
<input type="submit" value="Submit">
</form>
And the php (must go BEFORE the form):
<?php
if (isset($_POST['interests'])) {
$interests_str = implode(" ", $_POST['interests']);// converts $_POST interests into a string
$interests_array = explode(" ", $interests_str);// converts the string to an array which you can easily manipulate
}
for ($i = 0; $i > count($interests_array); $i++) {
echo $interests_array[$i];// display the result as a string
}
?>
The advantage of this script is that you can access the $interests_array whenever you want in your document as a common array.
Upvotes: 1
Reputation: 1898
Sorry, posted before I was done writing:(
Just a few improvements to the suggestions already posted:
Use labels for the form:
<label for="check_politics">Politics</label>
<input type="checkbox" name="intrests[]" id="check_politics" value="Politics"/>
Using labels to enhance a form is brilliant in my opinion:) Set their display to block if you want them to get linebreaks.
And use foreach to loop through it on the serverside:
$intrests = $_POST['intrests'];
foreach($intrests as $intrest) {
echo $intrest . " is my intrest";
}
Upvotes: 1
Reputation: 30111
the HTML markup:
<form method="get">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>
and in the php code:
$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++){
echo "Selected " . $checked[$i] . "<br/>";
}
Upvotes: 37
Reputation: 2452
use this:
<input type="checkbox" name="mydata[checkbox1]"> Option 1 (politics etc)
<input type="checkbox" name="mydata[checkbox2]"> Option 2
<input type="checkbox" name="mydata[checkbox3]"> Option 3
then access $_POST["mydata"] as an array
Upvotes: 7