POST without Form Method (PHP)

前端 未结 7 1780
囚心锁ツ
囚心锁ツ 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条回答
  • 2021-02-13 15:44

    One way is to use JavaScript to submit an invisible form. HTML:

    <button id="my-button">Go to other page</button>
    <form id="my-form" method="POST" action="/other-page" style="display: none">
      <input type="hidden" name="param1" value="value1">
      <input type="hidden" name="param2" value="value2">
    </form>
    

    JavaScript (jQuery):

    $("#my-button").click(function(e) {
        $("#my-form").submit();
    });
    

    You could also use a link (<a> tag) instead of a button, etc.

    This will make the browser go to the other page. If you want to stay on the current page and just post some data in the background, use the ajax functions in jQuery or similar libraries.

    0 讨论(0)
  • 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:

    <hmtl>
        <head>
            <script type="http://code.jquery.com/jquery-1.5.1.min.js"></script>
        </head>
        <body>
            <div class="my-fake-form">
                <input id="posting-value-1" type="text" />
                <a id="submit-form-link" href="#submit">Submit this Div!</a>
            </div>
        </body>
    </html>
    

    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:

    <?php
    $post = $_POST['post'];
    
    //Do Something with the value!
    
    //On Succes return json encode array!
    echo json_encode(array("finish" => true));
    ?>
    

    AJAX documentation
    AJAX function documentation
    AJAX tutorial

    0 讨论(0)
  • 2021-02-13 15:48

    For send request in POST from a page, you can do with JQuery.post (in this case, you must use the library) or cURL (in php)

    0 讨论(0)
  • 2021-02-13 15:52

    You'll need some javascript if you want to POST from the browser and make it look like a link:

    you can use submit() function . see http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml for example.

    but if you want it to act like GET, why not use GET ?

    0 讨论(0)
  • 2021-02-13 15:55

    Server side you can achieve this using cURL extension. Tutorial: here I'm not 100% sure you are looking for a server-side solution though?

    0 讨论(0)
  • 2021-02-13 16:02

    Just use sessions.

    The storage of sessions in a cookie is as indicated by the answers above limited to the ID of the session. Cookies can even be configured to be sent as HTTP-cookies only, if you're security-minded.

    On the requesting page, set the session key-value pair you want. On the requested page request the previously set session key (check if it exists first). In you're php.ini you can choose to use session cookies as HTTP-only so you have more security.

    Furthermore: posting data can be as simple as using AJAX or cURL as described above also.

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