duanguanya3052 2015-04-30 21:28
浏览 66
已采纳

使用Ajax调用php脚本

So I'm building a forgot password system, and I would like the Forgot Password div to execute the forgotpassword php script when it is clicked. Note: all files are in the same directory I so far have: In HTML

<div id="forgotpass" href="" onclick="fpass()">forgot your password?</div>

In my Javascript file:

function fpass() {
console.log("fpass function");
    $.ajax({
       type: "POST",
       url: 'forgotpass.php',
       success: function(data){
            //data returned from php
            //console.log(data);
       }
    });
}

Inside the php script I have a few echo statements so far to test if the script actually gets run.

However, when I click "forgot your password?" it runs fpass() and alerts "fpass function" so I know the function works. But I don't think its executing the php script. I'm new to this so can anyone provide me an explanation as to why the php script isn't getting executed. Do I need to put anything inside the php script? Thanks!

  • 写回答

1条回答 默认 最新

  • doujuanju3076 2015-04-30 21:41
    关注

    If you are sending any data add

    data    : form.serialize()
    

    In php you can catch your data's with

    $_POST['data_name']
    

    And this is a better convension for your code.

            $.ajax({
                type    : 'POST',
                url     : 'forgotpass.php',
                data    :  form.serialize()
            }).done(function(data){
                if(data['success']){
    
                }else{
    
                }
            });
            e.preventDefault();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效