0

I have 2 pages. The first page has a Javascript function that gets the value of h1 and when the user click the button, the alert will be displayed with the value of h1 and it will redirect to another page. The 2nd page have an HTML form.

Now I want to call the js function from the 1st page and display the value in the input text (2nd page). I tried this <?php echo "<script>document.writeln(h1function);</script>"; ?>, but it's not working correctly.

Here's the js script:

function h1function(){
    var h1 = document.getElementsByClassName("entry-title");
        for(var i = 0; i < h1.length; i++){
            alert(h1[i].innerHTML);
    }
}

1st page:

<a href="http://example.com/list/summary/" target="_blank" onclick="h1function()">

HTML (2nd page):

<input type="text" name="h1title" value="<?php echo "<script>document.writeln(getJob);</script>"; ?>" size="40" id="h1title" required/>
Ashan
  • 479
  • 7
  • 28
User014019
  • 1,215
  • 8
  • 34
  • 66
  • How are you loading the 2nd page? If it's just following the link, the `h1` element is long gone by then and you can't retrieve it from there. You'd have to find a way to persist the info between pages. – MinusFour Feb 11 '16 at 03:04

2 Answers2

0

You can use :

  1. Cookies in Javascript

  2. HTML5 Storage as described here

solimanware
  • 2,952
  • 4
  • 20
  • 40
0

You could send the data with GET

function h1function(){
    window.location.href = "http://example.com/page2?&h1="+h1[i].innerHTML;
  }

HTML 2nd page

<input type="text" name="h1title" value="<?php echo $_GET['h1']" size="40" id="h1title" required/>
Wistar
  • 3,770
  • 4
  • 45
  • 70