duanniubeng2265 2014-12-03 18:39
浏览 40
已采纳

jQuery / Ajax - 返回false和preventDefault,产生不同的结果但不起作用

*edited for words and added all relevant php

I am trying to send post data to a PHP script, which then populates a form and loads it into the current page.

When I use this script, the action from the form still fires and I am redirected to the new page, but I get this information returned -

string(99) "DATE=2014-12- 03+09%3A08%3A54&F_NAME=Johnny&M_NAME=Darby&L_NAME=Cache&dailyClient=Johnny+Darby+Cache"

$(document).ready(function(){
    $(".clientSubmit").submit(function(event){
        event.preventDefault();
        var clientInformation = $(this).serialize();
        console.log(clientInformation);
    $.ajax({
        type: 'POST',
        url: 'IRCpopulatecheckin.php';
        data: clientInformation,
        cache: false,
        success: function(result){
            $('#clientform').load("IRCpopulatecheckin.php");
            alert(clientInformation);
            } // end result
        }); // end .ajax
    }); // end .submit
}); // end ready

On the other hand, this script loads the form into the current page, but the php returns -

string(0) "" NULL

Notice: Undefined index: DATE in /Library/WebServer/Documents/IRC/IRCpopulatecheckin.php on line 353

Notice: Undefined index: F_NAME in /Library/WebServer/Documents/IRC/IRCpopulatecheckin.php on line 354

Notice: Undefined index: M_NAME in /Library/WebServer/Documents/IRC/IRCpopulatecheckin.php on line 355

Notice: Undefined index: L_NAME in /Library/WebServer/Documents/IRC/IRCpopulatecheckin.php on line 356

$(document).ready(function(){
    $(".clientSubmit").submit(function() {
        var clientInformation = $(this).serialize();
        console.log(clientInformation);
    $.ajax({
        type: 'POST',
        url: 'IRCpopulatecheckin.php',
        data: clientInformation,
        cache: false,
        success: function(result){
            $('#clientform').load("IRCpopulatecheckin.php");
            alert(clientInformation);
            } // end result
        }); // end .ajax
    return false;
    }); // end .submit
}); // end ready

As far as I can tell the only difference between the two pieces of code is whether I use return false or preventDefault, but neither works. I have no idea where to go from here.

*php

$result = file_get_contents('php://input');
var_dump($result);

$DATE   = isset($_GET['DATE'])   ? $_GET['DATE']   : $_POST['DATE'];
$F_NAME = isset($_GET['F_NAME']) ? $_GET['F_NAME'] : $_POST['F_NAME'];
$M_NAME = isset($_GET['M_NAME']) ? $_GET['M_NAME'] : $_POST['M_NAME'];
$L_NAME = isset($_GET['L_NAME']) ? $_GET['L_NAME'] : $_POST['L_NAME'];
  • 写回答

1条回答 默认 最新

  • dougao2830 2014-12-03 19:09
    关注

    If you want the returned HTML from the IRCpopulatecheckin.php after you made a POST request to it, you should change your script to replace the $('#clientform') content with the returned HTML.

    $(document).ready(function(){ $(".clientSubmit").submit(function(event) { event.preventDefault(); var clientInformation = $(this).serialize(); console.log(clientInformation); $.ajax({ type: 'POST', url: 'IRCpopulatecheckin.php', data: clientInformation, cache: false, dataType: "html", success: function(result){ $('#clientform').html(result); // Replaces the clientform with the HTML return by the php script } // end result }); // end .ajax }); // end .submit }); // end ready

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看