douhan4243 2018-07-10 04: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部