php - 我想在不重新加载页面且没有 OK 消息的情况下提交表单

标签 php ajax forms reload

<分区>

我需要您帮助解决以下问题:

我曾尝试在不重新加载页面的情况下提交表单,但没有成功。 我读过其他相关问题,但我做不到。

我想要以下内容: 当用户点击按钮时,数据应该被发送到服务器而不显示 OK 消息。 你能帮助我吗? 这是我的代码:

index.html

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    <p align="center">Example</p>
    <table align="center" width="730" border="0">
    <tr>
    <td align="center">
    <div>
      <table class="blueTable" style="float: left">
        <thead>
    <tr>
     <th colspan="1"><u>Menu</u></th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td><input type="button" value="New" id="new" onclick="new1();" class="button12"/></td></tr>
    <tr>
    <td><input type="button" value="Load" id="load" onclick="load2()" class="button"/></td></tr>
    <tr>
    <td><form name="SaveGame" id="SaveGame" method="post" action="http://127.0.0.1/PHP/mine2.php" enctype="multipart/form-data">
    <input type="submit" value="Save" id="save" onclick="save3()" class="button"/>
    </form>
    </body>
    </html>

我的2.php

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt insert query execution
$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
if(mysqli_query($link, $sql)){
    } else{
    }

// Close connection
mysqli_close($link);
?>

最佳答案

改变你的 html 文件,像这样:

<html>
    <head>
    <script
    src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    <p align="center">Example</p>
    <table align="center" width="730" border="0">
    <tr>
    <td align="center">
    <div>
      <table class="blueTable" style="float: left">
        <thead>
    <tr>
     <th colspan="1"><u>Menu</u></th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td><input type="button" value="New" id="new" onclick="new1();" class="button12"/></td></tr>
    <tr>
    <td><input type="button" value="Load" id="load" onclick="load2()" class="button"/></td></tr>

    <button  id="save" onclick="save3()" class="button">Save</button><!--This button will perform action-->


    <script>



       function save3(){
        var value = 123;//dummy variable to show you its functionality
        $.ajax({
               type: "POST",
               url: "http://127.0.0.1/PHP/mine2.php",
               data: {value: value}, //that value comes from above which I have initialized
               success: function(data)
               {
                   console.log(data); //it will show response from php file in your console
               }



        });
     }
    </script>

    </body>
    </html>

在 php 文件 mine2.php 中,这是您访问来自 ajax 请求的 value 的方式

<?php
 if(isset($_POST['value']))
  {
    $value = $_POST['value'];
  }
 ?>

关于php - 我想在不重新加载页面且没有 OK 消息的情况下提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52090879/

相关文章:

php - 带有wordpress作为子文件夹的Rails heroku应用程序,无法登录到管理员

php - MYSQL SELECT 和 JSON_ENCODE

android - Cordova session cookie 不适用于 Android Lollipop

javascript - 您如何建议我根据选择的选项显示不同的表单?

forms - 如何以编程方式将焦点设置为在 Angular2 中动态创建的 FormControl

forms - 在 Angular 中以 react 形式更改隐藏字段的值

php - amphp是如何工作的

php - Push Notification Server 使用什么语言维护与 Apple Push Notification Server 的套接字流连接

javascript - 如何为网页 <select> 框创建复杂的数据绑定(bind)?

Javascript:如何检查 fct 是否正在运行,如果没有运行,则使其运行?