Transfering form data in the url with post and get
I have a problem. I need to transfer a message th开发者_如何转开发at a user types to the same page (cause I need to save it to the database). I have a form and a 'submit button'.
If I use the command 'post' I am able to save transfer the data to the same page and save it after extracting the post variables.
Problem is that I dont send the page_id in the url (I could acheive it with GET method, but I cant use it, cause the users message could be too long to go into the url).
I tried to do some javascript code, to transfer the 'submit' button to simple button and do a redirect..:
<script type="text/javascript">
document.getElementById('messageSent').addEventListener('click', function () {
window.location = 'article.php?articleID=<?php echo $_REQUEST['pageID']; ?>';
}, false);
</script>
But then the POST variables are lost.
Basically, what I want is to achieve the effect of post and get, I want to post the the variables while encoding the url to look like this:
home.php?pageID=<?php echo $_REQUEST['pageID']; ?>
The page should repost to itself as post request with the pageID set..
How do I achieve that?!?
You'll have to dynamically build a form, populate it with the POST values, and submit it using JavaScript:
<form id="myform" action="target.php" method="post">
<input type="hidden" value="<?php echo $_POST['pageID']; ?>">
</form>
<script>
// ... eventually....
document.getElementById("myform").submit();
</script>
You should use an URL containing the page id in action of form. For example:
<form action="home.php?pageID=<?php echo $_REQUEST['pageID']; ?>" method="post">
</form>
精彩评论