POST without Form Method (PHP)

前端 未结 7 1769
囚心锁ツ
囚心锁ツ 2021-02-13 15:24

Is there any way to pass stuff from one page to another using the POST method without using forms.

Like in get you can just append a ? with whatever you wan

7条回答
  •  -上瘾入骨i
    2021-02-13 15:45

    You should use jQuery to this. You can use its ajax() function. Visit the link below and read the full description of it with the functions list to help you out.

    Here is a sample code for you:

    
        
            
        
        
            
        
    
    

    Ajax code:

    function fake_form_submit ()
    {
    
        var post = $('input#posting-value-1').val();
    
        $.ajax({
        'url': 'your-php-file.php',
        'type': 'POST',
        'dataType': 'json', 
        'data': {post: post},
        'success': function(data)
         {
             if(data.finish)
             {
                $("div.my-fake-form").attr("innerHTML","Form Submited!");
             }
             else
             {
                $("div.my-fake-form").attr("innerHTML","Form Not Submited!");   
             }
         },
         beforeSend: function()
           {
                $(document).ready(function () {
                    $("div.my-fake-form").attr("innerHTML","Loading....");
                });
           },
            'error': function(data)
            {
              $(document).ready(function () {
                $("div.my-fake-form").attr("innerHTML","ERROR OCCURRED!");
              });
            }
          });
    }
    
    $(document).ready(function () {
        $('a#submit-form-link').click(function (e) {
           e.preventDefault();
           fake_form_submit();
        });
    });
    

    PHP:

     true));
    ?>
    

    AJAX documentation
    AJAX function documentation
    AJAX tutorial

提交回复
热议问题