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.

    评论

报告相同问题?

悬赏问题

  • ¥60 如何批量获取json的url
  • ¥15 对法兰连接元件所承受的表面载荷等效转化为法兰开孔接触面上的等效表面载荷?
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼