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 selecte
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
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/>";
}
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
<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">
//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.
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.