charlie_cat
charlie_cat

Reputation: 1850

$_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 have 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?

Upvotes: 3

Views: 1420

Answers (4)

Ibu
Ibu

Reputation: 43850

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' />

Upvotes: 0

stealthyninja
stealthyninja

Reputation: 10371

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.

Upvotes: 0

user471710
user471710

Reputation:

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 ).

Upvotes: 0

Vadiklk
Vadiklk

Reputation: 3764

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.

Upvotes: 7

Related Questions