Error
Submit button from the view does not pass the value.
After submit button is clicked, then pass value from view to controller, then to the model. Database structure consists of table(booking) and column name ( booked_id, name and mobile) . But the value is not passed to the controller neither to the database.
View
<form method="post" action="">
<input type="text" value="<?php echo isset($post)?$post->name:''; ?>" name="manakamana[name]"/>
<input type="text" value="<?php echo isset($post)?$post->mobile:''; ?>" name="manakamana[mobile]"/>
<button type="submit" class="btn btn-success">POST</button>
</form>
Controller
public function bookManakamana(){
if ($data = $this->input->post('manakamana')) {
$data['created_on'] = date('Y-m-d H:i:s');
$this->booking->add($data);
redirect('booking');
} else {
$this->load->view('index');
}
}
Model
function add($data)
{
$this->db->insert('booking', $data);
}
Form action is not given
<form method="post" action="<?php echo base_url().'controller name/function name' ?>">
<input type="text" value="<?php echo isset($post)?$post->name:''; ?>" name="name"/>
<input type="text" value="<?php echo isset($post)?$post->mobile:''; ?>" name="mobile"/>
<button type="submit" class="btn btn-success">POST</button>
in controller
public function bookManakamana(){
if ($this->input->post('name')) {
$data['created_on'] = date('Y-m-d H:i:s');
$data['name'] = $this->input->post('name');
$data['mobile'] = $this->input->post('mobile');
$this->booking_models->add($data); //booking_model is the name of model
redirect('booking');
} else {
$this->load->view('index');
}}
Related
I write a conversion from celsius to fahrenheit and vice versa. The problem is that in the form fields now random values are displayed to me. How to convert this code so that after entering the value, the converted value in the second field appears in the first field?
Is it possible to use only php at all?
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}
if(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
I want the value entered in the first field to be shown in the second transformed.
You can do all php if you post back to itself. If the page with the input is calc.php
Add an else to set the value to empty string.
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
} else {
$cel = '';
}
if(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
} else {
$fah = '';
}
Try something like this
$cel="";
$fah="";
if(isset($_POST['fah']) && !empty($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}
if(isset($_POST['cel']) && !empty($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
You can try this example:
$celValue = $_POST['cel'];
$fahValue = $_POST['fah'];
if( ! empty($fahValue)){
$celValue = fahrenheitToCelcius($_POST['fah']);
}
if( ! empty($celValue)){
$fahValue = celciusToFahrenheit($_POST['cel']);
}
function celciusToFahrenheit($cel) {
return ($cel - 32) * 1.8;
}
function fahrenheitToCelcius($fah) {
return ($fah + 32) / 1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $celValue; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $fahValue; ?>" name="cel">
<input type="submit" value="Calc">
</form>
Since both the variables give isset() true so we can have something like this
if(isset($_POST['fah']) && isset($_POST['cel'])) {
//if cel is not empty
if(!empty($_POST['cel'])) {
$cel = $_POST['cel'];
$fah=($cel-32)*1.8;
} else if(!empty($_POST['fah']){
$fah = $_POST['fah'];
$cel = ($fah+32)/1.8;
}
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
You just missing set default values that will overwrite if other post value exist, and else if to load only one parameter so other will be empty
<?php
$cel = "";
$fah = "";
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}elseif(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
I'm having some challenge updating table a form joined with session userdata from another table. Each time I run, it displays undefined function. Please let me know where I'm getting it wrong so could fix it.Please find time to review it
I'm a newbie.
Here's the controller
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$id = $this->session->userdata('id');
$this->load->model('Userinfo_model');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
Model
function get_users($userid)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
/* all the queries relating to the data we want to retrieve will go in here. */
$query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");
return $query;
}
function update_user($id,$data){
if (isset($this->session->userdata['logged_in'])) {
$data = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
$this->db->where('userId', $data);
$this->db->update('walletbalance', $id);
}
View
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
Error message
Message: Undefined property: Userinfo::$Userinfo_model
and
Message: Call to a member function update_user() on null
Thanks, I'm grateful
change your view as follows :
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
</form>
form will send data to server only if the element has a name
and you cannot submit a form multiple times. The above code will create update button for each row. So if you want to update all the records in a single updation, use update_batch() in codeigniter. and change view as follows :
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<?php } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">
for reference : https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
In your update_user function you didn't include the usermain_info model. That is why the error is coming.
Please create constructor function and include the model in it. I have updated your code. Try with this.
<?php
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Userinfo_model');
}
public function index(){
if ( $this->session->userdata('logged_in') ){
$id = $this->session->userdata('id');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
?>
I am trying to update the data to the database by php CodeIgniter and display the result in the same page during update. Below is my code.The problem is it didn't show any error message and didn't update the values in the database and It is not displaying any values in the page after update. Please help.
View Page
<?php
echo form_open('Welcome/service1');
?>
<input type="hidden" name="servicer_id" value="<?php echo $value['service_id']?>">
<input type="hidden" name="servicer_name" value="<?php echo $value['servicer_name']?>">
<input type="hidden" name="servicer_email" value="<?php echo $value['servicer_email']?>">
<input type="hidden" name="servicer_checkin" value="<?php echo $value['servicer_checkin']?>">
<input type="hidden" name="servicer_checkout" value="<?php echo $value['servicer_checkout']?>">
<input type="hidden" name="servicer_requestdate" value="<?php echo $value['servicer_requestdate']?>">
<input type="hidden" name="servicer_detail" value="<?php echo $value['servicer_detail']?>">
<input type="hidden" name="servicer_contact" value="<?php echo $value['servicer_contact']?>">
<input type="hidden" name="servicer_priority" value="<?php echo $value['servicer_priority']?>">
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Complete") {?> checked="checked"<?php } ?> name="current_status" value="Complete">Complete
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Ongoing") {?> checked="checked"<?php } ?> name="current_status" value="Ongoing">Ongoing
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Rejected") {?> checked="checked"<?php } ?> name="current_status" value="Rejected">Rejected
</label>
<button type="submit" class="btn btn-default">
<span>Status</span>
</button>
<?php
echo form_close();
?>
Controller page
public function service1()
{
$this->load->helper('url');
$page_id =$this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status();
$data['s']=$this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service.php',$data);
}
Model page
public function add_status()
{
this->load->database();
this->load->helper('url');
hotel_id=1;
servicer_name= $this->input->post('servicer_name');
servicer_email= $this->input->post('servicer_email');
servicer_checkin= $this->input->post('servicer_checkin');
servicer_checkout= $this->input->post('servicer_checkout');
servicer_requestdate= $this->input->post('servicer_requestdate');
servicer_detail= $this->input->post('servicer_detail');
servicer_contact= $this->input->post('servicer_contact');
servicer_priority= $this->input->post('servicer_priority');
status= $this->input->post('status');
service_id= $this->input->post('servicer_id');
data=array('hotel_id'=>$hotel_id,'servicer_name'=>$servicer_name,'servicer_email'=>$servicer_email,'servicer_checkin'=>$servicer_checkin,'servicer_checkout'=>$servicer_checkout,'servicer_requestdate'=>$servicer_requestdate,'servicer_detail'=>$servicer_detail,'servicer_contact'=>$servicer_contact,'servicer_priority'=>$servicer_priority,'status'=>$status);
this->db->where('service_id',$service_id);
this->db->update('service_hotel1',$data);
}
You are passing inputs to the model but they are getting passed to the controller function.. please update
Controller:
public function service1()
{
/** you are posting the form to this function and not to the model.
* which is why all input->post must be here
*/
$servicer_name = $this->input->post('servicer_name');
$servicer_email = $this->input->post('servicer_email');
$servicer_checkin = $this->input->post('servicer_checkin');
$servicer_checkout = $this->input->post('servicer_checkout');
$servicer_requestdate= $this->input->post('servicer_requestdate');
$servicer_detail = $this->input->post('servicer_detail');
$servicer_contact = $this->input->post('servicer_contact');
$servicer_priority = $this->input->post('servicer_priority');
$status = $this->input->post('status');
$service_id = $this->input->post('servicer_id');
$this->load->helper('url');
$page_id = $this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id);
$data['s'] = $this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service',$data); //also view must be loaded without .php so $this->load->view('App_stay/pages/hotel1_service',$data);
}
Model:
public function add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id)
{
$this->load->database();
$this->load->helper('url');
$hotel_id =1 ;
$data = array(
'hotel_id' => $hotel_id,
'servicer_name' => $servicer_name,
'servicer_email' => $servicer_email,
'servicer_checkin' => $servicer_checkin,
'servicer_checkout' => $servicer_checkout,
'servicer_requestdate' => $servicer_requestdate,
'servicer_detail' => $servicer_detail,
'servicer_contact' => $servicer_contact,
'servicer_priority' => $servicer_priority,
'status' => $status
);
$this->db->where('service_id',$service_id);
$this->db->update('service_hotel1',$data);
}
I can't update data in a record in CodeIgniter . Instead of updating it's adding new record in database table.'hotelres_id' is the primary key of my table.
I have posted the code below:-
Controller code for update:-
function edit($id){
$this->load->model('hotel_reservation','mb');
$data['message'] = '';
$data['object'] = $this->mb->find_by_id($id);
if($data['object']){
$this->form_validation->set_rules('roomno', 'Room Number', 'required|is_unique[hotel_reservation.roomno]');
$this->form_validation->set_rules('checkin', 'Check In', 'required|is_unique[hotel_reservation.checkin]');
$this->form_validation->set_rules('checkout', 'Check Out', 'required|is_unique[hotel_reservation.checkout]');
if ($this->form_validation->run() == TRUE){
$this->mb->eventreg_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
$this->mb->eventhotel_id = $_POST['eventhotel_id'];
$this->mb->roomno = $_POST['roomno'];
$this->mb->checkin = $_POST['checkin'];
$this->mb->checkout = $_POST['checkout'];
$this->mb->comment = $_POST['comment'];
$this->mb->update();
$data['message'] = 'Details updated successfully';
$data['object'] = $this->mb;
}
$this->load->view('core/hotel_reservation/edit',$data);
}
else{
$data['message'] = 'No details available!! Fill It!!';
$this->load->view('core/hotel_reservation/save',$data);
}
}
View Code :-
<html>
<head>
<title>Hotel Reservation</title>
</head>
<body>
<h2>Hotel Reservation</h2>
<?php if(isset($message)&&$message!='') echo "<span class=\"message\">{$message}</span>"; ?>
<form action="<?php echo site_url('core/hotel_re/edit/'.#$object->hotelres_id); ?>" method="POST" >
<table class="formtable">
<tr><td>hotelres_id</td><td><input type="text" name="hotelres_id" id="hotelres_id" class="textbox" value="<?php echo #$object->hotelres_id; ?>" readonly></td></tr>
<tr><td>eventreg_id</td><td><input type="text" name="eventreg_id" class="textbox" value="<?php echo #$object->eventreg_id; ?>" readonly></td></tr>
<tr><td>eventhotel_id</td><td><input type="text" name="eventhotel_id" class="textbox" value="<?php echo #$object->eventhotel_id; ?>" readonly></td></tr>
<tr><td>Room Number</td><td><input type="text" name="roomno" value="<?php echo #$object->roomno; ?>" class="textbox" ></td></tr>
<tr><td>Check In</td><td><input type="text" name="checkin" value="<?php echo #$object->checkin; ?>" class="textbox"></td></tr>
<tr><td>Check Out</td><td><input type="text" name="checkout" value="<?php echo #$object->checkout; ?>" class="textbox"></td></tr>
<tr><td>Comment</td><td><textarea type="text" name="comment" value="<?php echo #$object->comment; ?>" class="textarea" ></textarea></td></tr>
<tr><td> </td><td><input type="submit" name="submit" value="Update" class="submitbutton"></td></tr>
</table>
</form>
<span class="validation-errors"><?php echo validation_errors(); ?></span>
</body>
</html>
Model code:-
<?php
class hotel_reservation extends CI_Model{
var $hotelres_id;
var $eventreg_id;
var $eventhotel_id;
var $roomno;
var $checkin;
var $checkout;
var $comment;
static $tablename = 'hotel_reservation';
static $tableid = 'hotelres_id';
function find_by_id($id)
{
$tableid = self::$tableid;
$resultset = $this->db->get_where(self::$tablename,array($tableid=>$id),1);
if($resultset->num_rows()==1)
return array_shift($resultset->result(get_class($this)));
return false;
}
function find_all()
{
$resultset = $this->db->get(self::$tablename);
return $resultset->result(get_class($this));
}
function save()
{
$tableid = self::$tableid;
if(isset($this->$tableid)&&$this->$tableid!=''&&$this->$tableid!=0)
$this->update();
else
$this->insert();
}
private function insert()
{
$this->db->insert(self::$tablename,$this);
}
function update()
{
$tableid = self::$tableid;
$this->db->where($tableid,$this->$tableid);
$this->db->update(self::$tablename,$this);
}
function delete()
{
$tableid = self::$tableid;
$this->db->where($tableid,$this->$tableid);
$this->db->delete(self::$tablename);
}
}
The mistake is here!!
In your controller, you have done this...
$this->mb->eventreg_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
This should be this
$this->mb->hotelres_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
And like i Said, the update term should be written like this...
$this->db->where($tableid,$this->hotelres_id);
Solved?
From what i can see, i think your issue is with this line:
$this->db->update(self::$tablename,$this);
As you should pass the update function an array with matching keys to your TB columns and values to be updated, with you send the whole object ($this), doesn't also send tableid and tablename?
According to your code ($this->mb->update();), it is calling mb model. But you have provided code for hotel_reservation model. May be some mismatch there.
how to display the result after submit the form
i want a display result after submit the form for print
example 1st im filling the form submit the result after the submit i want a screen to display same result
http://www.tizag.com/phpT/examples/formexample.php
how can i do this
please help me to fix this issue.
php form code
<?php
function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
</p>
<p><span class="style9"><strong>Roll No :</strong></span><strong> *</strong>
<input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
</p>
<p><span class="style9"><strong>Class:</strong></span><strong> *</strong>
<input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees Date :</strong></span><strong> *</strong>
<input id="fullDate" name="date" type="text" value="<?php echo $date; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees :</strong></span><strong> *</strong>
<input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
</p>
<span class="style9"><strong>Reference</strong></span><strong> *</strong>
<input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
<br/>
<p class="style1">* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
$class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
$fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));
// check to make sure both fields are entered
if ($grn == '' || $name == '' || $rollno == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
}
else
{
// save the data to the database
mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
or die(mysql_error());
echo "<center>KeyWord Submitted!</center>";
// once saved, redirect back to the view page
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','');
}
?>
Not quite shure what you are asking.
do you need help displaying the submited form data? or more spesific display for print?
to display it you would just need to make a html page that displays it.
like:
echo '<table><tr>';
echo '<td>';
echo '<Strong>Name:</strong><br/>';
echo $name;
echo '</td>';
echo '</tr></table>';
To display just the result and not the form, when you post to the same page you need to encapsulate the for code with an if statement.
if(isset($_POST['submit'])) {
//code for the php form
}else {
//code to display form
}
Use a new page for the "action" part in the form. On that new page, simply echo the $_POST of values you want to display. If you plan on making some sort of page so that people can check their entries, you could store these $_POST-data in sessions.
Simply call function on the bottom to disply posted values :
function showFormValues()
{
?>
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<?php echo $_POST['grn']; ?>
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<?php echo $_POST['name'];
</p>
and so on.
.
.
.
.
</div>
<?php
}
?>