donglie7778 2013-12-11 00:07
浏览 16
已采纳

jQuery表单数据不发送到PHP页面

I'm very new to anything outside of PHP, so forgive me if this comes off as simple.

I'm trying to create a simple registration form, which then calls a PHP function, which inserts the data.

The problem is, the data doesn't seem to be going to the page.

My form:

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function () {
$('#register').on('submit', function (e) {
    $.ajax({
        type: 'POST',
        url: 'http://warofman.com/actions.php?type=register',
        data: $('#register').serialize(),
        success: function () {
            alert('Form was submitted.');
            console.log(e);
        }
        });
    e.preventDefault();
    });
});
</script>
</head>
<body>
    <form id="register">
        Username: <input type="text" name="login"><br>
        Email: <input type="text" name="email"><br>
        Password: <input type="password" name="password"><br>
        Confirm Password: <input type="password" name="confpass"><br>
        <input type="submit" name="submit" value="Register">
    </form>
</body>
</html>

And then, the PHP:

function register()
{
    $login = $_POST['login'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $confpass = $_POST['confpass'];

    do_reg($login, $email, $password, $confpass);
}

Where do_reg() calls the process of registering.

  • 写回答

2条回答 默认 最新

  • dongyishe6689 2013-12-11 00:25
    关注

    Make the following changes, (follow good practice)

    <form id="register" action="" method="post">
            <label for="login">Username:</label>
            <input id="login" type="text" name="login"><br>
    
            <label for="email">Email:</label>
            <input type="email" name="email" id="email" /><br>
    
            <label for="password">Password:</label> 
            <input type="password" name="password" id="password"><br>
    
            <label for="confpass">Confirm Password:</label>
            <input type="password" name="confpass" id="confpass"><br>
    
            <input type="submit" name="submit" value="Register">
        </form>
    

    On php side:

    You are writing everything inside a function(register) and not calling it, instead do something like this,

    if(isset($_POST['submit'])){
      $login = $_POST['login'];
      $email = $_POST['email'];
      $password = $_POST['password'];
      $confpass = $_POST['confpass'];
      do_reg($login, $email, $password, $confpass);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线