我正在尝试通过 $.ajax 方法将表单值传递给 PHP。我在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>
和阿贾克斯:
$("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);
?>
在控制台上打印:
(
[Array] =>
)
我想要的是让 $data 成为一个关联数组:
$data = array(
'login' => 'foo'
'password' => 'bar'
);
但似乎 $data 正在作为字符串处理......提前谢谢你!