简体   繁体   English

我尝试了一些 ajax 请求,但它不起作用

[英]I've tried to some the ajax request but it won't work

I am trying to make a request to 'inserir.php' that's where I'm going to process the data from the ajax request and insert it to the database.我正在尝试向“inserir.php”发出请求,我将在此处处理来自 ajax 请求的数据并将其插入数据库。 But when I click the button nothing happens.. Not even an error.但是当我单击按钮时,什么也没有发生……甚至没有错误。 This is my form.. :这是我的表格..:

<div class="modal-body">
                    <!-- FORM-->
                    <form method="POST" id="formulariopost">
                        <div class="form-group">
                            <label for="exampleInputEmail1" style="font-family: Arial, Helvetica, sans-serif;">Email</label>
                            <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Email" style="font-family: Arial, Helvetica, sans-serif;" required>
                            <small id="emailHelp" class="form-text text-muted" style="font-family: Arial, Helvetica, sans-serif;">Nunca vamos partilhar o seu email com mais ninguem.</small>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputPassword1" style="font-family: Arial, Helvetica, sans-serif;">Nome da Equipa</label>
                            <input type="text" class="form-control" id="nome_equipa" placeholder="Nome da Equipa" style="font-family: Arial, Helvetica, sans-serif;" required>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputPassword1" style="font-family: Arial, Helvetica, sans-serif;">Nickname dos jogadores</label>
                            <input type="text" class="form-control" id="nickname" placeholder="(GAMETAG)" style="font-family: Arial, Helvetica, sans-serif;" required>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputPassword1" style="font-family: Arial, Helvetica, sans-serif;">Numero do whatsapp</label>
                            <input type="text" class="form-control" id="numero" placeholder="Numero do whatsapp" style="font-family: Arial, Helvetica, sans-serif;" maxlength="15" data-mask="(00) 00000-0000" required>
                        </div>
                        <button id="enviar" class="btn btn-secondary" >Close</button>
                        <!--<button type="submit" class="btn btn-primary" style="font-family: Arial, Helvetica, sans-serif;margin-bottom: 30px;float: right;">Submit</button>-->
                    </form>
                    <br>
                    <!--PAYPAL-->
                    <div id="paypal-button-container"></div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" id="saveg">Save changes</button>
                </div>
            </div>

This is my javascript:这是我的 javascript:

$(document).ready(function() {

        $('#saveg').click(function(e){

            e.preventDefault();

            var email = $("#email").val();
            var nome_equipa = $("#nome_equipa").val();
            var gametag = $("#nickname").val();
            var numerowhats = $("#numero").val();


            $.ajax({
                type: "POST",
                url: "inserir.php",
                dataType: "json",
                data: {email:email, nome_equipa:nome_equipa, nickname:nickname, numero:numero},
                success : function(data){
                    if (data.code == "200"){
                        alert("Success: " +data.msg);
                    } else {
                        console.log('mona');
                    }
                }
            });


        });
    });

And this is my php where I want to make the ajax request:这是我的 php ,我想在其中提出 ajax 请求:

    $servername = "localhost";
$username = "root";
$password = "root";
$dbname = "copacobre";


$email = $_POST["email"];
$nome_equipa = $_POST["nome_equipa"];
$gametag = $_POST["nickname"];
$numerowhats = $_POST["numero"];
$newURL="/copac/#";
echo $email;

$errorMSG = "error test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$sql = "INSERT INTO pagamentos (email, nome_equipa, gametag, numerowhats)
VALUES ('".$email."', '".$nome_equipa."', '".$gametag."', ".$numerowhats.")";



$conn->close();

echo json_encode(array('code' => 200));

Looks the event binding is wrong.看起来事件绑定是错误的。 You need to bind to the submit event.您需要绑定到提交事件。

Also in your HTML code, the submit button is for close, and a regular button is assigned to Save Changes, usually submit is to perform the action of submit the data and continue.同样在您的 HTML 代码中,提交按钮用于关闭,并为保存更改分配了一个常规按钮,通常提交是执行提交数据并继续的动作。

JQuery submit documentation JQuery 提交文档

if should be something like如果应该是这样的

$('#formulariopost').submit(function(e){
    ....
})

and remember at the end of the function, you should return true in case it was successful, or false to cancel the event.并记住在 function 的末尾,如果成功,则应返回 true,否则应返回 false 以取消事件。

There can be many reasons why event is not binded.事件未绑定的原因可能有很多。 You can try to debug it in few steps.您可以尝试通过几个步骤对其进行调试。

Clear cache清除缓存

Yes, browsers loves caching.是的,浏览器喜欢缓存。 Make sure you have actual version of JS file loaded in your page.确保您的页面中加载了实际版本的 JS 文件。

If you are using Chrome, CTRL+F5 will do the job.如果您使用的是 Chrome,CTRL+F5 将完成这项工作。

Check how far code goes检查代码走了多远

Add breakpoints in Developer Tools console or - easy way - some console.logs to few points in code - eg after document.ready, after button click, before ajax request and into ajax success.在开发人员工具控制台中添加断点或 - 简单的方法 - 一些 console.logs 到代码中的几个点 - 例如在 document.ready 之后,在按钮单击之后,在 ajax 请求之前并进入 ajax 成功。 Don't forgot AJAX can also return with error code, and the method 'success' can't handle error responses.别忘了 AJAX 也可以返回错误码,方法 'success' 不能处理错误响应。

Depending on how far code gets (eg using console.logs) you can determine where the problem is.根据代码到达的程度(例如使用console.logs),您可以确定问题出在哪里。 If you can't see log right behind document.ready, there is probably some problem with including file.如果您在 document.ready 后面看不到日志,则可能是包含文件的问题。

Debug AJAX调试 AJAX

Check if AJAX request was sent and what was the response.检查是否发送了 AJAX 请求以及响应是什么。 You can do this in Developer Tools in Networking tab.您可以在“网络”选项卡的“开发人员工具”中执行此操作。 You can see request and response body, so it can help you determine where the problem is.您可以看到请求和响应正文,因此它可以帮助您确定问题所在。

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

相关问题 我尝试使用Js和CSS创建Tab,但它们无法正常工作 - I tried creating Tabs using Js and CSS but they won't work 如何在CSS上延迟动画? 我尝试了动画延迟。 不起作用 - How to delay animation on CSS? I've tried animation-delay. Doesn't work 如何调用方法 myFunction 返回(名称)我已经尝试了很多方法但它不起作用 - How to invoke the method myFunction to return (name) i've tried many ways but it doesn't work 使用AJAX请求加载图片无法正常工作 - Loading image using AJAX Request won't work 如果不发送数据,Ajax请求将无法工作 - Ajax request won't work without sending data jQuery AJAX请求在Internet Explorer中不起作用 - jQuery AJAX request won't work in Internet Explorer 包装在函数中的异步AJAX请求不适用于promise() - Async AJAX request wrapped in function won't work with promise() 即使我尝试了很多方法来强制重绘,进度条也不会在繁重的工作中重绘 - Progress bar wouldn't repaint during heavy work, even though I've tried many methods to force repaint 我需要一个带有可滚动主体的固定表头。 我已经尝试了一切,但似乎不起作用 - I need a fixed table header with a scrollable body. I have tried everything but it won't seem to work 我试图拖放一个元素。 为什么我的代码不能正常工作? - I have tried to drag and drop an element. Why won't my code work properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM