使用 $.post 将序列化数据从 jquery 移动设备传递到 PHP
Passing serialized data from jquery mobile to PHP using $.post
我正在使用 jQuery 移动设备将表单数据传递给 PHP 脚本,但我无法访问 PHP 中的数据。我试过这个:
$.post('http://127.0.0.1/tum_old/testi.php', $('form#login_form').serialize(), function(data) {
console.log(data);
});
检查
通过 $('form#login_form').serialize()
传递的数据后
var param = $('form#login_form').serialize();
console.log(param);
我得到:
username=ihfufh&passwordinput=dfygfyf
PHP脚本:
<?php
$username = $_POST['username'];
echo "$username";
?>
给我这个错误:
Undefined index: username
像这样序列化并发送您的数据:
jQuery / AJAX
$('#form').on('submit', function(e){
e.preventDefault();
$.ajax({
// give your form the method POST
type: $(this).attr('method'),
// give your action attribute the value yourphpfile.php
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
cache: false,
})
})
然后在 PHP 中像这样接收它:
<?php
// assign your post value
$inputvalues = $_POST;
$username = $inputvalues['username'];
?>
我正在使用 jQuery 移动设备将表单数据传递给 PHP 脚本,但我无法访问 PHP 中的数据。我试过这个:
$.post('http://127.0.0.1/tum_old/testi.php', $('form#login_form').serialize(), function(data) {
console.log(data);
});
检查
通过$('form#login_form').serialize()
传递的数据后
var param = $('form#login_form').serialize();
console.log(param);
我得到:
username=ihfufh&passwordinput=dfygfyf
PHP脚本:
<?php
$username = $_POST['username'];
echo "$username";
?>
给我这个错误:
Undefined index: username
像这样序列化并发送您的数据:
jQuery / AJAX
$('#form').on('submit', function(e){
e.preventDefault();
$.ajax({
// give your form the method POST
type: $(this).attr('method'),
// give your action attribute the value yourphpfile.php
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
cache: false,
})
})
然后在 PHP 中像这样接收它:
<?php
// assign your post value
$inputvalues = $_POST;
$username = $inputvalues['username'];
?>