Tell Javascript What Form Has Called it in a PHP file

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
waqarchaudry
Forum Newbie
Posts: 4
Joined: Wed Jul 12, 2006 7:20 pm

Tell Javascript What Form Has Called it in a PHP file

Post by waqarchaudry »

I have the following code that preserves the location of the scroll bar after a submit... Now here is the problem, I am not very good at javascript to be honest! :( I just want the "function saveScrollCoordinates()" to know which form has called it! what can I put in document.form1.scrolly.value instead of form1 some sort of varaible passed through parameters etc or whatever way... if you run the code you will notice that only the first submit button works but not the second.... Summing it up: "Just need to know how to tell the document.form1.scrolly.value" which form has called it instead of hard wiring the form name.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Scroll</title>
<script language=javascript>

function saveScrollCoordinates() {
document.form1.scrolly.value = (document.all)?document.body.scrollTop:window.pageYOffset;
}
</script>
</head>
<body onLoad="javascript:window.scrollTo(0,[php]<?php echo $_POST['scrolly']; ?>[/php])">

<form name="saveCoords">
</form>
<form name="form1" id="form1" onSubmit='saveScrollCoordinates()' method="POST">
  <p>&nbsp;  </p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>
    <input type="hidden" name="scrolly" value="">
    <input name="" type="submit" value="Submit" />
              </p>
</form> 
<form name="form2" id="form2" onSubmit='saveScrollCoordinates()' method="post">
  <p>&nbsp;  </p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>
    <input type="hidden" name="scrolly" value="">
    <input name="" type="submit" value="Submit" />
                  </p>
</form> 

</body></html>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

function saveScrollCoordinates(form) {
   form.scrolly.value = (document.all)?document.body.scrollTop:window.pageYOffset;
} 

Code: Select all

<form name="form2" id="form2" onSubmit='saveScrollCoordinates(this);' method="post">
....
</form>
waqarchaudry
Forum Newbie
Posts: 4
Joined: Wed Jul 12, 2006 7:20 pm

Post by waqarchaudry »

Cheers! Thanks Man, really appreciate that!
Post Reply