2

我想将表单数据从 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</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>

Ajax 语法

$('#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 = {};
       that.find('[name]').each(function(index, element) {
        var that = $(this),
        name = that.attr('name'),
        element = that.val();
        alert(name+'='+element+' '+methods);
        data[name] = element;
    });

       $.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;
       });

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');
 }
4

3 回答 3

1

尝试这个,

$('#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;
   });
于 2013-09-18T11:48:29.137 回答
1

我解决了它在 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;
       });

});

于 2013-09-18T13:41:55.127 回答
1

您可以在表单上使用序列化方法,它会收集正确的所有内容。

$('#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;
   });
于 2013-09-18T10:40:41.037 回答