I have a (MySQL) database table that looks like:
CREATE TABLE `dm_webcategory` (
`cat_id` int(10) DEFAULT NULL,
`PUB_CATEGORY` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`web_only` tinyint(1) DEFAULT '0',
UNIQUE KEY `dm_webcategory_pk` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to display and save ALL rows in this table as checkboxes
If checked, will mark web_only as 1, and unchecked will mark web_only as 0.
Displaying them is simple:
<form method="post" action="">
<?php foreach($categories as $category): ?>
<input type="checkbox" name="category[]" value="<?php echo $category->cat_id;?>" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
But how should I go about saving this list of checkboxes?
I can access all of them using $_POST['category'], but this will only $_POST the checked boxes?
To answer my own question, I could update the whole table web_only field to 0, and then foreach the posted result to update individually to 1, assuming all those not included were unchecked.
SQL: UPDATE dm_webcategory SET web_only = 0
Then PHP:
foreach($this->input->post('category') as $category)
{
$this->db->update('dm_webcategory', array('web_only' => '1'), array('cat_id' => $category));
}
The above doesn't feel very 'safe' though, is there a better way? What about the scenario when I wasn't displaying / saving every row in the table?
Try this.
HTML:
<form method="post" action="">
<?php foreach($categories as $category): ?>
<input type="hidden" name="category[<?php echo $category->cat_id;?>]" value="0"/>
<input type="checkbox" name="category[<?php echo $category->cat_id;?>]" value="1" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
PHP:
foreach($this->input->post('category') as $id => $value)
{
$this->db->update('dm_webcategory', array('web_only' => $value), array('cat_id' => $id));
}
Try this form:
<form method="post" action="">
<?php foreach($categories as $category): ?>
<input type="hidden" name="category[<?php echo $category->cat_id;?>]" value="0" />
<input type="checkbox" name="category[<?php echo $category->cat_id;?>]" value="1" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
and then:
foreach($this->input->post('category') as $cat_id => $web_only)
{
$this->db->update('dm_webcategory', array('web_only' => $web_only), array('cat_id' => $cat_id));
}
You try to use following code
controller Code
$array = $this->input->post('s2');
$data['subject'] = implode(',',$array);
$this->student_model->saveForm($data);
view Code
<form method="post" action="">
Categories :
<?php foreach($categories as $category): ?>
<input type="checkbox" name="s2[]" value="<?php echo $category->cat_id;?>">English
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
model
$this->db->insert('Tablename', $data);
Related
I am making a quiz backend process. Where backend user will needed to update questions and answers that has been posted or saved.
Right now I have a problem in the update process I cannot update the answers
An example of my UI
See UI here
All data in answers textboxes has this element.
<input type="text" name="choice[1]" class="form-control" value="Sydney" ?="">
My update in Controller
public function update()
{
$this->post_model->update_post();
redirect('/');
}
My update in Model - $answer is null after I var_dump why?
public function update_post(){
$answer = $this->input->post('choice');
foreach($medicine as $key=>$val)
{
$data[] = array(
'answer' => $answer[$key]
);
$this->db->where('id', $val);
$updated = $this->db->update('answers');
}
}
View
<?php echo form_open('posts/update'); ?>
<input type="hidden" name="id" value="<?php echo $posts['id']; ?>">
Question:
<input type="text" name="question" class="form-control" value="<?php echo $posts['question']; ?>" ?><hr>
Answers:
<?php foreach($questions as $question): ?>
<input type="text" name="choice[<?php echo $question['id'] ?>]" class="form-control" value="<?php echo $question['answer']; ?>" ?><hr>
<?php endforeach; ?>
<hr>
<input type="submit" class="btn btn-success" value="Save Changes">
</form>
1-now already i added some texts on my database then after i want to update my database
anybody can help me?
i hope someone can help me
<?php
mysqli_query($db,"INSERT INTO tasks (task,status)
VALUES ('$task','$status')");
header('location:SIMPLE.PHP');
}
}
if (isset($_GET['check_selected'])) {
foreach ($_GET['check_selected'] as $key => $value) {
$row = mysqli_query($db,"DELETE FROM tasks WHERE id =
$value;") or die(mysqli_error($db));
}
}
$tasks = mysqli_query($db,"SELECT * FROM tasks ORDER BY id DESC");
?>
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="SIMPLE.PHP">
<input type="text" name="search" class="status_input" placeholder="TEXT" />
<input id='textbox' type='checkbox' value='1' name='status'>
<button type="submit" class="add_btn" name="submit">Add</button>
<p><?php echo $errors; ?></p>
</form>
<form action="SIMPLE.PHP" method="GET">
<tbody>
<tr>
<p><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
<?php
foreach ($rows as $row_id => $row)
{
?>
<input name="check_selected[]" type="checkbox" value=<?php echo $row['id'];?>"/>
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['status'];?>"
<?PHP if($row['status']){echo 'checked';
}
?>
>
<td class= "task"><?php echo $row['task']?></td>
<?PHP
}
?>
</tr>
</tbody>
</form>
</body>
</html>
Just wondering the best way to set a default value for a checkbox. I want the value to be 0 when checked and 1 when unchecked.
I can't use " input type="hidden" name="chk_name[]" value="1" " because of the array. It doubles my array.
If I try to set it after using isset it doesn't put it into my array.
I have multiple arrays and want to match them so that all the array[0]'s get updated together and array[1]'s etc so a missing value messes it up.
So how can I put a 1 into the array for wherever it isn't checked?
Form:
<?php
require_once ("con.php");
$p=payment();
$result = mysqli_query($mysqli,$p);
while($res=mysqli_fetch_array($result))
{
?>
<tr>
<td class="paid">
<input type="hidden" value="1" name="paid[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"
<?php
if($res["paid"]==0)
{
echo "checked";
}
?>>
</td>
<td class="active">
<input type="hidden" value="1" name="active[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]"
<?php
if($res["active"]==0)
{
echo "checked";
}
?> >
</td>
<input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
</body>
</html>
php:
$paid=$_POST['paid'];
$active=$_POST['active'];
foreach($_POST as $key=>$value)
{
$ID=$ID[$key];
$paid=$paid[$key];
$active=$active[$key];
$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up);
echo "Information stored successfully";
}
?>
my function:
function updatePayment($paid,$active,$ID)
{
$uc="UPDATE
`company`
SET
`paid`='$paid',
`active`='$active'
WHERE
`ID`='$ID'";
return $uc;
}
:Updated code:
I can see the arrays are coming out fine for everything now. The hidden method worked.
Thanks for everyone's help!
As soon the hidden field and the checkbox has the same name it will work no matter if the name is array or not.
The simple reason for this is that when is unchecked the checkbox value is not posted.
Try this code and you will see the result
<pre>
<?php
print_r($_POST);
?>
</pre>
<form method="POST">
<input type="hidden" name="fields[1]" value="1"/>
<input type="checkbox" name="fields[1]" value="0"/>
<input type="hidden" name="fields[2]" value="0"/>
<input type="checkbox" name="fields[2]" value="1"/>
<input type="hidden" name="fields[3][something][else]" value="3"/>
<input type="checkbox" name="fields[3][something][else]" value="3"/>
<button type="submit">Submit</button>
</form>
<form method="post">
<input type="submit" name="'.$pTest.'" value="submit" id="submit"/>
</form>
<?php
if(isset($_POST[''.$pTest.'']))
{echo 'something'
;}
?>
This doesn't work if I use variable as the name of the submit button. Please some help!
Try like this
<input type="submit" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
Your variable is doing nothing. Just echo your <input> name and catch it whith php:
<form method="post">
<input type="submit" name="<?php echo $pTest ?>" value="submit" id="submit"/>
</form>
<?php
if (isset($_POST[$pTest])) {
echo 'something'
}
?>
Add value to the variable first..
<?php $pTest = 'sks'; ?>
<form method="post">
<input type="submit" method="post" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
</form>
<?php
if(isset($_POST[$pTest]))
{
echo '<script>alert("something");</script>';
}
?>
Just replace your quotes with double quotes :
if(isset($_POST["'.$pTest.'"]))
{
echo 'something'
;}
Or name your input with a PHP variable :
<input type="submit" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
For some probably obvious to anyone else reason, I cannot save the input value of my radio buttons and retrieve them with php. $_POST['answerToQuestion'] is not empty and will print the $key of each, but the value is empty. Can anyone readily see my mistake?
html:
<form action="answerQuestion.php" method="post">
<?php foreach($questions as $k => $q):
if(!$q['is_subquestion']):?>
<div class="questionAnswer">
<?php echo $q['body']; ?><br/>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" id="answer_yes" name="answerToQuestion[<?php echo $k; ?>]" value= 1>Yes</button>
<button type="button" class="btn" id="answer_no" name="answerToQuestion[<?php echo $k; ?>]" value= 0>No</button>
<button type="button" class="btn" id="answer_na" name="answerToQuestion[<?php echo $k; ?>]" value= 2>N/A</button>
</div>
<input type="hidden" id="hidden_2" name="answerToQuestion[<?php echo $k; ?>]" value="">
</div>
<?php endif;?>
<?php endforeach; ?>
<input type="submit" value="Next" name="submit-form" />
</form>
php:
foreach($_POST['answerToQuestion'] as $key=>$value)
{
echo ' '.$value.'<br/>';
}
Basing this answer on the question which clearly states "my radio buttons".
You don't need to echo the $k variable and you should use actual radio buttons:
<form action="answerQuestion.php" method="post">
<div class="btn-group" data-toggle="buttons-radio">
<label><input type="radio" name="answerToQuestion" value="1"> Yes</label>
<label><input type="radio" name="answerToQuestion" value="0"> No</label>
<label><input type="radio" name="answerToQuestion" value="2"> N/A</label>
</div>
<input type="submit" value="Submit">
</form>
Also, remove the spaces value= 0 (from your example), I use quotes as well.
$_POST['answerToQuestion'];
That should work.