普通网友 2014-08-14 07:53
浏览 19

仅将AJAX复制到网址行

Im trying to make a login system. PHP cant be used to send data to the server since i need to run it in phone applications. It seems like all AJAX does is change the URL of my browser window when testing it, and nothing gets send to the server. Code below. The regformhash() only hashes the password. Nothing else. HTML:

<form action="#" id="registration_form">
        Username: <input type='text' name='username' id='username' /><br>
        Email: <input type="text" name="email" id="email" /><br>
        Password: <input type="password" name="password" id="password"/><br>
        Confirm password: <input type="password" name="confirmpwd" id="confirmpwd" /><br>
        <input type="button" value="Register" 
onclick="return regformhash(this.form,
                               this.form.username,
                               this.form.email,
                               this.form.password,
                               this.form.confirmpwd);" /> 
    </form>
    <p id="answer">Test</p>

Script:

$('#registration_form').submit(function( event ) {
        event.preventDefault();
        var formData = $("#registration_form").serializeArray();
        $.ajax({
            url : 'register.php',
            type : "POST",
            data : formData,
            success: function(data) {
                alert("Yes" +data);// alert the data from the server
            },
            error: function() {
                alert("No" + data);
            }
        });
    });

PHP:

$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['p'];
$skips = 5;

echo "succes";
  • 写回答

3条回答 默认 最新

  • weixin_33737774 2014-08-14 08:09
    关注

    Put your function call on your form submit handler...

    <form action="#" id="registration_form" onsubmit="return regformhash(...);">
        Username: <input type='text' name='username' id='username' /><br>
        Email: <input type="text" name="email" id="email" /><br>
        Password: <input type="password" name="password" id="password"/><br>
        Confirm password: <input type="password" name="confirmpwd" id="confirmpwd" /><br>
        <input type="submit" value="Register" /> 
    </form>
    <p id="answer">Test</p>
    

    That way, the form will abort submission when your function returns false.

    In addition, PHP is run on the server. Javascript is what carries out AJAX stuff.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥60 SOL语句中Where查询中的 from to 语句能不能从小到大换成从大到小(标签-SQL)
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))