Posting data to self using jquery

前端 未结 3 1383
再見小時候
再見小時候 2020-12-19 01:38

I am trying to post some form data to self with no success.My code is as follows


    Post to self
    

        
相关标签:
3条回答
  • 2020-12-19 02:19

    you are mixing two approch altogether.

    <form id="myform" action="" >
      .....
    </form>
    

    To send ajax request to the same page you can keep url parameter empty/removed

    TRY

     <script type="text/javascript">
        $(document).ready(function () {
           $('.x-button').live("click", function () {
               $.post({
                      data: $('form#myform').serialize(),
                      success: function (data) {
                         alert("Data Loaded:");
                      }
                  });
              });
            });
    </script>
    
    0 讨论(0)
  • 2020-12-19 02:29

    Of course you do not want to include the whole page to the response text so you need a statement if ajax is requested

        <?php
    /* AJAX check  */
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    
      if(isset($_POST))
      {
            print_r($_POST);
      }
    
    }else{
    ?>   
    <html>
        <title>Post to self</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
    
        </script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('.x-button').live("click", function () {
                    $.ajax({
                        type: "POST",
                        url: "self.php",
                        data: $(".aj").serialize(),
                        success: function (data) {
                            alert("Data Loaded:");
                        }
                    });
                });
            });
        </script>
        </head>
    
        <body>
           <form name="input" action="" class="aj" method="post">
                    <article>
                        <label>Firstname</label>
                        <input type="text" name="firstname" value="Ed" class="x-input"
                        />
                    </article>
                    <article>
                        <label>Lastname</label>
                        <input type="text" name="lastname" value="Doe" class="x-input"
                        />
                    </article>
                    <article>
                        <label>City</label>
                        <input type="text" name="city" value="London" class="x-input"
                        />
                    </article>
                    <input type="submit" value="Update Options" class="x-button" />
                </form>
        </body>
    
    </html>
    <?php }?>
    
    0 讨论(0)
  • 2020-12-19 02:30

    add return false; at the end of the inline javascript function to avoid the form gets submitted the "normal" way

    0 讨论(0)
提交回复
热议问题