Setting POST variable without using form

前端 未结 4 1409

Is there a way to set a $_POST[\'var\'] without using form related field (no type=\'hidden\') and using only PHP. Something like

$_POST[\'name\'         


        
相关标签:
4条回答
  • 2020-12-01 13:59

    Yes, simply set it to another value:

    $_POST['text'] = 'another value';
    

    This will override the previous value corresponding to text key of the array. The $_POST is superglobal associative array and you can change the values like a normal PHP array.

    Caution: This change is only visible within the same PHP execution scope. Once the execution is complete and the page has loaded, the $_POST array is cleared. A new form submission will generate a new $_POST array.

    If you want to persist the value across form submissions, you will need to put it in the form as an input tag's value attribute or retrieve it from a data store.

    0 讨论(0)
  • 2020-12-01 14:13

    If you want to set $_POST['text'] to another value, why not use:

    $_POST['text'] = $var;
    

    on next.php?

    0 讨论(0)
  • 2020-12-01 14:19

    You can do it using jQuery. Example:

    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    
    <script>
        $.ajax({
            url : "next.php",
            type: "POST",
            data : "name=Denniss",
            success: function(data)
            {
                //data - response from server
                $('#response_div').html(data);
            }
        });
    </script>
    
    0 讨论(0)
  • 2020-12-01 14:20

    you can do it using ajax or by sending http headers+content like:

    POST /xyz.php HTTP/1.1
    Host: www.mysite.com
    User-Agent: Mozilla/4.0
    Content-Length: 27
    Content-Type: application/x-www-form-urlencoded
    
    userid=joe&password=guessme
    
    0 讨论(0)
提交回复
热议问题