javascript 使用ajax将表单数据发送到php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18869908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:29:18  来源:igfitidea点击:

Using ajax to send form data to php

javascriptjqueryajaxhtml

提问by

I want to send form data from ajax to php. My ajax retrieves the form data but it doesnt send it i dont see anything wrong with the code maybe i need a much more professional help. Thanks in advance

我想将表单数据从 ajax 发送到 php。我的 ajax 检索了表单数据,但它没有发送它我没有看到代码有任何问题,也许我需要更专业的帮助。提前致谢

HTML5 syntax

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 syntax

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 syntax

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

回答by Gena Moroz

You can use serialize method on form, it will gather everything correct.

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

$('#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;
   });

回答by Ronjon

Try this,

试试这个,

$('#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;
   });

回答by Ronjon

I solved it works this way on the php side you do this

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

The Ajax side

阿贾克斯方面

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

});

});