3

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?

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

to send data through post you need to specify it as a from attribute, method and action @Helloise
thank you but i also need to send my mailid...how will i incorporate that in the action='rainbowcode.net/apps_dev.php/profiles/mail?mailid=....' ??? thanks
0

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

Comments

0

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.

3 Comments

i added this but it made no diff though.. where SCRIPT_NAME would be my script name??
@Helloise Smit: Yes, SCRIPT_NAME will be the name of your script, but looking at your JavaScript I get the impression that you're trying to post the form to another page by doing a a GET request. It's difficult to tell what you're trying to do...
ok i have it as my script name..mailid is a global var that i set in another js funtion and it contains the right id when i rediret to a new page i just want to access the message the user typed into <textarea>

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.