-5

I have two page -
1. Index.html
2. Main.html

I want to sent a data from index to main. Which i can do like this:

<form action="Main.html" method="POST">
   <input type="text" name="q">
</form>

I want to display the data sent from Index.html on Main.html

I know two methods of doing it -
1. using method="GET" in the form tag and get the data from the src(link).
2. Using side languages like php.

I dont want to use the first method to do this thing.

Please someone give me the codes of php for doing this.

It would be more help if you can tell any other way of doing this

Conor
  • 2,473
  • 1
  • 14
  • 22
  • `using method="post" in the form tag` and `get the data from the src(link).` don't you think it's controversial? – Alive to die - Anant Jul 16 '16 at 14:38
  • May be you can check the option of sending data via query string parameter. – Arun Ghosh Jul 16 '16 at 14:38
  • Change the form `method` to `GET`, then use this: http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter – Rory McCrossan Jul 16 '16 at 14:39
  • @Rory McCrossan i dont want to use that thing – Conor Jul 16 '16 at 14:41
  • To get the data from the url, you would need `method="GET"`, not `method="POST"`. Please give this a try yourself before asking for code. Using PHP, you can grab the information on the server side from the global `$_POST` variable. – m-a-r-c-e-l-i-n-o Jul 16 '16 at 14:42
  • Why not? Your only client side alternative is to use `localStorage` or `sessionStorage` then, but they will be more work as you need to ensure that the values came from the previous page only. Failing that you'll have to do something server-side. – Rory McCrossan Jul 16 '16 at 14:42
  • *"give me the codes of php "* ... you could have easily researched this first – charlietfl Jul 16 '16 at 15:30

2 Answers2

4
  1. Change your Main.html to Main.php
  2. To get data in the Main.php with post method is just: $_POST['q'].

If you want to display your input text, just echo it: echo $_POST['q'].

Or you can save it into a variable too: $var = $_POST['q'].


Or if you don't want to use PHP, you can save input's value into a cookie and get that cookie's value in the Main.html with just JavaScript.

// function to get cookie || Parameter is your required cookie's name
function getcookie(cookiename)
{
    var name = cookiename+"=";
    var cookiearray = document.cookie.split(';');
    for(var i=0; i<cookiearray.length; i++)
    {
        var c = cookiearray[i].trim();
        if(c.indexOf(name) === 0)
        {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
Harkály Gergő
  • 733
  • 8
  • 18
2

You have another option, if you don't want to use server side language...

you can use JavaScript, or it's library like jQuery to do that.

One HTML file:

<form method="POST">
    <input type="text" name="q">
</form>

Other HTML file:

<div id="here"></div>

The jQuery file:

var result = $("input[name=q]").val();
$("#here").text(result);

if you want to use it, read more about JavaScript and jQuery... (Google)

SeReGa
  • 1,219
  • 2
  • 11
  • 32