0

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 ?

4
  • 3
    This approach sounds fine. Without further details thats all i can say Commented Nov 6, 2014 at 12:33
  • You can use the local Storage Object of Javascript to store the data e.g. as a json-string and send this json to your server Commented Nov 6, 2014 at 12:35
  • hidden fields or javascript arrays ? which is good and easy and secure. Commented Nov 6, 2014 at 12:44
  • Have in mind that hidden-fields in HTML can be seen and (in the worst case) manipulated by the user Commented Nov 6, 2014 at 13:03

1 Answer 1

1

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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommed to set the dataType option of ajax to json in this case
That depends on how the Server will return the data. But i agree with you, better to work with json-json format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.