Jump to content

should i use array for this


dazzclub

Recommended Posts

Hello,

 

I have some checkboxes that i would like to use that inserts data into table if clicked.

 

I first using something like this

---------------------

<?PHP

$ch1 = 'unchecked';

$ch2 = 'unchecked';

$ch3 = 'unchecked';

$ch4 = 'unchecked';

$ch5 = 'unchecked';

 

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

 

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

$prod1 = $_POST['ch1'];

 

if ($prod1 = = 'car') {

$prod1 = 'checked';

}

}

...insert into table...

---------------------

 

But i realise this can become to long as i have at leaset 8 checkboxes (different categories).

 

I then thought it would be best to use array? My main concern is that each check box has its own category (inside the products table)

 

so im thinking i would write some like this

 

if checkboxes are true (as in checked)

then insert them into the table and all the write tables

else

dont worry about it,

 

am i on the right track

 

cheers

Darren

 

 

 

 

Link to comment
Share on other sites

You can name your checkboxes something like

 

<input type='checkbox' id='product_01' name='products[]' value='1'>
<input type='checkbox' id='product_02' name='products[]' value='2'>
<input type='checkbox' id='product_03' name='products[]' value='3'>

 

Then, in your PHP code you can get all of the selected values in an array like this

 

$selected_products = $_POST['products'];
foreach($selected_products as $product_id) {
  echo "You selected product: $product_id";
}

 

All of the products that the user selects will be in the $selected_products array.

Link to comment
Share on other sites

Hi there

 

Thanks for your help...so far i have created this

 

-------------------------

$product_interest = array('bathsafety', 'uvSafety', 'hotWarningLabel', 'feedingSpoon', 'bedroomAndNursery');

 

//coming from a checkbox or multiple select statement

$valid = true;

if(is_array($_POST['input'])) {

$valid = true;

foreach ($_POST['input'] as $input) {

if (!in_array($input, $product_interest)) {

$valid = false;

}

}

if($valid) {

 

$q = "INSERT INTO product_interest (bathsafety, uvSafety, hotWarningLabel, feedingSpoon, bedroomAndNursery) VALUES ( '$valid')";

 

}

 

}

-------------------------

 

still no update...but im working on it....cheers :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.