Outputted a row from a table ,am now trying to add some of the contents to another table using a form which is inside the outputted row but its not inserted to the database. am not getting an error.
Here is my view
<?php
foreach ($h->result() as $row)
{?>
<div class="row invoice-info">
<div class="col-sm-1 invoice-col">
</div>
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/res/images/goods/1.png">
</div>
<div class="col-sm-4 invoice-col">
<address>
Description: <?php echo $row->description;?><br>
Location Address: <?php echo $row->l_area;?><br>
Destination Address: <?php echo $row->d_area;?><br>
Date: <?php echo $row->dom;?><br>
Time: <?php echo $row->tom;?>
</address>
</div>
<div class="col-sm-2 invoice-col">
<address>
</address>
</div><!-- /.col -->
<div class="col-sm-2 invoice-col">
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</div>
</div>
My model
function bid($data){
$query=$this->db->update('bids',$data);
return $query;
}
My controller
public function bid(){
$this->load->database();
$this->load->model('Truckeraccount_model');
$data['a']=$this->Truckeraccount_model->accepted_bid();
$data['b']=$this->Truckeraccount_model->picked_loads();
$data['h']=$this->Truckeraccount_model->loads();
$data['g']=$this->Truckeraccount_model->notification();
$data['i']=$this->Truckeraccount_model->return_loads();
$data['accepted_return_loads']=$this->Truckeraccount_model->accepted_return_loads();
$data['bid_amount']=$this->Truckeraccount_model->bid_amount(); $this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('bid_amount', 'bid_amount', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('truckeraccount_view',$data);
$this->load->view('footer');
} else {
$data = array(
'truckerid' => $this->input->post('truckerid'),
'luggage_id' => $this->input->post('luggage_id'),
'bid_amount' => $this->input->post('bid_amount'),
);
$this->Truckeraccount_model->bid($data);
$data['message'] = '';
redirect('truckeraccount_ctrl/');
}
}
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</form>
Related
Good day. I just wanna ask how can I insert the data from the forms generated from a while loop. This is what I tried so far. I added a loop where they will have different id or names but when I tried on clicking the button the first form is the only one working. Thank you so much in advance.
<?php
include "../config/dbconfig.php";
$data['productCode'] = "1"; // sample data
$stmt = $conn->prepare("SELECT * FROM tbl_category");
//$stmt->bind_param("i", $data['productCode']);
$stmt->execute();
$result = $stmt->get_result();
$i = 1;
while ($stuff = $result->fetch_assoc()) {
?>
<div class="col-sm-6" style="margin-top:20px;">
<div class="card">
<div class="card-header"><?php echo $stuff['categoryname']; ?>
</div>
<div class="card-body outermydiv">
<div class="myDIV">
<form method="POST" name="itemform" action="">
<div class="form-row">
<div class="col-5">
<input type="text" class="form-control" name="name[<?php echo $i; ?>]" id="itemname[<?php echo $i; ?>]" placeholder="Item name" required autocomplete="off">
</div>
<div class="col">
<input type="number" class="form-control" name="cost[<?php echo $i; ?>]" id="itemcost[<?php echo $i; ?>]" placeholder="Cost" required>
</div>
<div class="col">
<input type="number" class="form-control" name="price[<?php echo $i; ?>]" id="itemprice[<?php echo $i; ?>]" placeholder="Price" required>
<input type="hidden" class="form-control" name="code[<?php echo $i; ?>]" id="forcatcode[<?php echo $i; ?>]" value="<?php echo $stuff['categorycode'] ?>">
</div>
<div class="col">
<button type="submit" class="btn btn-success" name="btnsaveitem" id="btnsaveitem">Save</button>
</div>
<?php $i++; ?>
<input type="hidden" name="count" value="<?php echo $i; ?>" />
</div>
</form>
</div>
<br>
</div>
</div>
</div>
<?php
}
?>
and here is my insert code
<?php
if (isset($_POST['btnsaveitem'])) {
$count = $_POST['count'];
for ($i = 1; $i < $count; $i++) {
//$code = $_POST['code'][$i]; // check empty and check if interger
$foritemname = $_POST['name'][$i]; // check empty and strip tags
//$qty = $_POST['qty'][$i]; // check empty and check if interger
$stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES ('".$foritemname."')");
//$stmt->bind_param("iss",$name);
$stmt->execute();
}
}
?>
Starting by rewriting your form...
You don't need $i for anything, but I'll leave the declaration in case you want it for something else.
Don't submit array type data, each form will submit its own set of fields.
It probably makes more sense to add $stuff['categorycode'] as the value of each submit to avoid needing the hidden field. I'll leave it your way for now.
Form:
foreach ($stmt->get_result() as $i => $stuff) { ?>
<div class="col-sm-6" style="margin-top:20px;">
<div class="card">
<div class="card-header"><?php echo $stuff['categoryname']; ?></div>
<div class="card-body outermydiv">
<div class="myDIV">
<form method="POST">
<div class="form-row">
<div class="col-5">
<input type="text" class="form-control" name="name" placeholder="Item name" required autocomplete="off">
</div>
<div class="col">
<input type="number" class="form-control" name="cost" placeholder="Cost" required>
</div>
<div class="col">
<input type="number" class="form-control" name="price" placeholder="Price" required>
</div>
<div class="col">
<button type="submit" class="btn btn-success" name="btnsaveitem">Save</button>
</div>
</div>
<input type="hidden" class="form-control" name="code" value="<?php echo $stuff['categorycode']; ?>">
</form>
</div>
<br>
</div>
</div>
</div>
<?php
}
Receiving script: (extend with additional fields as desired)
if (isset($_POST['btnsaveitem'])) {
$stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES (?)");
$stmt->bind_param("s",$_POST['name']);
$stmt->execute();
}
This is all untested code.
I am trying to insert the data to the database by php CodeIgniter and display the result in the same page during submit the button. Below is my code.The problem is i got an error message as Duplicate entry '104' for key 'PRIMARY'. Please help. View Page
<div class="container col-lg-4">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url();?>index.php/Welcome/gallery1" method="post">
<div class="form-group has-info">
<?php
foreach ($h->result_array() as $value){
?>
<input type="hidden" name="id" value="<?php echo $value['offer_id'];?>" >
<?php }?>
<br>
<label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
<input type="text" class="form-control col-lg-6" name="offered" id="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered']; ?>">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
<textarea id="description" name="description" class="form-control col-lg-6" rows="3" value="<?php if(isset($_POST['description'])) echo $_POST['description']; ?>" ></textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4"> <span>SUBMIT</span>
</button>
</div>
</div>
</form>
Controller page
public function gallery1()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('offered','offered','required');
$this->form_validation->set_rules('description','description','required');
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select();
$this->load->view('App_stay/pages/hotel1_galery.php',$data);
if($this->form_validation->run() == FALSE)
{
}
else
{
$data['query']=$this->Login_set->add_offer();
if($data['query']!=NULL)
{
$data['ins_msg']='data inserted';
}
else
{
$data['ins_msg']='data not inserted';
}
}
}
Model Page
public function add_offer()
{
$this->form_validation->set_message('offered','offered','required');
$this->form_validation->set_message('description','description','required');
$this->load->database();
$this->load->helper('url');
$data=array
(
'offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
$this->db->insert('offer_hotel1',$data);
}
I have simple code thats not working. I made validation for checkbox and I get error:
Unable to access an error message corresponding to your field name accept_terms_checkbox.(accept_terms)
This is my code:
Controller:
public function formularz2()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['szkolenia'] = $this->Szkolenie_m->pobierz();
//VALIDATION!!
$this->form_validation->set_rules('imie', 'Imię', 'required');
$this->form_validation->set_message('required', 'Błąd: wypełnij powyższe pole');
$this->form_validation->set_rules('accept_terms_checkbox', 'checkbox', 'callback_accept_terms');
//CALLBACK FUNCTION!!
function accept_terms()
{
if ($this->input->post('accept_terms_checkbox'))
{
return TRUE;
}
else
{
$error = 'Please read and accept our terms and conditions.';
$this->form_validation->set_message('accept_terms', $error);
return FALSE;
}
}
//Po przesłaniu danych
if (!empty($_POST))
{
$konsultant = $this->uri->segment(3);
$dane = array(
'email1' => $this->input->post('email'),
'imie' => $this->input->post('imie'),
'nazwisko' => $this->input->post('nazwisko'),
'nazwa_firmy' => $this->input->post('firma'),
'konsultant_id' => $konsultant,
);
//Trzeba jeszcze tutaj ogarnąć wrzucanie pivotów zaznaczonych checkboxów
$boxes = $_POST['formChecks'];
$N = count($boxes);
$ostatni_id = $this->Osoby_m->ostatni_id();
for($i=0; $i < $N; $i++)
{
$this->Osoby_m->nowy_wpis_formularz($boxes[$i]);
}
//Pobrać id dodanej osoby = policzyć ile osób w bazie +1
$this->Osoby_m->nowa_osoba($dane);
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formularz', $data);
}
else
{
$this->load->view('wyslano_formularz');
}
}
My View:
<?php echo form_open(); ?>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>">
<p class="help-block"><?php echo form_error('email'); ?></p>
</div>
</div>
<div class="form-group">
<label for="imie" class="col-sm-2 control-label">Imię:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="imie" name="imie" placeholder="Imię" value="<?php echo set_value('imie'); ?>">
<p class="help-block"><?php echo form_error('imie'); ?></p>
</div>
</div>
<div class="form-group">
<label for="nazwisko" class="col-sm-2 control-label">Nazwisko: </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nazwisko" name="nazwisko" placeholder="Nazwisko" value="<?php echo set_value('nazwisko'); ?>">
<p class="help-block"><?php echo form_error('nazwisko'); ?></p>
</div>
</div>
<div class="form-group">
<label for="firma" class="col-sm-2 control-label">Firma:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firma" name="firma" placeholder="Firma" value="<?php echo set_value('firma'); ?>">
<p class="help-block"><?php echo form_error('firma'); ?></p>
</div>
</div>
<div class="col-sm-12" style="margin-bottom: 30px; margin-top: 30px;"> <h3>Wybierz interesujące Cię szkolenia</h3></div>
<div class="form-group">
<label class="col-sm-2 control-label">Szkolenia:</label>
<div class="col-sm-10">
<?php foreach ($szkolenia as $szkolenie): ?>
<div class="checkbox">
<label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo $szkolenie->id; ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
<br>
</div>
<?php endforeach; ?>
</div>
</div>
//CALLBACK CHECKBOX
<div class="form-group">
<div class="col-sm-12" style="margin-top: 50px;">
<input type="checkbox" name="accept_terms_checkbox" value="1"/> Zgadzam się na otrzymywanie maili od firmy Gamma<br>
<p><?php echo form_error('accept_terms_checkbox') ?></p>
</div>
</div>
<button type="submit" class="btn btn-lg btn-success" style="margin-top: 50px;">Odbierz Voucher</button>
<?php echo form_close(); ?>
In order to use your own callback method with codeigniter's validation class, you must declare the method. IN your case ( add this to your controller or a helper etc...):
public function accept_terms($user_input){
if($user_input != ""){
return True;
}else{
$this->form_validation->set_message('accept_terms_checkbo', 'The %s field can not be empty"');
return False;
}
I fixed this error by simple validation without callback function. I figure out that I don't need callback function for Terms of Services checkbox.
$this->form_validation->set_rules('accept_terms_checkbox', 'Checkbox', 'required');
$this->form_validation->set_message('required', 'You need to accept Terms of Services');
I've been building a site recently for a friend and I've gotten stuck on this one form. A button links to url in which this form is on and then once you fill out all the information and click submit, instead of returning you back to home.php it just removes the form from view and all you see is a blank new.php and it doesn't submit the information.
<?php
function renderForm($user, $rank, $position, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<center>
<form action="" method="post">
<div class="form-group">
<label for="username">Username*</label>
<input id="username" class="form-control" type="text" name="user" placeholder="Username" value="<?php echo $user; ?>" />
</div>
<div class="form-group">
<label for="rank">Rank</label>
<select class="form-control" name="rank">
<option value="1">Pending Rank</option>
<option value="2">PVT</option>
</select>
</div>
<div class="form-group">
<label for="position">Position</label>
<input id="position" class="form-control" type="text" name="position" placeholder="MOG/GG" value="<?php echo $position; ?>" />
</div>
<div class="form-group">
<label for="Date">Date*</label>
<input id="Date" class="form-control" type="text" name="date" placeholder="<?php echo date('d M y'); ?>" value="<?php echo $date; ?>" />
</div>
<div class="form-group">
<label for="Tag">Tag*</label>
<input id="Tag" class="form-control" type="text" name="tag" placeholder="[]" value="<?php echo $tag; ?>" />
</div>
<div class="form-group">
<label for="adt">ADT</label>
<input id="adt" class="form-control" type="text" name="adt" placeholder="{TEST}" value="<?php echo $adt; ?>" />
</div>
<div class="form-group">
<label for="exp">EXP</label>
<input id="exp" class="form-control" type="text" name="exp" placeholder="420" value="<?php echo $exp; ?>" />
</div>
<div class="form-group">
<label for="reg">Regiment</label>
<input id="reg" class="form-control" type="text" name="reg" placeholder="[P]" value="<?php echo $reg; ?>" />
</div>
<div class="form-group">
<label for="Notes">Notes</label>
<input id="Notes" class="form-control" type="text" name="notes" placeholder="Notes" value="<?php echo $notes; ?>" />
</div>
<button type="submit" name="submit" class="btn btn-default" value="Submit">Submit</button>
</form>
<script>
$('.modal').on('hidden.bs.modal', function(){
$(this).find('form')[0].reset();
});
</script>
<?php
}
include('config/db.php');
if (isset($_POST['submit']))
{
$user = mysql_real_escape_string(htmlspecialchars($_POST['user']));
$rank = mysql_real_escape_string(htmlspecialchars($_POST['rank']));
$position = mysql_real_escape_string(htmlspecialchars($_POST['position']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$tag = mysql_real_escape_string(htmlspecialchars($_POST['tag']));
$adt = mysql_real_escape_string(htmlspecialchars($_POST['adt']));
$exp = mysql_real_escape_string(htmlspecialchars($_POST['exp']));
$reg = mysql_real_escape_string(htmlspecialchars($_POST['reg']));
$notes = mysql_real_escape_string(htmlspecialchars($_POST['notes']));
$datej = mysql_real_escape_string(htmlspecialchars($_POST['date']));
if ($user == '' || $rank == '' || $date == '' || $tag == '')
{
$error = '<center>ERROR: Please fill in all required fields!</center>';
#renderForm($user, $rank, $position, $error);
}
else
{
mysql_query("INSERT per SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', adt='$adt', exp='$exp', reg='$reg', notes='$notes', datej='$datej'", $db1)
or die(mysql_error());
include('logsadd.php');
write_mysql_log('has added member <font color="black"><b>'. $user .'</b></font>.', $db);
header("Location: home.php");
}
}
else
header("home.php");
{
#renderForm('','','');
}?>
Your else looks like this
else
header("home.php");
{
#renderForm('','','');
it should be
else
{
// header should be inside the else part
header("Location:home.php");
#renderForm('','','');
I have a view page.i have to edit a list of values.bt after submit i have an error disallowed character key.I cant find it how this error occur.
view
foreach($track_details->result() as $name) { ?>
<form class="form-horizontal" method="post" id="income_edit" action="<?php echo base_url();?>income_list_cntrl/update_income" enctype="multipart/form-data">
<input type="text" name="track_id" value="<?php echo $track_id; ?>" >
<div class="control-group">
<label class="control-label">Income Name</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->income_name?>" name="income_name" id="income_name" readonly>
<?php echo form_error('income_name'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Income Amount</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->income_amount?>" name="income_amount" id="income_amount" >
<?php echo form_error('income_amount'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Crop Quantity</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->crop_quantity?>" name=crop_quantity" id="crop_quantity" >
<?php echo form_error('crop_quantity'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Per Rate</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->per_rate?>" name=per_rate" id="per_rate" >
<?php echo form_error('per_rate'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Choose a client</label>
<div class="controls">
<select class="span6 m-wrap" name="select_client" >
<? foreach ($client_details->result() as $var) { ?>
<option <?php if($name->client_id == $var->client_id) { ?> selected="selected" <? } ?> value="<?echo $var->client_id;?>"><?echo $var->client_name;?></option>
<?}?>
</select>
<?php echo form_error('select_client'); ?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn green" id="Submit">Submit</button>
</form>
<?}?>
controller
After removing the crop quantity and per rate no error but dont edit it.
public function update_income(){
$this->form_validation->set_error_delimiters('<div style="color:#B94A48">', '</div>');
//$this->form_validation->set_rules('income_name', 'Income name', 'required');
$this->form_validation->set_rules('income_amount', 'Income Amount', 'required');
$this->form_validation->set_rules('crop_quantity', 'Crop Quantity', 'required');
$this->form_validation->set_rules('per_rate', 'Per Rate', 'required');
$this->form_validation->set_rules('select_client', 'Client Name', 'required');
if ( $this -> form_validation -> run() === FALSE ){
$this->index();
} else {
$track_id=$this->input->post('track_id');
$income_name=$this->input->post('income_name');
$income_amount=$this->input->post('income_amount');
$crop_quantity=$this->input->post('crop_quantity');
$per_rate=$this->input->post('per_rate');
$client_name=$this->input->post('select_client');
$this->load->model('income_edit_model');
$data = array(
'income_name'=>$income_name,
'income_amount'=>$income_amount,
'crop_quantity'=>$crop_quantity,
'per_rate'=>$per_rate,
'client_name'=>$client_name,
);
$result=$this->income_edit_model->update_track_income($data,$track_id);
// redirect(base_url().'income', 'refresh');
}
}
Two problems:
First: Missing <?php open tags. Look through this and change <? to <?php
<select class="span6 m-wrap" name="select_client" >
<? foreach ($client_details->result() as $var) { ?>
<option <?php if($name->client_id == $var->client_id) { ?> selected="selected" <? } ?> value="<?echo $var->client_id;?>"><?echo $var->client_name;?></option>
<? } ?>
</select>
Second: Missing closing </div
<div class="form-actions">
<button type="submit" class="btn green" id="Submit">Submit</button>
<!-- MISSING </div> HERE -->
</form>