如何停止jQuery发布页面刷新 - php

我正在通过PHP从MySQL数据库获取数据。我正在使用jQuery从发送并从PHP获取数据。这是代码。

$.POST("SubmitCode.php", $("#questionCodeForm").serialize(),'json').done(function(data) {});

现在的问题是,一旦我发送了该数据,页面就会刷新。如何停止页面刷新。如果我删除“ json”,则页面将停止刷新,但问题是我想在不刷新页面的情况下获取json数据。我怎样才能做到这一点?

-------------------------------------------编辑后---- -------------------------------------------------- ----------------------
这是更新的代码

$(document).ready(function() {
    initialization();

    codeSubmission();
});

function initialization() {
    $("#answerForm").hide();
}

function codeSubmission() {
  $("#submitButton").click(function(e) {
    e.preventDefault();
    $.post("SubmitCode.php", $("#questionCodeForm").serialize()).done(function(data) {
        var questionName = data.questionName,
            options = data.options,
            pollingStatus = data.pollingStatus,
            codeExist = data.codeExist;

        alert(data);
        alert(data[1]);
        alert(questionName);
        alert(options);

        if(codeExist == true) {
            $("#questionTitle").text("questionName");

            for(rowNum=1;rowNum<=5;rowNum++) {
                $("#checkbox-"+rowNum).val("Answer1");
                $("#checkbox"+rowNum+"label").text("Answer"+rowNum);
            } 

            $("#answerForm").slideDown(500);

        } else if(codeExist == false) {
            alert("This quiz code is invalid");
        }   
    },'json');  
    //return false;
  });
    //return false;
}

现在的问题是alert(questionName)的输出未定义。数据作为字符串传递。如何在正确的变量中获取正确的信息?

参考方案

请尝试以下操作:(请注意回调函数的位置以及小写的.post方法)

$.post("SubmitCode.php", $("#questionCodeForm").serialize(),function(data) {
  //manipulate your data here

 },'json');

此外,请确保触发该帖子的所有内容都不是实际链接,如果是实际链接,则需要停止执行默认操作。例如:

<a href="submit.php">Click here to submit</a>

javascript:

$(a).click(function(e) { 
e.preventDefault();
$.post("SubmitCode.php", $("#questionCodeForm").serialize(),function(data) {
  //manipulate your data here

 },'json');
});

PHP Count数组元素 - php

嗨,有人可以解释为什么这会返回“数组由0个元素组成”。 :$arr = array(1,3,5); $count = count($arr); if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements.…

PHP:从函数返回值并直接回显它? - php

这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…

PHP:将数据从二维数组复制到一维数组的最快方法 - php

我有一个巨大的二维PHP数组,带有500万行。$t = [ [ "id" => 1, "name" => "foo" ], [ "id" => 2, "name" => "bar" ] ]; 现在,我必须将此数组的I…

php-printf和sprintf具有不同的输出 - php

我编写了以下微型php程序来测试printf和sprintf:<?php $str_1 = printf("%x%x%x", 65, 127, 245); $str_2 = sprintf("%x%x%x", 65, 127, 245); echo $str_1 . "\n"; echo $s…

PHP-解析当前URL - php

在以下两种情况下,我都需要解析当前网址:http://mydomain.com/abc/ http://www.mydomain.com/abc/ 我可以获得“ abc”的返回值(或该位置的任何文本)。我怎样才能做到这一点? 参考方案 您可以使用parse_url();$url = 'http://www.mydomain.com/abc/…