I have this view
<div class="w3-row w3-padding-64">
<div class="w3-twothird w3-container">
<form id='create_delivery' method="post" class="form-horizontal"
enctype="multipart/form-data">
from <input type="text" name="first_address"><br>
To <input type="text" name="second_address" ><br>
<label>weight</label>
<input type="number" name="weight" ><br>
<label>price</label>
<input type="number" name="price" ><br>
<label>description</label><br>
<textarea name="description"></textarea>
<br>
<input type="submit" value="Submit">
</form>
</div>
</div>
and I sent a post request by ajax to the controller method
$(document).ready(function(){
$("#create_delivery").submit(function(evt){
var postData = $(this).serialize();
$.ajax({
url: baseURL + "deliverys/create_delivery",
type:'post',
data:postData,
dataType:'json',
success:function(data){
alert('delivery created');
}
});
evt.preventDefault();
});
});
this is the controller
public function create_delivery(){
if(isset($_POST)):
$first_address= $this->input->post('first_address');
$second_address= $this->input->post('second_address');
$description= $this->input->post('description');
$Weight= $this->input->post('Weight');
$price= $this->input->post('price');
$data = array(
"first_address"=>$first_address,
"second_address"=>$second_address,
"description"=>$description,
"Weight"=>$Weight,
"price"=>$price
);
$this->deliverys_model->create_delivery($data);
endif;
}
the problem that all the values are coming null,
it works when I send the post request without ajax
but with ajax the values are NULL
Error Number: 1048
INSERT INTO deliverys (first_address, second_address, description, ready_to_buy, Weight, price) VALUES (NULL, NULL, NULL, NULL, NULL, NULL)
I think you not getting form data with var postData = $(this).serialize().
Try like this $("#create_delivery").serialize()
If there is no <input type="file"> in form don't use enctype="multipart/form-data"
$(document).ready(function(){
$("#create_delivery").submit(function(evt){
evt.preventDefault();
$.ajax({
url: baseURL + "deliverys/create_delivery",
type:'POST',
data:$("#create_delivery").serialize(),
dataType:'json',
success:function(data){
alert('delivery created');
}
});
evt.preventDefault();
});
});
Try these
$first_address = $_POST['first_address'];
$second_address = $_POST['second_address'];
$description = $_POST['description'];
$Weight = $_POST['weight'];
$price = $_POST['price'];
Instead of
$first_address= $this->input->post('first_address');
$second_address= $this->input->post('second_address');
$description= $this->input->post('description');
$Weight= $this->input->post('Weight');
$price= $this->input->post('price');
I didn't know the context of $this->input in your code, maybe you could show more of your code, but above code should be work.
you need change 'type' to 'method'
$.ajax({
url: baseURL + "Trips/create_trip",
method:'POST',
data:postData,
dataType:'json',
success:function(data){
alert('Trip created');
}
});
.serialize(); is used to create get method string ... if you want to post data use ajax change your js with
$("#create_delivery").submit(function(evt){
evt.preventDefault();
var first_address = $( "input[name='first_address']" ).val();
var second_address = $( "input[name='second_address']" ).val();
var weight = $( "input[name='weight']" ).val();
var price = $( "input[name='price']" ).val();
var description = $( "input[name='description']" ).val();
$.ajax({
url: baseURL + "deliverys/create_delivery",
type:'post',
data : {first_address:first_address,second_address:second_address,weight:weight,price:price,description:description},
dataType:'json',
success:function(data){
alert('delivery created');
}
});
});
Related
view:
<script>
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
product_name = $("#product_name").val();
color = $("#colorWell").val();
$.ajax({
type:"POST",
data:{"product_name":product_name, "color":color},
url:"<?php echo base_url(); ?>admin/products",
success:function(data){
alert(data);
}
});
});
});
</script>
<input type="text" class="form-control" id="product_name" name="product_name">
<input type="color" id="colorWell" name="color">
<input type="file" id="product_image" name="product_image[]" multiple>
<input type="submit" class="btn btn-primary" id="submit" name="submit">
controller:
public function products()
{
$product_name = $this->input->post('product_name');
$color = $this->input->post('color');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['product_image']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['product_image']['name']= $files['product_image']['name'][$i];
$_FILES['product_image']['type']= $files['product_image']['type'][$i];
$_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
$_FILES['product_image']['error']= $files['product_image']['error'][$i];
$_FILES['product_image']['size']= $files['product_image']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
$data = array(
'product_name' => $product_name,
'color' => $color,
'product_image' => implode(",",$dataInfo['product_image']),
);
$result_set = $this->db->insert('add_product',$data);
if($sql == true)
{
echo 'New Product Added';
}
else
{
echo 'Unable to Proceed!';
}
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = ''.base_url().'resource/product/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
In this code I am trying to insert and wants to move an image into a folder. But Now, problem is that when I click on submit button it throw an error as show in below:
Message: Undefined index: product_image
and query looks:
INSERT INTO `product` (`product_name`, `color`, `product_image`) VALUES ('men hoodies','#004080', NULL)
I don't know where am I doing wrong. So, How can I solve this issue? Please help me.
Thank You
Send all formdata with file in your ajax.
HTML code like this...
<form method="POST" id="YourFormID" enctype="multipart/form-data">
<input type="text" class="form-control" id="product_name" name="product_name">
<input type="color" id="colorWell" name="color">
<input type="file" id="product_image" name="product_image[]" multiple>
<input type="submit" class="btn btn-primary" id="submit" name="submit">
</form>
Ajax code here....
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
var formData = new FormData($('form#YourFormID')[0]);
$.ajax({
type:"POST",
data:formData,
url:"<?php echo base_url(); ?>admin/products",
success:function(data){
alert(data);
}
});
});
});
</script>
You are not send file in your ajax request. therefore not found index product_image
You didn't submit the files data.
Use formData to upload files data, and append any other input you like to add to formData :
<script>
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
product_name = $("#product_name").val();
color = $("#colorWell").val();
var formData = new FormData();
$.each($("#product_image"), function (i, obj) {
$.each(obj.files, function (j, file) {
formData.append('product_image[' + i + ']', file);
});
});
formData.append('product_name', product_name);
formData.append('color', color);
$.ajax({
type:"POST",
data:formData,
processData: false,
contentType: false,
url:"<?php echo base_url(); ?>admin/products",
success:function(data){
alert(data);
}
});
});
});
</script>
use array_column like below to add get all product_image values
implode(",",array_column($dataInfo, 'product_image'))
How can I send input values through AJAX on button click? My code is below. Thanks in advance.
while
{
<form class="commentform">
<input type="hidden" class="proid" name="proid" value="<?=$rr['id']?>">
<input type="text" class="form-control" name="comval" placeholder="Write a comment.." autocomplete="off">
<button class="btn btn-post" type="button">Post</button>
</div>
</form>
}
$(document).ready(function() {
$(document).on('click', '.btn-post', function(){
var thePostID = $(this).val;
$.ajax({
url: 'fetch_comments.php',
data: { postID: thePostID },
type: 'POST',
success: function() {
alert(data);
}
});
Firstly, the correct method is $(this).val(), not just $(this).val.
Secondly, you can simplify your code by getting the data from the closest form element using serialize(). Try this:
$(document).on('click', '.btn-post', function() {
var $form = $(this).closest('form');
$.ajax({
url: 'fetch_comments.php',
data: $form.serialize(),
type: 'POST',
success: function() {
alert(data);
}
});
});
$("form").serialize();
Serialize a form to a query string, that could be sent to a server in an Ajax request.
What I have here is ajax that's post information into textbox from database. This ajax work's in input field, but when I tried to use select box it doesn't working. Why is that?
not working
<select id="tag"><option value="">none</option><option value="crs">crs</option></select>
working
<input name="tag" id="tag" type="text" value="" />
Index.php
<select id="tag"><option value="">none</option><option value="crs">crs</option></select>
<input name="output" id="output" type="text" value="" />
<script type="text/javascript">
$(document).ready(function()
{
$('input[id="tag"]').change(function()
{
var prjt_code = $("#tag").val();
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"prjt_code="+prjt_code,
dataType:'html',
type:'POST',
success:function(data){
//alert(data);
$('#output').val(data);
}
});
return false;
});
});
</script>
autocomplete-ajax.php
<?php
if(isset($_POST['prjt_code'])) {
$prjt_code = $_POST['prjt_code'];
$sql = $mysqli1->query("SELECT * FROM project WHERE project='$prjt_code'");
while($row1 = $sql->fetch_assoc())
{
$code = $row1['project_code'];
}
echo $code;
}
?>
You are targeting input[id="tag"] when you want select[id="tag"]
http://jsfiddle.net/releaf/U28jb/
$(document).ready(function() {
$('select[id="tag"]').on('change', function() {
var prjt_code = $("#tag").val();
alert(prjt_code);
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"prjt_code="+prjt_code,
dataType:'html',
type:'POST',
success:function(data){
//alert(data);
$('#output').val(data);
}
});
return false;
});
});
i've this two fields:
<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">
And I need to send to a php page with an ajax call.
I've this function that i use in many scripts
$("#sendSms").click(function(){
var text = $("input[name=smsText]").val();
var recipients = $("input[name=recipients]").val();
var datastr ='text=' + text +'&recipients=' + recipients;
$(over).appendTo('#box');
$.ajax({
type: "POST",
url: "send-result.php",
data: datastr,
cache: false,
success: function(data){
$('#box').html(data);
}
});
return false;
});
Please, i need help to modify my function to send both "smsText" and array recipients[] to other php page via Ajax...
Thank you very much!
Replace your following code:
var recipients = $("input[name=recipients]").val();
var datastr ='text=' + text +'&recipients=' + recipients;
for this one:
var datastr = '';
$("input[name='recipients[]']").each(function() {
datastr += '&recipients[]=' + $(this).val();
});
datastr ='text=' + text + datastr;
that should do what you want and cause PHP to create the array variable $_POST['recipients'] with all your values in it.
Have a look at jQuerys functions .serializeArray() and .serialize()
Try:
var datastr = '&recipients[]=';
var arr = [];
$("input[name='recipients[]']").each(function() {
arr[] = $(this).val();
});
datastr ='text=' + text + datastr + arr;
If the fields are contained within a form, you can use jQuery's serialize() method to convert the fields into a string to send via Ajax
<form id="sms-form">
<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">
</form>
$("#sendSms").click(function(){
var datastr = $("form#sms-form").serialize();
$(over).appendTo('#box');
$.ajax({
type: "POST",
url: "send-result.php",
data: datastr,
cache: false,
success: function(data){
$('#box').html(data);
}
});
return false;
});
Try
html
<form name='ohForm' action="#">
<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">
// and others components
</form>
javascript
$("#sendSms").click(function(){
var form = $("form[name='ohForm']");
var datastr = form.serialize();
$(over).appendTo('#box');
$.ajax({
type: "POST",
url: "send-result.php",
data: datastr,
cache: false,
success: function(data){
$('#box').html(data);
}
});
return false;
});
php
print_r($_POST['data']);
On form submit i want to load a div with an updated list of a mysql table. I'am sending the form variables across to a php and posting them into a mysql table. the same page displays the full table data. I want to load the data into the same div tag as the form. So it appears that the information is being loaded above the form.
My Javascript
$("#formSubmit").submit(function(){
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
My form -
<div id="2">
<p>Add a Comment</p>
<form id="formSubmit" method="POST">
<div>
<input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="submit" id="comment" class="button" value="Submit" />
</form>
</div>
All it is doing is refreshing the page and not loading the information in to div 2 :S
thanks for your help
You need to prevent the form from redirecting the page using the preventDefault method on the event object:
$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
e.preventDefault(); // right here
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
});
add a return false to the end to stop the form from submitting. or if you want to be more elegant use the preventDefault(); method. personally for something as simple as this though i just stick with return false;
$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
return false;//right here
});