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";
    ?>

图片转代码服务由CSDN问答提供 功能建议

在我的测试工作中,我想通过按“保存”按钮将数据发送到DB并保持在同一页面 。 但是在向DB发送数据后,此按钮打开/file.php并显示“Success”。 我尝试通过JS找到解决任务,但它不起作用。

  $('#sub')。click(function(){
 $ .post($('myForm')  .attr('action'),$('#myForm:input')。serializeArray(),function(info){$('result')。html(info);}); 
 clearInput(); 
  } 
; 
 
 $('myForm')。submit(function(){
 return false; 
}); 
 
函数clearInput(){
 $('#myForm:input')。  each(function(){
 $(this).val(''); 
}); 
} 
   
 
 
 &lt; html&gt; 
&lt; head&gt; 
&lt; title&gt; 
 OneTwoThree 
&lt; / title&gt; 
&lt; / head&gt; 
&lt; body&gt; 
 
 
&lt;  form id ='myForm'action =“db.php”method =“post”&gt; 
名称:&lt; input type =“text”name =“name”&gt;&lt; br&gt; 
年龄:&lt;输入 type =“text”name =“age”&gt;&lt; br&gt; 
&lt; button id =“sub”&gt;发送&lt; / button&gt; 
&lt; / form&gt; 
 
&lt; span id =“result”  &gt;&lt; / span&gt; 
 
&lt; script src =“https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”&gt;&lt; / script&gt; \  n&lt; script src =“script / myjs.js  “type =”text / javascript“&gt;&lt; / script&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

可调用的PHP文件:

 &lt;?php 
 $ conn = mysql_connect('localhost','root',''); 
 $ db = mysql_select_db('mytestbd'); 
 \  n $ name = $ _POST ['name']; 
 $ age = $ _POST ['age']; 
 
 $ q =“INSERT INTO`post`(`number`,`sometext`)VALUES('  $ age','$ name')“; 
 
 if(mysql_query($ q))
 echo”Success“; 
 else 
 echo”Fail“; 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    点赞 打赏 评论
  • dongliu0823 2018-07-11 03:19

    Just use a include statement.

    if(mysql_query($q))
        echo "Success";
        include('yourFileWithTheForm.php');
    else
        echo "Fail";
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题