I created a contact form for the inquiry of customers, category and subcategory I was create using the checkboxes, it is the format of accordion
feature and subfeature are the checkboxes, I am using the insert query for the storing of the database, but I can't insert approximate feature with subfeatures.
three tables are here such as 'iv_mail',iv_mail_features','iv_mail_subfeatures`.
shown below the code
$data=array('name'=>$this->input->post('sendname'),
'lastname'=>$this->input->post('sendlname'),
'mobile'=>$this->input->post('sendphone'),
'location'=>$this->input->post('sendlocation'));
$this->db->insert('iv_mail',$data);
$insert_id=$this->db->insert_id();
foreach($this->input->post('features') as $feature){
$data=array('mail_id'=>$insert_id,
'featurename'=> $feature);
$this->db->insert('iv_mail_features',$data);
$sub_id=$this->db->insert_id();
foreach($this->input->post('sub_feature') as $subfeature){
$data=array('feature_id'=>$sub_id,
'subfeaturename'=> $subfeature);
$this->db->insert('iv_mail_subfeatures',$data);
}
}
here i used insert_id, anyone can help me
You must do name all checkbox as an array like this.
<input type="checkbox" name="checkbox[]" value="add"><hr>
<input type="checkbox" name="checkbox[]" value="edit"><hr>
<input type="checkbox" name="checkbox[]" value="update"><hr>
<input type="checkbox" name="checkbox[]" value="delete">
then in your function must be like this
$inputnum = count($this->input->post('checkbox'));
for ($i=0;$i < $inputnum; $i++) {
echo $this->input->post('checkbox')[$i].'<hr>';
}
You can change the code as you want.
also, you can add all features as json in one column like this
$inputnum = count($this->input->post('checkbox'));
for ($i=0;$i < $inputnum; $i++) {
$checkbox[] = $this->input->post('checkbox')[$i].'<hr>';
}
json_encode($checkbox);
Related
I have been searching this for the last couple of hours and there are a few very similar questions and answers but none are quite working for my situation. What I'm trying to do is insert a list of songs set to the same value for two columns, one per row, and with a couple of options set via radio buttons. The latter is the reason why I can't just write all of the songs in a textarea and split them.
This is what the form looks like (obviously not fully designed)
There are only three there now while I get it working, in reality an unlimited amount may be added via javascript.
And the code:
<form action="" method="post">
<label>Session ID</label>
<input type="text" name="session-id"><br>
<label>Songs</label><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="song-key[]"><input type="checkbox" name="is-partial[]"><input type="checkbox" name="is-jam[]"><br>
<input type="text" name="update-date" value="<?php echo date(" Y-m-d H:i:s ");?>" hidden readonly><br>
<input type="submit" name="submit" value="Submit">
</form>
The desired result, assuming the ID has been set and a unique song entered for each of the them, and whatever variety on the radio buttons, would be three table rows. session-id and update-date would all be the same value, the rest would be unique based on entry.
This is what I currently have, but it only inserts the last of the three songs.
for ($i=0; $i < count($_POST['song-key']); $i++ ) {
$sessionkey = mysqli_real_escape_string($connection, $_POST["session-key"]);
$songkey = mysqli_real_escape_string($connection, $_POST["song-key"][$i]);
$partial = mysqli_real_escape_string($connection, isset($_POST['is-partial'][$i])) ? 1 : 0;
$jam = mysqli_real_escape_string($connection, isset($_POST['is-jam'][$i])) ? 1 : 0;
$updated = mysqli_real_escape_string($connection, $_POST["update-date"]);
$sql = "INSERT INTO session_songs (session_key, song_key, is_partial, is_jam, last_updated)
VALUES ('$sessionkey', '$songkey', '$partial', '$jam', '$updated')";
}
What do I need to change to ensure all three (or more) are entered?
If you alter your input names, you can get a more useful $_POST array.
<input type="text" name="songs[0][key]">
<input type="checkbox" name="songs[0][is-partial]">
<input type="checkbox" name="songs[0][is-jam]"><br>
<input type="text" name="songs[1][key]">
<input type="checkbox" name="songs[1][is-partial]">
<input type="checkbox" name="songs[1][is-jam]"><br>
<input type="text" name="songs[2][key]">
<input type="checkbox" name="songs[2][is-partial]">
<input type="checkbox" name="songs[2][is-jam]"><br>
With specified keys, the checkbox type inputs will match up properly with the text inputs, where they way you have it now, they will not*, because only checked checkboxes are submitted to PHP. (Try only checking "is-partial" in the second and "is-jam" in the third row, and then var_dump($_POST), and you'll see that they both have index 0.)
If your form is structured like that, you can insert your records using a foreach loop instead of a for loop.
foreach ($_POST['songs'] as $song) {
$key = $song['key'];
$partial = isset($song['is-partial']) ? 1 : 0;
$jam = isset($song['is-jam']) ? 1 : 0;
// do the insert inside the loop rather than just building the SQL
}
The reason you're currently only getting the last one is that you're defining the SQL string inside the loop, but without executing the statement in the loop, you'll only get the values from the last iteration of the loop. Move your query execution inside the loop and you should get all three rows.
*unless they are all checked
Hello I am using various check boxes with different values and than storing the vales inside DataBase as all the checkboxes values are store inside Array
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<input type="checkbox" value="Spa" id="details_11" name="utilities[]">
and than store in the database like: Gym, Spa.
Later I am making MySQL query inside edit form where I will be able to edit those and other parameters. For the other inputs I am using:
<?php if($row['data'] == 'Sample Data'){echo 'selected="selected"';}?>
In this case I would like to use something like the following:
<input type="checkbox" value="Gym" id="details_10" name="utilities[]"<?php if($row['utilities'] == 'Gym'){echo 'checked"';}?>>
Any help to achieve this result will be very welcome.
I've always used this in these cases.
<?php
if($row['utilities'] == 'Gym')
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]" checked="checked">
<?php
}
else
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<?php
}
?>
I want to store multiple checkbox value in database ,and my checkbox name is differnt,how can save ??.I have three column like Called them,Called you,toured, how i can store each value in data base.
HTML Code
<input type="checkbox" name="Called_them[]" value="1">1<br>
<input type="checkbox" name="Called_them[]" value="2">2<br>
<input type="checkbox" name="Called_you[]" value="1">1<br>
<input type="checkbox" name="Called_you[]" value="2">2<br>
PHP Code:-
if(isset($_POST['submits']) )
{
$checked = $_POST['alled_them'];
$checked1 = isset($_POST['alled_them']);
for($i=0; $i < count($checked); $i++){
$tellfriend=new MemberEmails;
$tellfriend->called_them = isset($checked[$i])==0?'':$checked[i];
$tellfriend->save();
}
if(isset($_POST["submit"])) {
foreach($_POST['called_them'] as $called_them) {
//do things
}
foreach($_POST['called_you'] as $called_you) {
// do things
}
}
$array_called_them = $_POST['called_them'];
$array_called_them = $_POST['called_you];
You now have both checkbox groups as arrays. if you want the value from the array just use foreach on each of the arrays.
I cannot figure out how to save checked checkbox form input into MySQL and be able to retrieve them and re-populate the inputs.
I haven't even created a table yet because I cannot mentalize how go by doing it.
I have an html table with multiple rows(tr) and inside it all the tables have these checkboxes and I am wanting to save each of the selected boxes to MySQL and
be able to retrieve the selected ones and re-populate it, but as I said I cannot figure out how to do it.
The only way I can think of is, submit the selected with a foreach and save all the selected ones, in each row with their specific table row iD but I don't understand how I would retrieve it if they are selected.
I've got the following but no idea how it would be retrievable. Also, the checkboxes can increase at any time(the options) so I cannot create a specific table for them as there are currently 4 checkboxes, but tomorrow it can be 20 checkboxes to choose from.
$checkboxData = $_POST['cbO'];
$tableiD = '1';
if(isset($_POST['submit']))
{
for( $i=0; $i < sizeof($checkboxData); $i++ )
{
$sth = $this->db->prepare("INSERT INTO table(rows) VALUES (:checkboxInfo) WHERE table_id = :tableiD");
$sth->execute(array(
':checkboxInfo' => $checkboxData[$i],
':tableiD' => $tableiD)
);
}
}
<form method="post" action="">
<li><input type="checkbox" name="cbO[]" value="349169846048613138">CheckBox1</li>
<li><input type="checkbox" name="cbO[]" value="349169846048613136">CheckBox2</li>
<li><input type="checkbox" name="cbO[]" value="349169846048613140">CheckBox3</li>
<li><input type="checkbox" name="cbO[]" value="349169846048613141">CheckBox4</li>
<input type="submit" name"submit" value"Submit"/>
</form>
Regarding the variable amount of items, get the items from the database and make a loop with them:
<form method="post" action="">
<ul>
<?php if (!empty($items)) foreach ($itens as $i => $value) { ?>
<li>
<input type="checkbox" name="cbO[<?= $i ?>]" value="<?= $value ?>">
CheckBox<?= ($i + 1) ?>
</li>
<?php } ?>
</ul>
</form>
Run a print_r($_POST) on the value class you expect to receive, then the value will be what you expect it to be always.
I am storing content into a database table. One table column is called attrbutes and has a list of values such as (ex: 1, 3, 5) based on the checkboxes that were checked.
<form>
<input type="checkbox" name="attribute" value="1">Attr 1<br>
<input type="checkbox" name="attribute" value="2">Attr 2<br>
<input type="checkbox" name="attribute" value="3">Attr 3<br>
<input type="checkbox" name="attribute" value="4">Attr 4<br>
<input type="checkbox" name="attribute" value="5">Attr 5<br>
<form>
Couple of questions on how to integrate checkboxes with PHP...
1) How do I check to see if at least 1 checkbox is checked on form submit?
2) How do I turn the checked checkboxes into a list like 1, 3, 5 if checkboxes 1, 3, and 5 are selected.
3) As a reverse to #2, on page load I need to figure out how to check each checkbox that's value is listed in the database column. if 1, 3, 5 is listed in table column, I need checkboxes 1 3 and 5 checked on page load.
I know how to code the basic queries for inserting, updating, and removing etc...but I've never worked with checkboxes and storing values from checkboxes using php before.
Change you html:
<input type="checkbox" name="attribute[]" value="1">Attr 1<br>
<input type="checkbox" name="attribute[]" value="2">Attr 2<br>
<input type="checkbox" name="attribute[]" value="3">Attr 3<br>
<input type="checkbox" name="attribute[]" value="4">Attr 4<br>
<input type="checkbox" name="attribute[]" value="5">Attr 5<br>
1)
$checkedAttr = $_POST['attribute'];
if(count($checkedAttr) > 0)
echo "At least one checkbox is selected";
2)
$checkboxList = implode(',', $checkedAttr);
3)
$checkedAttr = explode(',', $yourQueryResultStringContainingTheCheckedList);
<input type="checkbox" name="attribute[]" value="1" <?php if(in_array('1', $checkedAttr)) echo 'checked=\"checked\"'; ?>Attr 1<br>
...
1, 2) You can treat form elements as arrays with name="attribute[]" then loop through the posted values in your php as an array.
For example:
<?php
$attributes = $_POST['attribute'];
if(empty($attributes)) {
echo "No attributes selected";
} else {
// echo whole array
print_r($attributes);
// loop through array
foreach($attributes as $attribute) {
echo $attribute." ";
}
// create list as one whole string
$list = implode(',', $attributes);
}
?>
3) When you are building the form (using php) you can check each value in a loop. Note that I also made your labels proper labels so they will also activate the checkbox if clicked.
<?php
// need some code to get db values into array
$attributes = array(1,3,5); // your list
// loop through the amount of checkboxes you want
for($i=1; $i <= 5; $i++) {
if(in_array($i, $attributs) { // check for a match with current checkbox
$checked = " checked";
} else {
$checked = "";
}
echo'<input type="checkbox" name="attribute[]" id="attribute'.$i.'" value="'.$i.'"'.$checked.'><label for="attribute'.$i.'">Attr 1</label><br>'
}
?>