1

Is it possible to add the h3 from the previous page to the current page's input value? I'm creating a form and want the first input value to be populated by the previous pages h3.

I have an unordered list, and each list item has a h3 which I want to populate the input value with, and each list item has a link to the form. So when that link to the form is clicked, the h3 from the list item is used to populate the input value.

Is this possible?

EdSF
  • 11,753
  • 6
  • 42
  • 83
JV10
  • 891
  • 3
  • 17
  • 40
  • You need to pass the variable between pages using either cookies (which jQuery can do) or with serverside sessions (which jQuery can't). – Blender Jun 15 '12 at 00:35
  • What's wrong with passing it as a query string parameter? – mVChr Jun 15 '12 at 00:37

3 Answers3

1

If you wish to access information, then it needs to be stored somewhere. You can store the data client-side or pass data through a link using a querystring or a hashtag.

On origin page

<a href="nextpage.php?h3=test">link</a>

On receiving page, assuming PHP

<?
$heading = $_GET['h3'];
php echo '<input type="text">'.$heading.'</input>';
?>

edit for comment: for silly aspers use google ;) http://www.w3schools.com/asp/coll_querystring.asp

Sinetheta
  • 9,189
  • 5
  • 31
  • 52
  • @Terry I was in a hurry and took the first google hit for a topic on which I have no expertise (since the OP didn't tag .NET). Would you prefer [a link from the MSDN](http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx)? – Sinetheta Jun 15 '12 at 03:04
0

You need to keep the value of the element from page to page, there are few ways to accomplish this:

In the source page:

  • Query String

    myLink.NavigateUrl = "~/MyTargetPage.aspx?d=" + GetCurrentH3Value();
    
  • Session

    Session["d"] = GetCurrentH3Value();
    
  • Cookies

    Response.Cookies["d"].Value = GetCurrentH3Value();
    

In the target page:

  • Query String

    var lastH3 = Server.HtmlEncode(Request.QueryString["d"]);
    
  • Session

    var lastH3 = Session["d"].ToString();
    
  • Cookies

    var lastH3 = Request.Cookies["d"].Value;
    
Terry
  • 14,099
  • 9
  • 56
  • 84
Jupaol
  • 21,107
  • 8
  • 68
  • 100
0

Using just javascript (jQuery):

First create a function that reads the query string, this question has many examples.

Then:

Source page:

<ul id="listOfH3s">
   <li><h3>1</h3></li>
   <li><h3>2</h3></li>
   <li><h3>3</h3></li>
</ul>

$('#listOfH3s li').click(function() {
    window.location = 'mypage.aspx?h3=' + $(this).find('h3').text();
});

Destination page:

<input type="text" id="h3" />

$('#h3').val(getUrlParam('h3'));
Community
  • 1
  • 1
Terry
  • 14,099
  • 9
  • 56
  • 84