0

I'm trying to pass form values to PHP via $.ajax method. I have following form in html:

<form>
<p>
   <label for="login">User ID:</label>
   <input type="text" name="login" id="login" value="user name">
</p>

<p>
   <label for="password">Password:</label>
   <input type="password" name="password" id="password" value="password">
</p>
</form>

and ajax:

$("form").submit(function(event) {
 var formInput = $('form').serialize();// tried with serializeArray() also

 var request = $.ajax({
    type: "POST",
    url:"../some.php", 
    data: {formInput:formInput},
    statusCode: {
                404: function() { alert("file not found");}}

 });

php:

<?
$data = $_POST['formInput'];
$data = array();
parse_str($_POST, $data);
print_r($data);
?>

On console it prints:

(
     [Array] =>
)

What I wanted was for $data to be an associative array as :

$data = array(
        'login' => 'foo'
        'password' => 'bar'
        );

but it seems like $data is being handled as a string... Thank you in advance!

5 Answers 5

2

Use data: $('form').serializeArray() instead of data: {formInput:formInput},

And then in the php side, $_POST is just the data you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response! This works. I guess data: {formInput: formInput}, puts already serialized array into an array again?! Thanks!
2

you need to specify you want an associative array instead of an object from json_decode:

json_decode($data, true);

You might also need to do this -

$('form').serializeArray();

1 Comment

.serializeArray() did it for me. Thanks!
1

instead of this :

$("form").submit(function(event) {
 var formInput = $('form').serialize();// tried with serializeArray() also

 var request = $.ajax({
    type: "POST",
    url:"../some.php", 
    data: {formInput:formInput},
    statusCode: {
                404: function() { alert("file not found");}}
 });

use this :

$("form").submit(function(event) {
 var formInput = $('form').serialize();// tried with serializeArray() also

 var request = $.ajax({
    type: "POST",
    url:"../some.php", 
    data: formInput,
    statusCode: {
                404: function() { alert("file not found");}}
 });

3 Comments

i'm sorry.. but i don't see the difference in the code... am i overlooking something??
I changed this line : data: {formInput:formInput}, to this : data: formInput,
works flawlessly with Adil's and Fresh's recommandation of using .serializeArray(). thanks
1

What we want is to serialize to a json object that can then be decoded. Use THIS pluggin to get access to .serializeObject.

$("form").submit(function(event) {
 var formInput = $('form').serializeObject();
 formInput = JSON.stringify(formInput);

 var request = $.ajax({
    type: "POST",
    url:"../some.php", 
    data: {formInput:formInput},
    statusCode: {
                404: function() { alert("file not found");}}

 });

Then on the php side you can decode the JSON easily

$data = json_decode($_POST['formInput'], true);

3 Comments

Thank you for quick response. But, now it prints " </br> " only. It shows up as null.
Thank you again for quick response! This was more than what I was looking for. Really apprciated!!
@dts316 cool! if this worked for you, please don't forget to hit the checkmark next to my answer so I can get credit.
0

From great responses I've figured out what my issue was. When I was submitting POST i was putting array into an array.

the following ajax code:

$("form").submit(function(event) {
var formInput = $('form').serialize();// tried with serializeArray() also

var request = $.ajax({
type: "POST",
url:"../some.php", 
data: {formInput:formInput},
statusCode: {
            404: function() { alert("file not found");}}
});

was changed to:

$("form").submit(function(event) {
var formInput = $('form').serializeArray();

var request = $.ajax({
type: "POST",
url:"../some.php", 
data: formInput,
statusCode: {
            404: function() { alert("file not found");}}
});

And PHP was changed to:

<? $data = $_POST;
    print_r($data); ?>

on cosole it printed exactly what I wanted:

Array
(
[login] => admin
[password] => qwerty123!
)

Thank you everyone for quick responses

Comments

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.