Javascript 使用ajax将表单数据发送到php
我想将表单数据从ajax发送到php。我的ajax检索表单数据,但它不发送表单数据。我看不出代码有任何问题。也许我需要更专业的帮助。提前谢谢 HTML5语法Javascript 使用ajax将表单数据发送到php,javascript,jquery,ajax,html,Javascript,Jquery,Ajax,Html,我想将表单数据从ajax发送到php。我的ajax检索表单数据,但它不发送表单数据。我看不出代码有任何问题。也许我需要更专业的帮助。提前谢谢 HTML5语法 <div align="center"><a id="hi">Header</a></div> <a id="signup" data-add-back-btn="true" style="float:right;" data-icon="arrow-r">Sign- In<
<div align="center"><a id="hi">Header</a></div>
<a id="signup" data-add-back-btn="true" style="float:right;" data-icon="arrow-r">Sign- In</a>
</div>
<form class="check-user" action="php/sup.php" method="POST">
<label>Username</label>
<input id="Susername" name="username" placeholder="username" type="text" >
</div>
<div align="center" data-role="fieldcontain" style="width:100%;overflow:hidden" data-position="static">
<label>Email</label>
<input id="Semail" name="email" placeholder="email" type="email" >
</div>
<div align="center" data-role="fieldcontain" style="width:100%;overflow:hidden" data-position="static">
<label>Password</label>
<input id="Spassword" name="password" placeholder="password" type="password" >
</div>
<!---input type="submit" style="visibility:hidden;" id="send"/-->
</form>
PHP语法
session_start();
$name = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
if(isset($name) && isset($email) && isset($password))
{
echo $name;
$_SESSION['username'] = $name;
}
else
{
die('data not set');
}
您可以在表单上使用serialize方法,它将正确地收集所有内容
$('#signup').live('click', function(){
var that = $('form.check-user'),
urls = that.attr('action'),
methods = that.attr('method'),
data = that.serialize();
$.ajax(
{
url: urls,
type: methods,
data : data,
beforeSend: function(response){alert('Sending');},
success: function(response){ alert('success');},
error: function(response){alert('failed');},
complete: function(response){alert('finished');},
}
);
return false;
});
试试这个
$('#signup').live('click', function(){
$.ajax({
url:’’,//url to submit
type: "post",
dataType : 'json',
data : {
'Susername' : $('#Susername').val(),
'Semail' : $('#Semail').val(),
'Spassword' : $('#Spassword').val()
},
success: function (data)
{
}
});
return false;
});
我解决了它在php端的工作方式,您可以这样做吗
$name = isset(json_decode($_POST['username']));//htmlentities($values[0]);
$email = isset(json_decode(($_POST['email'])));//htmlentities($values[1]);
$password = isset(json_decode($_POST['password']));//htmlentities($values[2]);
阿贾克斯方面
$(document).ready(function(e) {
$('#signup').live('click', function(){
//var name = document.getElementById('Susername').value;
//var email = document.getElementById('Semail').value;
//var pass = document.getElementById('Spassword').value;
var that = $('form.check-user'),
urls = that.attr('action'),
methods = that.attr('method'),
data = {};
data = that.serialize();
console.log(data);
$.ajax(
{
url: urls,
type: methods,
dataType:'json',
data : data,
beforeSend: function(response){$.mobile.showPageLoadingMsg(true);},
success: function(response){ $.mobile.showPageLoadingMsg(false);},
error: function(xhr, textStatus, errorThrown){alert(textStatus);},
complete: function(response){},
}
);
return false;
});
})) 注册元素在哪里?您对
.live()
的调用似乎与任何内容都不匹配,因此在.ajax()
调用中使用的值从未填充过,也从未调用过。(另外,你不应该使用.live()
,而应该使用.on()
)@David Hm,这是一件显而易见的事情。我相信“忘记添加要单击的内容”不是错误。不管怎样,请检查您的元素和选择器要单击的元素是我的原始语法,我只是取了这一小段以便您理解,但是#注册是有效的,我将尝试重新定义元素的原因:元素=that.val()代码>?这到底应该做什么?很抱歉,语法不起作用,它显示发送,然后什么也没有发生。您会得到哪个警报?您还尝试过var\u dump($\u POST)
为了确保您在PHP文件中接收到数据,它将显示在Chrome中的网络选项卡中。还要尝试使用console.log()在javascript中获取所有正确的值;我的值出现在chrome控制台中,但网络表示它在sup.php上的挂起状态似乎php没有接收到数据。是吗?我试过了,但我的php文件似乎没有接收到我的数据。因为在chrome控制台中,它显示挂起时间很长
$(document).ready(function(e) {
$('#signup').live('click', function(){
//var name = document.getElementById('Susername').value;
//var email = document.getElementById('Semail').value;
//var pass = document.getElementById('Spassword').value;
var that = $('form.check-user'),
urls = that.attr('action'),
methods = that.attr('method'),
data = {};
data = that.serialize();
console.log(data);
$.ajax(
{
url: urls,
type: methods,
dataType:'json',
data : data,
beforeSend: function(response){$.mobile.showPageLoadingMsg(true);},
success: function(response){ $.mobile.showPageLoadingMsg(false);},
error: function(xhr, textStatus, errorThrown){alert(textStatus);},
complete: function(response){},
}
);
return false;
});