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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler