2 Replies - 434 Views - Last Post: 14 November 2012 - 11:16 AM

#1 phoenix.softeng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-June 10

sending names to another page

Posted 09 November 2012 - 08:10 AM

Hi everyone!
I am tyring to design two pages with Jscript and HTML.It's is really simple but I have problem:)
on first page,you write your first name and family and by you clicking on submit button you will be directed to another web page that Welcome you.
I konw I should use querystring.
This code is for first page.



    <!DOCTYPE html>
    <html>
    <body>
    <form id="frm1" action="HW1.html" method="post" target="_blank">
      First name: <input type="text" name="fname"><br>
      Last name: <input type="text" name="lname"><br>
      <input type="Submit" value="Enter">
    </form>
    
    <script>
    document.write(document.getElementById("frm1").method);
    </script>
    
    <p>Click to see welcome message</p>
    
    </body>
    </html>


I try this for something like this for the second one.(HW1.html)



    <!DOCTYPE html>
    <html>
    <head>
     <script>
    function welcome(fname,lname)
    {
     alert("Welcome "   +fname +" "  +lname);
     }
    </script>
    
    
     <script>
     function func()
       {
        var fname= Request.QueryString("fname");
        var lname= Request.QueryString("lname"); 
    	 welcome(a,B)/>/>;
    	 }
    </script>
    </head>
    <body>
    alert(func());
    
    </body>
    </html> 




It's better if I don't use alert.

and another try:



    <script>
    document.write("<p> welcome <%=Request.QueryString("fname")%>. 
     <%= Request.QueryString("lname")%> </p>");
    </script>
    
    <script type="text/javascript">
    alert('Welcome <%=Request.QueryString("fname")%> <%= Request.QueryString("lname")%>');
    </script>


anyway I really don't know how to show the names in second page correctly...

This post has been edited by Dormilich: 09 November 2012 - 02:06 PM
Reason for edit:: please use [CODE] [/CODE] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: sending names to another page

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: sending names to another page

Posted 09 November 2012 - 08:45 AM

Hello,

If we send the user to "index.php?name=Barry" (index.php being your page), on that page we can access the name like so...
<script>
	function getParameterByName(name)
	{
	  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	  var regexS = "[\\?&]" + name + "=([^&#]*)";
	  var regex = new RegExp(regexS);
	  var results = regex.exec(window.location.search);
	  if(results == null)
		return "";
	  else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}
	
	alert(getParameterByName('name'));
</script>


Source: http://stackoverflow...y-string-values
Was This Post Helpful? 1
  • +
  • -

#3 phoenix.softeng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-June 10

Re: sending names to another page

Posted 14 November 2012 - 11:16 AM

It seems this one works

<html>
<body>
<script>
var urlParams = {};
var match,
pl = /\+/g,
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
alert("hello " + urlParams["firstname"] + " " + urlParams["lastname"]);
</script>
</body>
</html>
<html>
<body>
<form method="get" action="p2.html">
First Name:
<input type="text" name="firstname"><br />
Last Name:
<input type="text" name="lastname">
<br />
<input type="submit" name="Ok">
</form>
</body>
</html>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1