On one page, I created a form to enter a name, e-mail and the actual text of the message. After clicking on the "Send message" button on the same page, a new page opens, containing the data of the fields filled in in the previous one.

How to do it?

I found different scripts on the Internet, but some use php . I worked with PHP , but when I specify the full path, the button does not want to work:

<form method="post" action="C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\UserMail.php"> <input type="submit"> 

Redid one script, it turned out such a thing:

 <script language="javascript" type="text/javascript"> function goMail(){ var UsName=document.getElementsByName(('UserName').value); if (confirm("Уверены, что хотите отправить сообщение?")) { document.location="UserMail.html?id="+UsName; } } </script> <form> <p>Ваше имя: </p> <input type="text" name="userName" value=""> <br> <input type ="button" value="Отправить сообщение" onClick="goMail()"> <form> 

A new page is opened, but nothing is transmitted here ... Purely there ... Here is the code of that page:

 <td valign="top" colspan="4" align="center"> <font id="UsName"> </font> </td> 

What you need and how to do?

  • If I'm not mistaken, then try using Kukki) - Zeloras
  • cookie is not always suitable because has a length limit. - user309954 2:42 pm

5 answers 5

You need to fill in the fields with the data passed in the query string (you only have id ). If you use only javascript , then at the <body> new page you need to add an onload , in which after the page loads you will read the passed parameter values ​​and paste them into the required fields. To read the parameters, you can use the function from here http://www.netlobo.com/url_query_string_javascript.html

I am writing the answer here so that the code is visible to the code. First page:

 <script language="javascript" type="text/javascript"> function goMail() { if (confirm("Уверены, что хотите отправить сообщение?")) { document.location = "UserMail.html?id=" + document.getElementById("userName").value; } } </script> <form> <p>Ваше имя: </p> <input id="userName" type="text" name="userName" value="" /> <br/> <input type ="button" value="Отправить сообщение" onclick="goMail()" /> </form> 

The second (for educational purposes and with one parameter, you can do without additional functions):

 <head> <title></title> <script language="javascript" type="text/javascript"> function OnLoad() { var paramValue = window.location.href.split("?")[1].split("=")[1]; document.getElementById("UsName").innerHTML = paramValue; } </script> </head> <body onload="OnLoad()"> <font id="UsName"> </font> </body> 
  • Sorry Georgy!) And how will it all look for my version?) I reviewed your function on netlobo.com/url_query_string_javascript.html , but, excuse me, of course, I'm in JavaScript recently, about a week, and indeed in WEB programming the maker , so I did not understand much ... Can you explain by my example? Thank. - Leshij_2005
  • Thank you very much Georgy!) Everything works well for one parameter. But let's say it is necessary to transfer not only the Name, but also the e-mail, so that they can be located in different cells. How to implement it? I tried to change your function, but in the end sends one E-mail ... Here is the input: <p> Your e-mail: </ p> <input id = "userMail" type = "text" name = "userMail" value = ""> In goMail () added var UsMail = document.getElementsByName (('userMail'). Value); if (confirm ("Are you sure you want to send a message?")) {... document.location = "UserMail.html? id =" + document.getElementById "userMail"). value;} How to proceed - Leshij_2005

Ok, for several parameters:

  function OnLoad() { var query = window.location.href.split("?")[1]; // результат - строка запроса без адреса страницы "id=someName&userMail=some@mail.com&usText=MemoText" var params = query.split("&"); // результат - массив строк из пар "id=someName", "userMail=some@mail.com", "usText=MemoText" // теперь брать по очереди document.getElementById("UsName").innerHTML = params[0].split("=")[1]; document.getElementById("userMail").innerHTML = params[1].split("=")[1]; document.getElementById("usText").innerHTML = params[2].split("=")[1]; } 

If you use the function from the link, then

  function OnLoad() { document.getElementById("UsName").innerHTML = gup("id"); document.getElementById("userMail").innerHTML = gup("userMail"); document.getElementById("usText").innerHTML = gup("usText"); } 
  • Sorry Georgy!) Again, I!) What's in the gomail () function should be?) I wrote it like this) function goMail () {var UsName = document.getElementsByName (('userName'). Value); var UsMail = document.getElementsByName (('userMail'). value); var UsText = document.getElementsByName (('userText'). value); if (confirm ("Sure ...")) {document.location = "UserMail.html? id =" + document.getElementById "userName"). value + document.getElementById ("userMail"). value + document.getElementById ("userText"). value;}} I tried for each one to designate document.location, but in the end I only passed the last value ... - Leshij_2005
  • It is better not to guess, and read. document.location opens a new page, we have UserMail.html, then '?' and the parameter list, in the form [parameter name] = [value], parameters are separated using '&', i.e. as a result, document.location should be assigned something like UserMail.html?id=someName&userMail=some@mail.com&usText=MemoText. - Georgy
  • The function looks like this: function goMail () {if (confirm ("Sure ...")) {var UsName = document.getElementsByName ('userName'). Value; var UsMail = document.getElementsByName ('userMail'). value; var UsText = document.getElementsByName ('userText'). value; document.location = "UserMail.html? id =" + UsName + '& userMail =' + UsMail + '& usText =' + UsText; }} - Georgy
  • What advice do you have to read, Georgy?) Thank you so much for clarification of document.location, everything is clear, I would have such a web-programming lecturer;) Did as you wrote, but for some reason passes the value "undefined" ... Means or does not read the contents , or can not find the necessary elements? ... What is the problem then? ( - Leshij_2005
  • Perhaps the syntax. You do not need to write document.getElementsByName (('XXXXX'). Value), but document.getElementsByName ('XXXXX'). Value For "read" :) - w3schools.com/js/js_examples.asp - Georgy

Ok, looked. First, not getElementsByName , but getElementById (last time you copied it ByName .. ByName , not .. ById , sorry), secondly, you textarea c name=userText does not have id . Then, the name=.. attributes are not required. If you are not using anywhere, you need to specify id=..

Those.

one.

 function goMail() { if (confirm("Уверены...")) { var UsName = document.getElementById('userName').value; var UsMail = document.getElementById('userMail').value; var UsText = document.getElementById('userText').value; document.location = "UserMail.html?id=" + UsName + '&userMail=' + UsMail + '&usText=' + UsText; } } 

2

 <textarea id = "userText" ... 

why introduce js if it is not needed for this? The form will transmit the completed data, take it out of the array and assign it to a new form, layers or anything else:) If only for the “sure you want to send”, you can simply check the checkbox that makes the send button active ...

For php, you do not need to specify the full path, it will not master this :) use relative paths.

  • Ama, if you read the comments above, you would understand that I am "in the WEB programming maker";) If you give an example of your answer, I will be very grateful to you :) - Leshij_2005

For several parameters:

 function OnLoad() { var query = window.location.href.split("?")[1]; // результат - строка запроса без адреса страницы "id=someName&userMail=some@mail.com&usText=MemoText" var params = query.split("&"); // результат - массив строк из пар "id=someName", "userMail=some@mail.com", "usText=MemoText" // теперь брать по очереди document.getElementById("UsName").innerHTML = params[0].split("=")[1]; document.getElementById("userMail").innerHTML = params[1].split("=")[1]; document.getElementById("usText").innerHTML = params[2].split("=")[1]; } 

Add to the last parameter:

 params[2].split("=")[1].split("#")[0]; 

Otherwise, if the link is an anchor, it will enter the value.