JavaScript and sticky page

Hello

I am new to JavaScript and need some assistance.

I am trying to create a page/form
where the user enters first and last name and select their title (Mr, Miss or Mrs).

There is also the ability to select 1 of 2 options by clicking a link.

After the name, title and option is selected the data is sent to the database using a submit button

The problem is when the link is selected and page refreshes and all the data is lost because the sticky page is not working.



<?
/**
   page: page_list_form.php
**/

include("../reqin/title_in.php");//title drop down

$option = $_REQUEST['u_source_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- <!DOCTYPE html PUBLIC "-//W3C/DTD//xhtml 1.0 transitional//EN"
  "http://www.w3.org/tr/xthtml1-transitional.dtd"> -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 6 November 2007), see www.w3.org" />
<title></title>

<script type="text/javascript">
//<![CDATA[

   function OnClick()
    {
      var title = document.getElementById('TitleId').value;

      calForm = document.getElementById("calendar");
      calForm.setAttribute("action", "<?=$_SERVER['PHP_SELF']?>?u_title="+title);

      calForm.submit(); // submit it, keeping POST vars

    }


//]]>
</script>

</head>
<body>
<form id="calendar" action="process.php>" method="post">

<?

 echo"<table><tr>
      <td width='30%'>
         <input type='text' name='first_name' value='$first_name' width='95%'/></td>";

 echo"<td width='30%'>
        <input type='text' name='last_name' value='$last_name' width='95%'/></td>

      <td width='30%'>";
           title($code);//dropdown with titles
 echo" 
  </td>
  <td>";

      /**display option selection**/
      if($option == "p1")
          {
            echo"Option One";
          }
         elseif($option == "p2")
          {
            echo"Option Two";
          }

 echo"<input type='hidden' name='option' value='$option'/>";//option value passed to database

 echo"</td></tr>
 <tr>";

 /**link to select and refresh page and keep title, first & last name **/
 echo"
     <td><div id='blue-listing'><a href ='../page_list_form.php?u_source_id=p1' onclick='javascript:OnClick();'>Option 1</a></div></td>
     <td><div id='blue-listing'><a href ='../page_list_form.php?u_source_id=p2' onclick='javascript:OnClick();'>Option 2</a></div></td>
     <td>&nbsp;</td>";

 /** submit data to database**/
 echo"<td><input type='submit' name='Submit'/></td>

  </tr></table>";

?>

</form>
</body>
</html>

Hello

What I have posted is a simplified version of the real page.
The top part of the form collects user identification and contact information.

The lower part of the page has a dynamic linked list derived from the database. The user is able to select info from a list to populate the upper part of the form.

The page will refresh when the user selects a link to display their selection.
The page information is stored in the database when the Submit button is selected.

Thanks for the response. I will try your last suggestion.

$_REQUEST[‘u_source_id’] is assigned to $option
$option is assigned to a hidden form element called “option”

Do you spot the issue when that form is submitted?

Thanks for the response.

I am already having difficulty with Javascript so Ajax is out.
Can you give an example of what you mean?

if you’re wanting the page to retain information that has not yet been submitted to the server, you will need to do it in a different way.

One possible solution is to override the onclick event of the link, so that you can submit the form to the server with additional hidden information that specifies which link was clicked.

Another solution is to use Ajax to retrieve additional information from the server so that you can update parts of the page without needing to reload the entire page.

Hello

Your suggestion has provide some results.

When I select the link the sticky form works and the values are not lost.
The url display the value for the selected option. The is problem I am now having is with the


$option = $_REQUEST['u_source_id']; 

it is not receiving the url value. The php if statement is not displaying the information related to the user selection.


<?
/**
   page: page_list_form.php
**/

include("../reqin/title_in.php");//title drop down

$option = $_REQUEST['u_source_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- <!DOCTYPE html PUBLIC "-//W3C/DTD//xhtml 1.0 transitional//EN"
  "http://www.w3.org/tr/xthtml1-transitional.dtd"> -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 6 November 2007), see www.w3.org" />
<title></title>

<script type="text/javascript">
//<![CDATA[

 function()
    {
    	document.getElementById('op_link').onclick = function()
    	  {
    	  	var form;
    	  	form = document.getElementById('calendar');
    	  	form.elements.link = this.href;
    	  	form.submit();
    	  };
    }

//]]>
</script>

</head>
<body>
<form id="calendar" action="process.php>" method="post">

<?

 echo"<table><tr>
      <td width='30%'>
         <input type='text' name='first_name' value='$first_name' width='95%'/></td>";

 echo"<td width='30%'>
        <input type='text' name='last_name' value='$last_name' width='95%'/></td>
      <td width='30%'>";
           title($code);//dropdown with titles
echo" </td>
  <td>";

      /**display option selection**/
      if($option == "p1")
          {
            echo"Option One";
          }
         elseif($option == "p2")
          {
            echo"Option Two";
          }

 echo"<input type='hidden' name='option' value='$option'/>";//option value passed to database

 echo"</td></tr>
 <tr>";

 /**link to select and refresh page and keep title, first & last name **/

 /** submit data to database**/
 echo"<td><input type='submit' name='Submit'/></td>

  </tr></table>";

?>

</form>
</body>
</html>

The PHP forum isn’t really the place for this part of the discussion, but here’s the basic details.

A form:


<form id="aform" method="post">
    <p><label>A label: <input name="aname" value="a value"></label></p>
    <p>
        <input type="hidden" name="link">
        <input type="submit" name="submit" value="Submit">
    </p>
</form>

A link:


<a id="alink" href="#ImALink">A link</a>

Click link to attach link info to form, and submit the form


document.getElementById('alink').onclick = function () {
    // the this keyword is a reference to the clicked-on link
    var form;
    form = document.getElementById('aform');
    form.elements.link = this.href;
    form.submit();
};

Will when the link is clicked, result in the page being submitted. The value in $_POST[‘link’] will be “#ImALink

However, you do need to consider that javascript may not be available to all of your users, so you need to ensure that the form can continue to work properly without javascript. This means that saving the values is an added-on enhancement due to javascript being available.

There are other ways to save the values when no javascript is available. Instead of links you can use different submit buttons, so that the submitted form can tell which button was selected. The server can then take whichever action is appropriate. In this situation when javascript is available, it can replace those buttons with links (to enhance the presentation) and attach on similar onclick behaviour as shown above.

Hello

Thanks for responding again.

I don’t think the option is the problem. It is my fault you have that impression. I apologize, I did not post all the code. The links were missing:



 /**link to select and refresh page and keep title, first & last name **/
 echo"
     <td><div id='blue-listing'><a href ='../page_list_form.php?u_source_id=p1' onclick='javascript:OnClick();'>Option 1</a></div></td>
     <td><div id='blue-listing'><a href ='../page_list_form.php?u_source_id=p2' onclick='javascript:OnClick();'>Option 2</a></div></td>
     <td>&nbsp;</td>";


]

These links pass the u_source_id values via the URL. Then the


$option = $_REQUEST['u_source_id'];

picks up the URL u_source_id values and passes it to the


 echo"<input type='hidden' name='option' value='$option'/>";//option value passed to database

for submission to the database.

Okay, et us simplify the issue.

With the following code:


<a href ='../page_list_form.php?u_source_id=p1' onclick='javascript:OnClick();'>Option 1</a>

What do you intend for the code to do?
Do you intend for a new page to be loaded, or for a function called OnClick to be executed? What is that function supposed to do?

Hi

I will try your suggestion.

I have fought with this for the last two weeks before posting.

thanks