Saving to database via Ajax - javascript

I have a situation,I need to add lot of text to server (via ajax & php).Each is happeening by clicking on a add button.In order to reduce round trip.I am planning to give a save all button so once I store everything in client side and I can save all together to database via ajax so only one round trip.
I have 6 input fields and needs to save this info everytime
My planning
Store everything in a JavaScript hidden variable and itreate this in php side and save it.
I will have to store lot of text in hiden field.Is my approach correct ? any better way ?

You dont need to store it on hidden fields, just make a JSON object containing the data you want and then send it to the server throught ajax.
You can create the JSON object this way:
var jsonObject = {'name': $('#name').val(), 'city': $('#city').val()};
And then you send it to PHP throught AJAX:
$.ajax({
type: 'POST',
url: 'some.php',
data: jsonObject,
dataType: 'json'
}).done(function() {
alert('success');
}).fail(function() {
alert('error');
}).always(function() {
alert('complete');
});

Related

Laravel not getting all request data

I want to pass select2 multiple select values to database using ajax in my laravel project. Before using ajax, I passed the values using get requests. All values are shows as url parameters, but when it checks with a request -> all() it gets the token value and the last value only, other values are not shown. Why are some other values missing? How can I get all values? I also want to store them in a database. Additionally how can I send those values using ajax.
full URL example:
http://127.0.0.1:8000/send_services?_token=oo6hLRavd6mPczbsgjwQ8RXSNUohhktO5NyNGxCN&services_select=6&services_select=8&services_select=7
Here is my ajax request(send data using ajax instead of normal form submission):
$('#submit-service').click( function() {
var terms = $("#service-select").val();
$.ajax({
type: "GET",
url: '/send_services',
data: terms
})
.done(function(result) {
console.log(result);
})
.fail(function(error) {
console.log(error);
});
});
I want to send this data mainly using ajax.
you just need to add array in name attribute of select tag
for e.g
<select multiple="" name="multiple_values[]" id="service-select">
Sending values using ajax and GET is the same, you simply use the URL:
$.ajax({
type: "GET",
url: '/send_services?data1=value1&data2=value2&data3=value3'
});

How to make a request using jQuery to a django server to refresh infos on page

I made a website in python using Django. My site allows you to control lights while indicating if the light is on or not.
I'm looking for a solution that could make a simple request with data to the server and send data back to the client without updating the entire page but only a part of it.
My ideal would be for the client to make a request to the server with identification data. Then, the server returns the updated data that the user is allowed to have.
Is that possible to make a JavaScript to do that ? And the server, how it can return data to the client ?
You can Use jquery AJAX for send request and get a response and Update an element or part of the page you can read more about it in :
W3schools
or :
jquery.com
AJAX function I use for PHP project:(Just for example)
$.ajax({
type: "POST",
url: Url,
data: InfoData, // data if you want to send for example InfoData={dataName: variable} or you can set this = '' if you don't want to send data
datatype: "html",
success: function(result) {
console.log(result);
}
});

How do I pass form data to PHP and return a json string to Javascript?

Does anyone know how pass form values into PHP and still return the data to JavaScript? (For use in Google Charts if anyone is wondering.)
I have an HTML form with 4 radio boxes. I'd like to pass the value of the form so that my PHP request will be modified based on the user's selection.
The results from the PHP request need to be passed to JavaScript for processing.
In the radio button on click event place a javascript function that will perform an XMLHttpRequest to the php page and have the PHP page echo some JSON content that can be decoded in the return of the XHR in Javascript.
Example of such
May this can help you.
I think that you must do an Ajax request
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( data) {
alert( "Data Saved: " + data);
});
You send data to server form your form (here sent are name and location). You read this data with javascript with OnSelect and transfert it to your PHP code and,
You get back data from server into your JavaScript code and you can do what you want with it.
Sorry if my solution use JQuery, it is better for cross-browser with less code writing !
You could use this line to parse an array into json and use it later in javascript:
json_encode($rows); //format array named $rows into json data
More info: http://php.net/manual/en/function.json-encode.php

How to send 3mb data URL with ajax post?

I need to pass a huge set of data with jquery ajax - image data. In which I can't use the usual way of uploading. Because the image data is from a canvas - literalycanvas. I've tried sent it but it's not get through. The data sample as follow: (pink is image data which is a lot longer than this and yellow is a truncate warning)
Well, I need to pass this data along the jquery ajax. So
Is there any possibility to do?
Can I write it to a cookie instead?
Can I pass it through the session?
Here's my code :
$.ajax({
url: 'print.php',
type: 'POST',
data: {
// convert the image data to base64
filename:'<?=$_GET[file]?>',//photo/2015-03-09/img_00215.jpg
image: lc.canvasForExport().toDataURL().split(',')[1],
type: 'base64'
},
success: function(data) {
$('.export_btn').html("<div class=\"alert alert-success\"><i class=\"fa fa-check\"></i> Printed</div>");
},
error: function() {
$('.export_btn').html("<div class=\"alert alert-danger\"><i class=\"fa fa-warning\"></i> Print error!</div>");
}
});
return false;
});
As you can see, the image: is sending a long line of data. Which is needs to send along the data. Any idea I can send this?

Can’t POST huge json data to PHP server with Jquery ajax function

I am trying to post some variables to PHP server via json. One of this variable contains huge string. When I post data all other variable get posted successfully but one that contains huge string remains undefined.
If I make content of that variable sort, then its running fine.
$.ajax({
type: 'POST',
url: url,
data:{name:”aaa”,age:”aaaaa”,detail:”huge string”},
async:false
}).done(function( data ) {
alert(data);
}).fail(function() {
$.notify("Cannot connect to server", "error");
});
please check your php.ini setting for directive
post_max_size
and try to increase like as below
ini_set('post_max_size' '20m')

Categories