add data using ajax and codeigniter - php

I'm new in codeigniter
I try to add data in table users using ajax when I send data I have now problem and all data send good but no data stored in data base
and this is my code
controller
public function addusersajax()
{
if($this->input->post("action") =="addusersintable")
{
$this->load->helper('date');
$data=array(
"fullname"=> $this->input->post("fullname"),
"username"=> $this->input->post("username"),
"password" => md5($this->input->post("password")),
"email"=>$this->input->post("email"),
"groubid"=>$this->input->post("groubid"),
"date"=>mdate('%Y-%m-%d ', now()),
"time"=>date(" H:i:s")
);
$this->load->model("usersmodel");
if($this->usersmodel->adduserbyajax($data)){
echo "done";
}else{
echo 'failed';
}
}
}
this is function for view form
public function view()
{
$data['pagetitle']="xx";
$this->load->view("template/admin/header",$data);
$this->load->view("users/ss",$data);
$this->load->view("template/admin/footer");
}
this is my view
<div class="container">
<div id="container">
<div class="col-lg-8">
<form id="insercodegn" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-name" class="control-label">Full Name :</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">UserName :</label>
<input type="text" class="form-control" name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="message-text" class="control-label">Password:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
<i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email :</label>
<input type="text" class="form-control" name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">User Type :</label>
<select class="form-control" name="groubid" id="groubid">
<option value="1">Administartor</option>
<option value="0">User</option>
<option value="2">Maker</option>
<option value="3">cheker</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Image :</label>
</div>
<input type="submit" name="action" value="adduserssss">
<button type="submit" name="action" value="addusersintable" class="btn btn-primary">addcc</button>
</form>
</div>
</div>
</div>
****this is my script inside ss viewpage****
<script>
$(document).ready(function () {
$(function () {
$("#insercodegn").on('submit', function (e) {
e.preventDefault();
$.ajax({
url:'<?php echo base_url()?>Users/addusersajax',
//url:"<?php echo base_url() .'Users/addusersajax'?>",
method:'post',
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success:function(data) {
alert(data);
}
})
})
})
})
</script>
my form is
<div class="container">
<div id="container">
<div class="col-lg-8">
<form id="insercodegn" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-name" class="control-label">Full Name :</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">UserName :</label>
<input type="text" class="form-control" name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="message-text" class="control-label">Password:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
<i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email :</label>
<input type="text" class="form-control" name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">User Type :</label>
<select class="form-control" name="groubid" id="groubid">
<option value="1">Administartor</option>
<option value="0">User</option>
<option value="2">Maker</option>
<option value="3">cheker</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Image :</label>
</div>
<button type="submit" name="action" value="adduserssss" class="btn btn-primary">add</button>
</form>
</div>
</div>
</div>

Change this:
<input type="submit" name="action" value="adduserssss">
<button type="submit" name="action" value="addusersintable" class="btn btn-primary">addcc</button>
To this:
<input type="hidden" name="action" value="addusersintable">
<button type="submit" class="btn btn-primary">addcc</button>
Buttons don't have $_POST data thus: if($this->input->post("action") =="addusersintable") is always evaluating to false and never hitting your model. A hidden input is what you are looking for.

1) Use lower case url in your javascript file, try "users/addusersajax" instead of "Users/addusersajax".
2) Use var_dump($_POST) or print_r($_POST) in your controller. in network tab of Chrome or Firefox check to see result of your request. you must see what is posted. if it is all good you must check your model
3) In your model it is best to use transactions. see transactions documentations in Codeigniter user guide. also you can echo $this->db->last_query() to see what query is executed. copy the executed query and run it from phpMyAdmin or any other MySQL client you use and see if you get any error.

Related

I want to update the recorded in data base. I want to send the "employee_id" on clicking save button. so that corresponding 'id' recorded is updated

I want to update the record of the corresponding id that is sent on clicking save button. I tried button tage, input tage and link tage but there is some mistake that I am making, but I don't know where?
This is my form
<form method="POST" action="handle_update.php">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">First name</label>
<input type="text" name="fname" value="'.$first_name.'" class="form-control" >
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Last name</label>
<input type="text" name="last_name" value="'.$last_name.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Email</label>
<input type="email" name="email" value="'.$email.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Designation</label>
<input type="text" name="designation" value="'.$designation.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Address</label>
<input type="address" name="address" value="'.$address.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Date of Joining</label>
<input type="date" name="joining_date" value="'.$joining_date.'" class="form-control">
</div>
<a name="update" role = "button" type="submit" href="handle_update.php?ployee_id='.$id2.'">Save</a>
</form>
Add a hidden input field that holds the value you want to submit. Change your <a> to a <button> that can submit your form. Change your code to:
<form method="POST" action="handle_update.php">
<input type="hidden" name="ployee_id" value="' . $id2 . '">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">First name</label>
<input type="text" name="fname" value="'.$first_name.'" class="form-control" >
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Last name</label>
<input type="text" name="last_name" value="'.$last_name.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Email</label>
<input type="email" name="email" value="'.$email.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Designation</label>
<input type="text" name="designation" value="'.$designation.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Address</label>
<input type="address" name="address" value="'.$address.'" class="form-control">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Date of Joining</label>
<input type="date" name="joining_date" value="'.$joining_date.'" class="form-control">
</div>
<button type="submit" name="update">Save</button>
</form>
More on forms: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
More on hidden inputs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden

can't connect a laravel api.php rout from an html form

I am trying to post a form from a HTML file to a live database using laravel api.php route. But it's yielding the following error :
my api.php looks like :
<?PHP
use Illuminate\Http\Request;
use App\Http\Controllers\User\PageController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('summitRegistration',[PageController::class,'summitRegistration']);
My HTML code :
<form action="https://www.aspireelearning.com/summitRegistration" method="POST">
<div class="col-12 col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="full_name" id="full_name"
placeholder="Your Full Name">
<input type="text" class="form-control" name="phone" id="phone"
placeholder="Phone Number">
<input type="text" class="form-control" name="profession" id="profession"
placeholder="Profession">
</div>
</div>
<div class="col-12 col-md-6">
<div class="form-group">
<input type="email" class="form-control" name="email" id="email"
placeholder="Email Address" required>
<input type="text" class="form-control" name="organization" id="organization"
placeholder="Organization/Institute">
<select class="form-control country-search" name="country" id="country">
<option value="">Your Country</option>
<option value="">USA</option>
<option value="">UK</option>
</select>
</div>
</div>
<div class="col-12 text-center">
<button type="submit" class="btn btn-danger">
CONFIRM
</button>
</div>
</form>
i have update action and also added #csrf token now i hope you will get your out put .
<form action="https://www.aspireelearning.com/api/summitRegistration" method="POST">
//#csrf
<div class="col-12 col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="full_name" id="full_name"
placeholder="Your Full Name">
<input type="text" class="form-control" name="phone" id="phone"
placeholder="Phone Number">
<input type="text" class="form-control" name="profession" id="profession"
placeholder="Profession">
</div>
</div>
<div class="col-12 col-md-6">
<div class="form-group">
<input type="email" class="form-control" name="email" id="email"
placeholder="Email Address" required>
<input type="text" class="form-control" name="organization" id="organization"
placeholder="Organization/Institute">
<select class="form-control country-search" name="country" id="country">
<option value="">Your Country</option>
<option value="">USA</option>
<option value="">UK</option>
</select>
</div>
</div>
<div class="col-12 text-center">
<button type="submit" class="btn btn-danger">
CONFIRM
</button>
</div>
</form>
You have to add api as prefix https://www.aspireelearning.com/api/summitRegistration

Form submit pressing enter (in a site and in another one don't)

I have the following form in index.php:
<form action="#" method="post" class="from" id="inviadati">
<div class="form__group">
<input type="text" class="form__input" placeholder="Cognome*" name="CognomeCliente" id="CognomeCliente" pattern="[a-zA-Z\s]+" required>
<label for="CognomeCliente" class="form__label">Cognome*</label>
</div>
<div class="form__group">
<input type="text" class="form__input" placeholder="Nome*" name="NomeCliente" id="NomeCliente" pattern="[a-zA-Z\s]+" required>
<label for="NomeCliente" class="form__label">Nome*</label>
</div>
<div class="form__group">
<input type="text" class="form__input" placeholder="Indirizzo" name="IndirizzoCliente" id="IndirizzoCliente">
<label for="IndirizzoCliente" class="form__label">Indirizzo</label>
</div>
<div class="form__group">
<input type="text" class="form__input" placeholder="Comune" name="CittaCliente" id="CittaCliente" pattern="[a-zA-Z\s]+">
<label for="CittaCliente" class="form__label">Comune</label>
</div>
<div class="form__group">
<input type="text" class="form__input" placeholder="Telefono*" name="Telefono" id="Telefono" pattern="\d*" required>
<label for="Telefono" class="form__label">Telefono*</label>
</div>
<div class="form__group">
<select class="form__input" name="IDProduct" id="IDProduct" required>
<option value="">Scegli</option>
<option value="3003">2 49,9 €</option>
<option value="3004">3 69,9 €</option>
<option value="3005">4 89,9 €</option>
</select>
<label for="IDProduct" class="form__label">Offerta*</label>
</div>
<div class="form__group">
<input type="text" class="form__input" placeholder="Note per il corriere" name="Note" id="Note">
<label for="Note" class="form__label">Note per il corriere</label>
</div>
<div class="form__group">
<div class="section__contattaci--radio">
<input type="radio" class="form__input section__contattaci--radio-1" name="RitiroCorriere" id="RitiroCorriere" checked>
<label for="RitiroCorriere" class="form__label">Ritiro in sede con corriere convenzionato (<a target="_blank" href="https://benesserefarm.com/termini-e-condizioni/" class="link">condizioni</a>)</label>
</div>
<div class="section__contattaci--radio">
<input type="radio" class="form__input section__contattaci--radio-1" name="RitiroCorriere" id="RitiroSede" >
<label for="RitiroSede" class="form__label">Ritiro in sede</label>
</div>
</div>
<div class="form__group">
<input id="inserisci" class="btn section__contattaci--btn" type="submit" value="Invia →">
</div>
<input hidden id="subid" name="subid" value="<?php echo $_GET['SUBID'] ?>">
</form>
And the following script before close of the body tag:
<script>
$('#inserisci').click(function () {
var data;
data=$('#inviadati').serialize()
//data = new FormData();
$.ajax({
url: '../dist/inviaordine.php',
data: data,
type: 'POST',
success: function ( data ) {
window.location.replace("../dist/thankyou2.html");
}
});
//e.preventDefault();
});
</script>
PROBLEM:
In a domain and host it works perfectly.
In another one (different domain and host) if I fill the input and it's invalid (or correct) and I press enter, it submit the form and go to /dist/thankyou2.html.
I tested with same browser.
I can't find the solution to solve this problem, can you help me?
Thank you.

I forgot how to capture a post in php

I am trying to capture the $_POST from this form:
<?php
var_dump($_POST);
?>
<form class="form-horizontal" action="" method="post">
<div class="form-group">
<div class="col-sm-6">
<input type="text" class="form-control input-lg" id="inputEmail3" placeholder="Name*">
</div>
<div class="col-sm-6">
<input type="email" class="form-control input-lg" id="inputPassword3" placeholder="Email*">
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<input type="text" class="form-control input-lg" id="inputPassword3" placeholder="Position">
</div>
<div class="col-sm-6">
<input type="text" class="form-control input-lg" id="inputPassword3" placeholder="Company">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<textarea class="form-control input-lg" rows="3" cols="10" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-6 col-sm-6">
<input type="submit" class="btn btn-default btn-lg btn-custom-sbmit" value="Send" />
</div>
</div>
</form>
and some how I keep failing because when I enter the details, and hit submit I get and array back: array(0) { }.
This is probably one of the most basic things, yet I forgot ow to do it. Ideas?
You have no name attributes for your <input> form elements. As a result nothing is sent to the server. You can verify this by using Firebug or Chrome's developer tools to see no data is sent.
<input type="text" class="form-control input-lg" id="inputEmail3" placeholder="Name*">
should be
<input type="text" class="form-control input-lg" name="inputEmail3" placeholder="Name*">
etc.

HTML - form acting weird. Not redirecting to URL and using GET instead of POST

I'm a CS student and I have a DB project due in less than 24hrs! This is really annoying because I just need to forms to access my DB. Anyway, I have this form that works perfectly, while the second form does not work. Instead of posting and directing to the correct URL the second form re-loads the current page with the variables in the URL. Anybody have any ideas?
<form role="form" method="post" action="../controller/AddPerson.php">
<div class="box-body">
<div class="form-group">
<label for="newReservationFirstName"> First name</label>
<input type="text" class="form-control" name="newReservationFirstName" placeholder="Enter first name">
<label for="newReservationLastName"> Last name</label>
<input type="text" class="form-control" name="newReservationLastName" placeholder="Enter last name">
<label for="newReservationPhoneNumber"> Phone Number</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="newReservationPhoneNum" data-inputmask='"mask": "(999) 999-9999"' data-mask/>
</div><!-- /.input group -->
<label for="newReservationStreetAddress"> Street Address</label>
<input type="text" class="form-control" name="newReservationStreetAddress" placeholder="Enter street address">
<label for="newReservationCity"> City</label>
<input type="text" class="form-control" name="newReservationCity" placeholder="Enter city">
<label for="newReservationState"> State</label>
<select class="form-control" name="newReservationState">
<?php
$result = getTableOrderBy('States','stateName');
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row[stateAbbr].">".$row[stateName]."</option>";
} ?>
</select>
<label for="newReservationZip"> Zip Code</label>
<input type="text" class="form-control" name="newReservationZip" placeholder="Enter zipcode">
</div>
<button type="submit" class="btn btn-success btn-lg">Add New Customer</button>
</div>
</form>
This is the form that doesn't work correctly, both pages exist on the server:
<form role="form" method="post" action="../controller/AddEmployee.php">
<div class="box-body">
<div class="form-group">
<label for="newEmployeeFirstName"> First name</label>
<input type="text" class="form-control" name="newEmployeeFirstName" placeholder="Enter first name">
<label for="newEmployeeLastName"> Last name</label>
<input type="text" class="form-control" name="newEmployeeLastName" placeholder="Enter last name">
<label for="newEmployeePhoneNumber"> Phone Number</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="newEmployeePhoneNum" data-inputmask='"mask": "(999) 999-9999"' data-mask/>
</div><!-- /.input group -->
<label for="newEmployeeStreetAddress"> Street Address</label>
<input type="text" class="form-control" name="newEmployeeStreetAddress" placeholder="Enter street address">
<label for="newEmployeeCity"> City</label>
<input type="text" class="form-control" name="newEmployeeCity" placeholder="Enter city">
<label for="newEmployeeState"> State</label>
<select class="form-control" name="newEmployeeState">
<?php
$result = getTableOrderBy('States','stateName');
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row[stateAbbr].">".$row[stateName]."</option>";
} ?>
</select>
<label for="newEmployeeZip"> Zip Code</label>
<input type="text" class="form-control" name="newEmployeeZip" placeholder="Enter zipcode">
<p></p>
<p></p>
<label for="newEmployeeFirstName"> Account Username</label>
<input type="text" class="form-control" name="newEmployeeUsername" placeholder="Enter username">
<label for="newEmployeeLastName"> Account Password</label>
<input type="text" class="form-control" name="newEmployeePassword" placeholder="Enter password">
<label for="newEmployeePhoneNumber"> Social Security Number</label>
<input type="text" class="form-control" name="newEmployeeSocial" placeholder="Enter SSN">
<div class="form-group" name="newEmployeePrivileges">
<br>
Privileges :
<select name="newEmployeePrivileges">
<option value="admin">Admin</option>
<option value="admin">Non-Admin</option>
</select>
</div>
<button type="submit" class="btn btn-success btn-lg">Add New Employee</button>
</div>
</div>
</form>
----------------------------------EDIT ----------------------------------------------
I tried making a another really simple form on some extra space and it still didn't work. I have no idea what could be cause it to do this.
<form method="post" action="post" action="../controller/AddEmployee.php">
<button type="submit" class="btn btn-success btn-lg">Add New Employee</button>
</form>
It could be that the button tag you are using to submit the form is causing it behave strangely. Try swapping out the button tag for an input. So:
<form method="post" enctype="multipart/form-data" action="../controller/AddEmployee.php">
<input type="submit" class="btn btn-success btn-lg" name="submit" >Add New Employee</input>
</form>
Also, I noticed you've included two 'action' attributes in your example form :-)

Categories