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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测