Javascript I';我尝试了一些ajax请求,但它赢了';行不通

Javascript I';我尝试了一些ajax请求,但它赢了';行不通,javascript,php,ajax,request,Javascript,Php,Ajax,Request,我试图向'inserir.php'发出请求,在那里我将处理来自ajax请求的数据并将其插入数据库。但是当我点击按钮时,什么也没发生。。甚至一个错误都没有。 这是我的表格 <div class="modal-body"> <!-- FORM--> <form method="POST" id="formulariopost"> <

我试图向'inserir.php'发出请求,在那里我将处理来自ajax请求的数据并将其插入数据库。但是当我点击按钮时,什么也没发生。。甚至一个错误都没有。 这是我的表格

<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>
这是我的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));
$servername=“localhost”;
$username=“root”;
$password=“root”;
$dbname=“copacobre”;
$email=$_POST[“email”];
$nome_equipa=$_POST[“nome_equipa”];
$gametag=$_POST[“昵称”];
$numerowhats=$_POST[“numero”];
$newURL=“/copac/#”;
echo$电子邮件;
$errorMSG=“错误测试”;
//创建连接
$conn=newmysqli($servername、$username、$password、$dbname);
//检查连接
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
}
if($conn->query($sql)==TRUE){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error; } $sql=“插入pagamentos(电子邮件、设备名称、游戏标签、数字帽) 值(“$email.”、“$nome_equipa.”、“$gametag.”、“$numerowhats.”); $conn->close(); echo json_编码(数组('code'=>200));
看起来事件绑定是错误的。 您需要绑定到提交事件

同样在HTML代码中,“提交”按钮用于关闭,一个常规按钮用于保存更改,通常“提交”用于执行提交数据并继续的操作

如果应该是

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

请记住,在函数结束时,如果成功,则应返回true,否则应返回false以取消事件。

事件未绑定的原因可能有很多。您可以尝试在几个步骤中调试它

清除缓存 是的,浏览器喜欢缓存。确保页面中加载了JS文件的实际版本

如果您使用的是Chrome,则CTRL+F5将完成此工作

检查代码运行的距离 在开发人员工具控制台中添加断点,或者-简单方法-将一些console.log添加到代码中的几个点上-例如,在document.ready之后、在单击按钮之后、在ajax请求之前以及ajax成功之前。别忘了AJAX也可以返回错误代码,方法“success”无法处理错误响应

根据代码到达的距离(例如,使用console.logs),您可以确定问题所在。如果在document.ready后面看不到日志,则可能存在包含文件的问题

调试AJAX
检查是否发送了AJAX请求以及响应是什么。您可以在“网络”选项卡中的“开发人员工具”中执行此操作。您可以查看请求和响应正文,这样可以帮助您确定问题所在。

代码中没有id为
saveg
的按钮。@DanielVítek感谢您的回复。。我忘了粘贴整个模式代码。。我已经有一个名为savegI的按钮忘记粘贴整个模式代码。。我已经有了一个名为savegI的按钮,我得到了这个错误:jquery-1.12.1.min.js:4未捕获范围错误:cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)超过了最大调用堆栈大小在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)在cc(jquery-1.12.1.min.js:4)看起来像是某种递归。顺便说一下,您正在向数据传递错误的变量。绰号和数字。应该是gametag和numerowhats Daniel那是个错误。。变量。。天哪,谢谢你的帮助很高兴我能帮忙:)
$('#formulariopost').submit(function(e){
    ....
})