Send POST data to PHP without using an HTML form?

前端 未结 7 2009
眼角桃花
眼角桃花 2020-12-08 11:54

Is there anyway to send post data to a php script other than having a form? (Not using GET of course).

I want javascript to reload the page after X seconds and post

7条回答
  •  再見小時候
    2020-12-08 12:17

    As requested above, here is how you could dynamically add a hidden form and submit it when you want to refresh the page.

    Somewhere in your HTML:

    
    

    And some Javascript:

    function postRefreshPage () {
      var theForm, newInput1, newInput2;
      // Start by creating a 
    theForm = document.createElement('form'); theForm.action = 'somepage.php'; theForm.method = 'post'; // Next create the s in the form and give them names and values newInput1 = document.createElement('input'); newInput1.type = 'hidden'; newInput1.name = 'input_1'; newInput1.value = 'value 1'; newInput2 = document.createElement('input'); newInput2.type = 'hidden'; newInput2.name = 'input_2'; newInput2.value = 'value 2'; // Now put everything together... theForm.appendChild(newInput1); theForm.appendChild(newInput2); // ...and it to the DOM... document.getElementById('hidden_form_container').appendChild(theForm); // ...and submit it theForm.submit(); }

    This is equivalent to submitting this HTML form:

    
      
      
    
    

提交回复
热议问题