Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHEN to use serialize vs just regular POST

Tags:

jquery

php

I recently asked a question that was partially answered but I was hoping someone could explain this a little clearer. Its regarding PHP and the serialize function.

So I have a form with 12 fields, all with names. Traditionally I would just use the POST method, send them to a PHP page and then supply them into a database.

Today I stumbled upon the serialize method but after using it, it seems like it doesnt do anything that a regular POST statement would do. For example, if I wanted to use serialize, I would do the following:

var formData = $('#contForm').serialize();
$.post('functs.php',formData,dispAdd);

Then to print a value I would use

echo $_POST['value_name']

What I see happening is that with or without the serialize, I get the same results. Am I missing something here?

like image 828
Robert Mailloux Avatar asked Mar 12 '13 05:03

Robert Mailloux


People also ask

When should serialization be used?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

Why should I use serialization?

Well, serialization allows us to convert the state of an object into a byte stream, which then can be saved into a file on the local disk or sent over the network to any other machine. And deserialization allows us to reverse the process, which means reconverting the serialized byte stream to an object again.

Why is serialization not good?

It is not secure Using XML serialization is inherently insecure. Your classes need to be public, and they need to have public properties or fields. In addition, XML serialization works by creating temporary files.

What is a serialVersionUID and why should I use it?

SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.


Video Answer


1 Answers

The .serialize() method creates a text string in standard URL-encoded notation. It operates on a jQuery object representing a set of form elements. The form elements can be of several types. Read more.

.serialize() is used when you are sending data through an AJAX request. It's the same as submitting a form using a submit button. Only difference it when you use AJAX it does not refresh the page. That's why you get the same results even though you submit the form using a submit button or .serialize() when using AJAX.

.serializeArray() would be an alternative to .serialize().

Remember if you don't use .serialize() you have to create your key-value pairs manually as @Akam mentioned in the comment above.

And finally

When to use serialize <-- When you use AJAX to post data

When regular POST <-- When you use submit button to post data

like image 178
Techie Avatar answered Oct 10 '22 08:10

Techie