weixin_33670713 2015-06-11 17:05 采纳率: 0%
浏览 8

使用AJAX将数据发布到PHP

I am trying to test PHP post data using AJAX. From page test I want to post on to the same page and check if PHP receives post data, if data is posted just to see if redirection is successful, so that I can write authentication code and assign session before page redirects.

When I click on the login button, I just checking isset post data and if data exists then redirect. I am not sure why this is not working. Any help is appreciated.

<script>
    $(document).ready(function(){
        $('#login').click(function(){
            $.ajax({
                type: 'POST',
                url: 'http://domain.com/backend/test',
                data: { username: "John", password: "Boston" }
            });
            return false;
        });
    });
</script>

<?php
if(isset($_POST['username']) && isset($_POST['password'])) {
    redirectto("http://domain.com/backend/test2");
    // redirecto is a function equivalent to header location
}
?>

<form autocomplete="off" class="ui fluid form segment" method="post">
    <div class="ui fluid form segment">
        <div class="two fields">
            <div class="field">
                <label>Email/Username</label>
                <input placeholder="Email/Username" name="username" id="username" type="text">
            </div>
            <div class="field">
                <label>Password</label>
                <input placeholder="Password" name="password" id="password" type="password">
            </div>
        </div>
        <input type="submit" class="ui fluid submit button" name="dosubmit" value="Submit" id="login" />
    </div>
</form>
  • 写回答

2条回答 默认 最新

  • weixin_33711647 2015-06-11 17:14
    关注

    Change

    <input type="submit" class="ui fluid submit button" name="dosubmit" value="Submit" id="login" />
    

    to

    <input type="button" class="ui fluid submit button" name="dosubmit" value="Submit" id="login" />
    

    Even if you triggered the click event of #login button, its a submit type and it will trigger the submit event first. Changing the button type should help.

    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行