使用Ajax表单数据发送到PHP发送到、表单、数据、Ajax

2023-09-11 00:41:09 作者:衣不如新 | 人不如故

我想从阿贾克斯将表单数据发送到PHP。我的AJAX检索形式的数据,但它不发送我没有看到什么毛病code,也许我需要一个更专业的帮助。在此先感谢

HTML5语法

 <分区对齐=中心><一个ID =喜>标题< / A>< / DIV>
<一个ID =注册数据反加-BTN =真的风格=浮动:权利;数据图标=箭头-R>点登录与< / a取代;
< / DIV>

<窗​​体类=检查用户行动=PHP / sup.php的方法=POST>
        <标签>用户名和LT; /标签>
        <输入ID =SusernameNAME =用户名占位符=用户名类型=文本>
    < / DIV>
    <分区对齐=中心的数据角色=fieldcontain的风格=宽度:100%;溢出:隐藏数据位=静态>
        <标签>电子邮件和LT; /标签>
        <输入ID =SemailNAME =电子邮件占位符=电子邮件类型=电子邮件>
    < / DIV>
    <分区对齐=中心的数据角色=fieldcontain的风格=宽度:100%;溢出:隐藏数据位=静态>
        <标签>密码和LT; /标签>
        <输入ID =spassword开头NAME =密码占位符=密码类型=密码>
    < / DIV>
    !< ---输入类型=提交风格=能见度:隐藏; ID =发送/  - >
    < /形式GT;
 

阿贾克斯语法

  $('#注册)。住(点击,函数(){
       //变种名称=的document.getElementById('Susername')值;
       //变种的电子邮件=的document.getElementById('Semail')值;
       // VAR通=的document.getElementById('spassword开头')值;
       VAR的= $('form.check用户),
       网址= that.attr('行动),
       方法= that.attr(方法),
       数据= {};
       that.find([名]')。每个(函数(指数,元素){
        VAR的= $(本),
        NAME = that.attr(名称),
        元= that.val();
        警报(名称+=+元素+''+方法);
        数据[名] =元素;
    });

       $阿贾克斯(
    {
        网址:网址,
        类型:方法,
        数据:数据,
        beforeSend:函数(响应){警报('发送');},
        成功:函数(响应){警报(成功);},
        错误:函数(响应){警报(失败);},
        完成:函数(响应){警报('完成');},
    }
    );
       返回false;
       });
 

PHP语法

 在session_start();
$ NAME = $ _ POST ['用户名'];
$电子邮件= $ _ POST [电子邮件];
$密码= $ _ POST ['密码'];


如果(使用isset($名)及和放大器;使用isset($电子邮件)及和放大器;使用isset($密码))
 {

        回声$名称;
        $ _SESSION [用户名] = $名称;

  }
 其他
 {
死亡(数据没有设置');
 }
 
我用ajax向php页面发送数据,结果返回到html页面提交成功后页面刷新了

解决方案

试试这个,

  $('#注册)。住(点击,函数(){

$阿贾克斯({
            网址:'',// URL提交
            类型:后,
            数据类型:JSON,
            数据 : {
                Susername:$('#Susername)VAL()。
                Semail:$('#Semail)VAL()。
                spassword开头:$('#spassword开头)VAL()。
            },
            成功:函数(数据)
            {

            }
        });

返回false;
   });
 

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

HTML5 syntax

<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

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

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

解决方案

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