微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP-我想提交一个表单而无需重新加载页面并且没有OK消息

在以下问题上,我需要您的帮助:

我尝试提交表单而不重新加载页面,但是没有成功.
我读过其他相关问题,但我做不到.

我想要以下内容
用户单击按钮时,应将数据发送到服务器而不显示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>

mine2.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请求的值的方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐