表单插入数据库无法异步工作

时间:2019-05-22 06:52:23

标签: javascript php html sql ajax

我试图将文本区域的值异步插入到我的数据库中,但是它一直重定向到PHP处理页面,并在其中回显结果。 如何获取当前HTML页面上PHP脚本的结果?

JS:

$("#sub").click(function() {
$.post( $("#text").attr("action"), $("#text :input").serializeArray(), 
function(info) { $("#result").html(info);});
});

$("#text").submit( function(){
return false;
})

PHP:

$sql = "UPDATE text 
        SET text_content = ? WHERE (id = 40) AND (number = $Number);";
            $stmt = mysqli_stmt_init($connection);
            if (!mysqli_stmt_prepare($stmt, $sql))
            {
                header("Location: ../create_text.php?error&prepare1111");
                exit();
            }
            else
            { 
                $stmt->bind_param("s", $content);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_close($stmt);
                $connection->close();
                echo "successfully saved";
            }

HTML:

<form type="text" method="post" onSubmit="return validateText(); toHTML();" action="processing.php" id="text">
            <textarea name="content" rows="45" id="auto-expand" class="text-box" type="text"><?php echo stripslashes($content) ?></textarea>
            <input type="hidden" name="id" id="id" value="<?php echo $id ?>">
            <input type="hidden" name="number" id="number" value="<?php echo $number ?>">
            <input type="hidden" name="updated" value="<?php echo $updated ?>">
            <button type="submit" id="sub" name="submit">Save</button>
            <button type="button" onClick="validateText(); toHTML();">Check</button>
            <span id="result"></span>
        </form>

任何帮助都如此好! :)

1 个答案:

答案 0 :(得分:0)

尝试preventDefault:

$("#text").submit( function(e){
    e.preventDefault();
})