I'm using CodeIgniter and mySQL to build a checkbox form. The form contains 4 options; each option has only one checkbox; users can select any combination of the options. I want to do the following:
1 - For each checkbox, use a value of 1 (if unchecked) or 2 (if checked) and pass those values through to the database (each checkbox has its own field). Right now, whether checked or unchecked, the checkboxes are sending a value of 0 through to the database.
2 - Once users update their checkboxes, I'd like to update the database to reflect the new values. Right now, a new row is added for each update to the checkboxes.
What I've got so far is a form that submits the checkbox values to the database, a controller, and a model):
Form
<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" />
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>
Controller
function addFoo()
{
if ($this->input->post('submit')) {
$id = $this->input->post('id');
$foo1 = $this->input->post('foo1');
$foo2 = $this->input->post('foo2');
$foo3 = $this->input->post('foo3');
$foo4 = $this->input->post ('foo4');
$this->load->model('foo_model');
$this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
}
}
Model
function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
$data = array(
'id' => $id,
'foo1' => $foo1,
'foo2' => $foo2,
'foo3' => $foo3,
'foo4' => $foo4
);
$this->db->insert('foo_table', $data);
}
At your Controller :
if you want to insert new entry for all selected checkbox:
foreach($this->input->post('foo') as $r)
{
$data['fieldname']=$r;
$this->model_name->insert($data);
}
if you want to insert all selected checkbox values in different fields within single entry then,
foreach($this->input->post('foo') as $key=>$val)
{
$data['field'.$key]=$val;
}
$this->model_name->insert($data);
Well in reference to setting the values, a checkbox doesn't send anything if unchecked. To achieve what you want, you have to do this:
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="2" />
This will send a value regardless of whether the checkbox is checked or not.
use the different values for each checkbox and get the value of checkbox array and use this in your controller print_r($this->input->post('foo')); it will print the values that are selected by user
use like this
<?php
if (isset($_POST['submit'])){
print_r($_POST['foo']);
}
?>
<form action="" method="POST">
<input type="checkbox" name="foo[]" value="1">1<br>
<input type="checkbox" name="foo[]" value="2">2<br>
<input type="checkbox" name="foo[]" value="3">3<br>
<input type="checkbox" name="foo[]" value="4">4<br>
<input type="submit" value="submit" name="submit">
</form>
Related
I want to create a feature to filter data. I use the checkbox to do this.
But I want when the checkbox is selected and then the user submits to filter the data, the previously selected checkbox remains selected.
i tried to do it like this but it didn't work
<input type="checkbox" id="ac" name="ac" value="ac" #if(old('ac')) checked #endif>
my form method to submit this is GET.
old() helper function is only for saved flash session data. Without saved it on flash session you can not retrieve it. An example to save flash data for old() function
request()->flashOnly(['ac']);
or
return redirect('form')->withInput();
If you use GET method and no redirection after submit you can do it using request() helper function. Like this:
<input type="checkbox" id="ac" name="ac" value="ac" #if(request()->ac) checked #endif>
For the submitting form using POST method with multiple checkboxes,
#foreach ($categories as $index => $category)
<div class="form-check form-check-inline">
<input type="checkbox" name="type[]"
value="{{ $category->id }}"
{{ (is_array(old('type')) && in_array($index + 1, old('type'))) ? 'checked' : '' }}
class="form-check-input #error('type') is-invalid #enderror"
id="type_{{$category->id}}">
<label class="form-check-label" for="type_{{$category->id}}">
{{ $category->name }}
</label>
</div>
#endforeach
This worked form me:
<input name="mail" type="hidden" value="0" >
<input name="mail" type="checkbox" value="1"#if(old('mail')) checked #endif>
controller:
$request->validate([
'mail' => 'required',
]);
Contact::create($request->all());
How can i on click on submit send values to my custom mysql table??
Form html:
<form action="/ekz.php" method="post">
<input type="text" name="jjkk1">
<input type="text" name="jjkk2">
<input type="text" name="jjkk3">
<input type="text" name="jjkk4">
<input type="submit" name="submit">
</form>
Table name is wp_ekz2020:
Try doing like this :
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$a=$_POST['jjkk1'];
$b=$_POST['jjkk2'];
$c=$_POST['jjkk3'];
$d=$_POST['jjkk4'];
$wpdb->insert( 'wp_ekz2020', array( 'num1' => $a, 'num2' => $b,'num3' => $c,
'num4' => $d), array( '%s', '%s','%s', '%s' ) );
}
?>
please try to place both html and php in single file
here is code i have try and it works
<form action="<?php the_permalink(); ?>" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="text" name="num3">
<input type="text" name="num4">
<input type="submit" name="submit">
</form>
if(isset($_POST['submit']))
{
function insertnumber(){
global $wpdb;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$table_name = "newtab";
$wpdb->insert( $table_name, array(
'num1' => $num1,
'num2' => $num2,
'num3' => $num3,
'num4' => $num4
) );
}
insertnumber();
}
put <?php the_permalink(); ?> in your form action
i hope above code will help you : )
change table name and also variable
in this case, whenever you submit the form with action attribute then the page gets reloaded, and '$wpdb' returns null. So this will cause error.
To prevent this you have to include 'wp-load.php' in your file or you can use
add_action('init', 'your_function_name')
how to create batch insert
view
two input number check and tow input price check and tow input date check
<input type='text' name="number_check[]">
<input type='text' name="price_check[]">
<input type='text' name="date_check[]">
<input type='text' name="number_check[]">
<input type='text' name="price_check[]">
<input type='text' name="date_check[]">
controller
I don't know what to write ?????
Yii::$app->db->createCommand()->batchInsert('sale_check',[
'number_check',
'price_check',
'date_check',
'user_id',
'sale_id',
],$data)->execute() ;
Looking to you sample where you have just two set of same fields you shoul populate the $data array and perform execute() of db command
$post = Yii::$app->request->post();
for ($i=0; $i<2; $i++){
$data[$i][0] = $post[number_check][$i];
$data[$i][1] = $post[price_check][$i];
$data[$i][2] = $post[date_check][$i];
$data[$i][3] = Your_value_for_user_id;
$data[$i][4] = Your_value_for_sale;
}
Yii::$app->db->createCommand()->batchInsert('sale_check',[
'number_check',
'price_check',
'date_check',
],
$data
)->execute();
The following is written in ASP.NET Razor.
I have a page that dynamically creates lines in an HTML form based on results from a database query. Each row in the result set corresponds to one row in the form. Each row has two input fields that I need to be able to pass via $_POST.
foreach (var #row in list) {
<li>
<span style="width: 100px; display: inline-block">#row.Name</span>
<input type="text" name="time[]" />minutes on
<input type="text" name="date[]" />
</li>
}
My expectation is that I would then be able to access and iterate over the data like this:
var timeList = Request.Form["time"];
foreach (var time in timeList) {
// Stuff
}
But this isn't working. I keep getting an "Object reference not set to an instance of an object" error for timeList. Is what I'm trying to do even possible? Any idea what's wrong?
Try removing the square brackets from your names:
foreach (var #row in list) {
<li>
<span style="width: 100px; display: inline-block">#row.Name</span>
<input type="text" name="time" />minutes on
<input type="text" name="date" />
</li>
}
The loop will create duplicate names which forms arrays in HTML, so you get what you want. However, I'm not sure if that will work when the list has a single item, so test that.
Client HTML:
<form action="~/Page" method="post">
<input type="hidden" name="product_0_id" value="0" />
<input type="text" name="product_0_name" value="Prod. #1" />
<input type="hidden" name="product_1_id" value="1" />
<input type="text" name="product_1_name" value="Prod. #2" />
</form>
Server:
string youGroupKey = "product";
Dictionary<string, string>[] values = Request.Form
.AllKeys
.Where(k => k.StartsWith(youGroupKey))
.GroupBy(k => k
.Substring(youGroupKey.Length + 1, 1),
(a, b) => {
return b
.ToDictionary(c => c
.Substring(youGroupKey.Length + 3), d => Request.Form[d]);
}
)
.ToArray();
I've setup a checkbox form (each question has only one checkbox). The form is submitting, but it only sends zeros to mysql--whether the box has been checked or not. How can I get the correct values (1 or 0) sent to mysql?
Built in codeigniter/mysql.
FORM
<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" /> //I tried this w/values incl; still zeros
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>
CONTROLLER
function addFoo()
{
if ($this->input->post('submit')) {
$id = $this->input->post('id');
$foo1 = $this->input->post('foo1');
$foo2 = $this->input->post('foo2');
$foo3 = $this->input->post('foo3');
$foo4 = $this->input->post ('foo4');
$this->load->model('foo_model');
$this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
}
}
MODEL
function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
$data = array(
'id' => $id,
'foo1' => $foo1,
'foo2' => $foo2,
'foo3' => $foo3,
'foo4' => $foo4
);
$this->db->insert('foo_table', $data);
}
<input type="checkbox" name="foo2" value="" /> <-- needs a value. Your DB inserts a zero if it's empty.
I think the issue is likely on your DB end, not the form-end.