19

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 some data to the page at the same time. I could do it with GET but I would rather use POST, as it looks cleaner.

Thanks a lot.

EDIT: Would it be possible to do with PHP header? I'm sure it is better to use JQuery but for my current situation I could implement that a lot easier/faster : )

Cheers

I ended up doing it like so:

<script>
  function mySubmit() {
    var form = document.forms.myForm;
    form.submit();
  }
</script>

...

<body onLoad="mySubmit()";>
  <form action="script.php?GET_Value=<?php echo $GET_var ?>" name="myForm" method="post">
    <input type="hidden" name="POST_Value" value="<?php echo $POST_Var ?>">
  </form>
</body>

Seems to work fine for me, but please say if there is anything wrong with it!

Thanks everyone.

3
  • You could, if you want to avoid using AJAX, have hidden form, or create one dynamically, and just submit the form using Javascript after a timeout. AJAX may be the best solution, it depends exactly what you want to do, and AJAX arguably gives you more scope for handling errors gracefully. Commented Dec 26, 2011 at 20:53
  • Ah yes that sounds possible for me, I'm sure the other suggestions are better overall but this is a small project and this solution sounds like it suits my style of coding :) Commented Dec 26, 2011 at 20:57
  • See my answer below for details of how this can be done in framework-free Javascript. Commented Dec 26, 2011 at 21:19

7 Answers 7

27

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:

<div id="hidden_form_container" style="display:none;"></div>

And some Javascript:

function postRefreshPage () {
  var theForm, newInput1, newInput2;
  // Start by creating a <form>
  theForm = document.createElement('form');
  theForm.action = 'somepage.php';
  theForm.method = 'post';
  // Next create the <input>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:

<form action="somepage.php" method="post">
  <input type="hidden" name="input_1" value="value 1" />
  <input type="hidden" name="input_2" value="value 2" />
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

I posted what I used in the end question/OP (Not sure what you call it on here)
6

You can use JQuery to post to a php page: http://api.jquery.com/jQuery.post/

1 Comment

Looks like I need to learn Ajax and JQuery to advance... A lot of what I have been trying to do looks like it can be done easier with them. However I have never touched either so for the short term I can't really use it (Till I learn about JQuery). Thanks a lot.
4

By jQuery:

$.ajax({
  url: "yourphpscript.php",
  type: "post",
  data: json/array/whatever,

  success: function(){ // trigger when request was successfull
    window.location.href = 'somewhere'
  },
  error: anyFunction // when error happened
  complete: otherFunction // when request is completed -no matter if the error or not
  // callbacks are of course not mandatory
})

or simplest:

$.post( "yourphpscript.php", data, success_callback_as_above );

more on http://api.jquery.com/jQuery.ajax

Comments

4

Use the FormData API.

From the example there:

var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456);

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);

Comments

3

Form your own header, as such:

POST /submit.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userId=admin&password=letmein

Comments

3

How about this:

    function redirectWithPostData(strLocation, objData, strTarget)
{
    var objForm = document.createElement('FORM');
    objForm.method = 'post';
    objForm.action = strLocation;

    if (strTarget)
        objForm.target = strTarget;

    var strKey;
    for (strKey in objData)
    {
        var objInput = document.createElement('INPUT');
        objInput.type = 'hidden';
        objInput.name = strKey;
        objInput.value = objData[strKey];
        objForm.appendChild(objInput);
    }

    document.body.appendChild(objForm);
    objForm.submit();

    if (strTarget)
        document.body.removeChild(objForm);
}

use like this:

redirectWithPostData('page.aspx', {UserIDs: getMultiUserSelectedItems()},'_top');

Comments

2

You can send an xhr request with the data you want to post before reloading the page.

And reload the page only if the xhr request is finished.

So basically you would want to do a synchronous request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.