$_POST array empty
i have this code:
<div id="messageDiv">
<form>
<textarea name="message" rows="10" cols="20" id="message"></textarea></textarea>
<br /><br />
<input type="submit" value="Send" onClick="return sendmail()">
<input type="reset" value="Reset" name='reset'>
</form>
</div>
then i hav开发者_如何学运维e my JS:
function sendmail()
{
var mail = document.getElementById('message').value;
window.location.href = "http://www.rainbowcode.net/apps_dev.php/profiles/mail?id="+mailid;
return false;
}
when i alert(mail)
i get the correct value but when i get to my new page via window.location.href i want to access that value...my form is a "post" when i do a print_r($_POST['message'])
i get an empty array..please help?
By using window.location.href
you just change the link you are going to (by that refreshing the page to the desired page, which in your case is a page that gets an id from the URL).
If you want to post values, use document.MyForm.submit();
EDIT:
Obviously you will have to add a normal <form>
tag, and not just an empty one.
Add a name/id to it so you could refer to it using JS + method post + action as an URL.
You have an error in your html that might affect it
<textarea name="message" rows="10" cols="20" id="message"></textarea></textarea>
should be
<textarea name="message" rows="10" cols="20" id="message"></textarea>
And also that is not how a post form is done. the send mail function is not helping at all
<form method='post' action='http://www.rainbowcode.net/apps_dev.php/profiles/mail'>
<textarea name="message" rows="10" cols="20" id="message"></textarea>
<br /><br />
<input type="submit" value="Send">
<input type="reset" value="Reset" name='reset'>
</form>
This should work
UPDATE:
You can always add hidden values that can be used for different purposes without being visible in the form. Like your mail id, it can be added to the form too:
<input name='mailid' type='hidden' value='somevalue' />
It's kind of obvious. Popup window you open does not contain same request variables as "parent" page which opens it. It's a new HTTP request.
Transfer data between "parent" window and pop-up through session ($_SESSION
, http://hr.php.net/manual/en/reserved.variables.session.php ).
Your form
isn't set to post
:
Update your HTML code to:
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
Update
function sendmail()
{
var mail = document.getElementById('message').value;
window.location.href = "http://www.rainbowcode.net/apps_dev.php/profiles/mail?id="+mailid;
return false;
}
Where is mailid
defined? Also, are you trying to print_r($_POST['message'])
in the page you're redirecting to from JavaScript? Try print_r($_GET['id'])
instead.
精彩评论