douhan4243 2018-07-10 12:58
浏览 78
已采纳

如何在不重新加载的情况下调用PHP函数?

In my test work, I want to send data to DB by pressing "Save" button and stay at the same page. But this button after sending data to DB opens /file.php and shown "Success". I tried to solve the task by JS I found, but it doesn`t work.

$('#sub').click( function() {
    $.post( $('myForm').attr('action'), $('#myForm :input').serializeArray(), function(info){ $('result').html(info);} );
    clearInput();
});

$('myForm').submit(function() {
    return false;
});

function clearInput() {
    $('#myForm :input').each( function() {
        $(this).val('');
    });
}
<html>
<head>
    <title>
        OneTwoThree
    </title>
</head>
<body>


<form id='myForm' action="db.php" method="post">
    Name: <input type="text" name="name"><br>
    Age: <input type="text" name="age"><br>
    <button id="sub">Send</button>
</form>

<span id="result"></span>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="script/myjs.js" type="text/javascript"></script>
</body>
</html>

Callable PHP file:

<?php
    $conn = mysql_connect('localhost','root','');
    $db = mysql_select_db('mytestbd');

    $name = $_POST['name'];
    $age = $_POST['age'];

    $q = "INSERT INTO `post`(`number`, `sometext`) VALUES ('$age','$name')";

    if(mysql_query($q))
        echo "Success";
    else
        echo "Fail";
    ?>
  • 写回答

2条回答 默认 最新

  • ds15812330851 2018-07-10 13:18
    关注

    Try this block of js which includes some changes and adjustments (noted below):

    $(document).ready(function() {               // ready wrapper (was missing in your code)
    
        $('#sub').click( function(e) {           // added 'e'
            e.preventDefault();                  // added to stop buttons default behavior
            $.post( $('#myForm').attr('action'), // added '#' to id locator
                    $('#myForm').serialize(),    // reduced to just serialize the form data
                    function(info){
                        $('#result').html(info); // added '#' to id locator
                        clearInput();            // moved clear form into success callback
                    }
                  );
        });
    
        $('#myForm').submit(function() {         // added '#' for id locator
            return false;                        // this submit handler is most likely
        });                                      // not needed, but doesnt hurt
    
        function clearInput() {
            $('#myForm :input').each( function() {
                $(this).val('');
            });
        }
    
    });
    

    PS Once you get the submission process working, you will want to switch your use of mysql_* functions to either mysqli or PDO (as mysql_ is depreciated in php 5.5x, and removed in php7+).

    Then after you switch, look into Prepared Statements to safeguard from sql injection attacks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件