简体   繁体   English

觉得我的Ajax有问题吗?

[英]Think something's wrong with my ajax?

What I want to do is return the echo value of "addTemplate.php" when I send it a form value via a http POST request. 我想做的是当我通过http POST请求发送表单值时返回“ addTemplate.php”的回显值。 However, when I send the request, absolutely nothing happens. 但是,当我发送请求时,绝对没有任何反应。

What should I do? 我该怎么办?

Here's my JS 这是我的JS

$(document).ready(function() {
    $("#sendTemplate").on("submit", function(event) {
        var template = $("#template").val();
        event.preventDefault();
        $.ajax({method: "POST", url: "addTemplate.php", data: template, success: function(result) {
            alert(result);
        });
    });
});

Here's my html 这是我的HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Admin</title>
        <link type="text/css" rel="stylesheet" href="stylesheet.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="adminscript.js"></script>
    </head>
    <body>
        <h1>Admin/Developer page</h1>
        <form id="sendTemplate">
            <p>Enter a new template for the server: (Enter #word to enter a placeholder for words)</p><input type="text" name="template" id="template">
            <input type="submit" name="submit">
        </form>
        <div id='success'></div>
        <br><br><br><br>
        <form action="turing.html">
            <button type="submit">Home</button>
        </form>
    </body>
</html>

And here is my php 这是我的PHP

$template = $_POST["template"]; 
if(strlen($template) <= 100 && strpos($template, "#word")) {
    file_put_contents("templates.txt", "\r\n" . $template, FILE_APPEND);
    header("Location: http://community.dur.ac.uk/h.j.g.baum/admin.html");
    echo "True";
}
else {
    echo "False";
}

What's wrong with my code (most likely in the JavaScript) and how can I improve it? 我的代码出了什么问题(很可能是在JavaScript中),我该如何加以改进?

Thanks 谢谢

Replace ajax data: 替换ajax数据:

data: template,

With: 带有:

data: "template="+template,

Issue: 问题:

You are using $_POST['template'] for getting values and didn't define in ajax data. 您正在使用$ _POST ['template']获取值,但未在ajax数据中定义。

I think you're missing a closing curly on the Ajax success function 我认为您缺少Ajax成功功能的结束功能

$(document).ready(function() {
    $("#sendTemplate").on("submit", function(event) {
        var template = $("#template").val();
        event.preventDefault();
        $.ajax({method: "POST", url: "addTemplate.php", data: template, success: function(result) {
            alert(result);
        }});
    });
});

id do this: id这样做:

 $(document).ready(function () {
            $("#sendTemplate").on("submit", function () {
                $.ajax({
                    method: "POST",
                    url: "addTemplate.php",
                    data: $(this).serialize(),
                    success: function (result) {
                        alert(result);
                    });
                return false;
            });
        });

Just change your js to 只需将您的js更改为

$(document).ready(function() {
$("#sendTemplate").on("submit", function(event) {
    var template = $("#sendTemplate").serialize();

    $.ajax({method: "POST", url: "test.php", data: template, success: function(result) {
        alert(result);
    }});
    event.preventDefault();
});

And

<form id="sendTemplate">

to

<form id="sendTemplate" method="POST">

This is the right way to get the form data and pass it to your php script 这是获取表单数据并将其传递给您的php脚本的正确方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM