Use data: $('form').serializeArray()
instead of data: {formInput:formInput},
And then in the php side, $_POST
is just the data you want.
Вопрос
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!
Решение
Use data: $('form').serializeArray()
instead of data: {formInput:formInput},
And then in the php side, $_POST
is just the data you want.
Другие советы
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();
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");}}
});
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);
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