【问题标题】:Ajax and Php Post in One ButtonAjax 和 PHP 一键发布
【发布时间】:2018-10-13 07:20:06
【问题描述】:
    <form method="post" id="test-form">
     <input type="text" name="test_user" id="test_user" value="<?php echo 
      $userRow['user_id'];?>"  hidden>

    <button type="submit" class="btn btn-danger " name="test_button" 
    id="test_button" ">Test Button</button> 

     </form>

 if($_POST)--->php this not way =too much post will be on the same php page

 if(isset($_POST['test_button']))--->php this way
                {
 $test_user= trim($_POST['test_user']);
 $test=1;

     $son_gorulme=$baglanti->prepare("UPDATE test_status SET status=:test 
       WHERE test_user=:id");  

    $son_gorulme->bindparam(":id",$test_user);
    $son_gorulme->bindparam(":test",$test);
    $son_gorulme->execute();


                }
  bla bla blaa....

问题是:我怎样才能用 ajax 做到这些?

我想用ajax返回表单值?太多的帖子会在同一个php页面上

非常感谢您

【问题讨论】:

  • 您似乎想要介绍 Ajax。 Stackoverflow 期望您在提出问题之前执行a degree of research,并且不乏 Ajax 教程(包括来自MDN)。

标签: javascript php ajax post


【解决方案1】:
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>

<form method="post" id="test-form">
<input type="text" name="test_user" id="test_user" value="<?php echo $userRow['user_id'];?>"  hidden>

<button type="submit" class="btn btn-danger " name="test_button" id="test_button">Test Button</button> 

</form>


<script>


$("#test_button").click(function(){

    var user = $("#test_user").val();

    $.ajax({
        method: "POST",
        url: "form.php",
        data: { 'user': user }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
});
</script>

<?php 

if(isset($_POST['user']))
{
    $test_user= trim($_POST['user'])
}

?>

【讨论】:

    【解决方案2】:
    <form method="post" id="test-form">
         <input type="text" name="test_user" id="test_user" value="<?php echo 
          $userRow['user_id'];?>"  hidden>
    
        <button type="submit" class="btn btn-danger " name="test_button" 
        id="test_button" ">Test Button</button> 
     </form>
    

    如果你准备好使用jquery,可以试试这个ajax表单提交方式

         $("#test-form").on('submit', (function(e) {
                e.preventDefault();
                $.ajax({
                    url: "url",
                    type: "POST",
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    success: function(data) {
    alert ('success')
                }
    
    
                })
            }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2013-03-09
      相关资源
      最近更新 更多