Jump to content

[SOLVED] When form fails to submit the browser jumps to top of page (?)


webmaster1

Recommended Posts

Hi All,

 

I have a form that displays an error message below particular fields if they are empty.

 

My problem is that the browser jumps to the top of my page .The form is at the bottom and therefore now out of view (even though the error message is showing).

 

I've seen anchor tags used with regular mark-up to jump to a specific part of a given page.

 

Any ideas on how to do this with php? ???

 

<?php

if (isset($_POST['submit']))
{ 

     if (strlen($_POST['name']) > 0)
  {$name=TRUE;}
     else {$name=FALSE;
          $message_name=" *Please complete this field!";}

     if (strlen($_POST['contact']) > 0)
  {$contact=TRUE;}
     else {$contact=FALSE;
          $message_contact=" *Please complete this field!";}


if 
        ($name && $contact)
     {                       
                   include("dbinfo.php");
           mysql_connect(localhost,$username,$password);
           @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

                   $name = mysql_real_escape_string($_POST['name']);
                   $contact = mysql_real_escape_string($_POST['contact']);
                   $message = mysql_real_escape_string($_POST['message']);
                   $ipaddress = getenv('REMOTE_ADDR');

                   $query = "INSERT INTO eventcontact VALUES ('','$name','$contact','$message','$ipaddress',NOW())";
           mysql_query($query);

                   echo '</br></br>
                   *Your message has been sent. We will be in touch shortly. Thank you for your interest.</br></br>
                   <a href="index.php">Send another message</a>
                   '; 

                   
                   exit();
              }

}                            
?>

Link to comment
Share on other sites

Eep, no takers yet?  :o

 

Heres the form:

 


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<fieldset>

<ol>

    <li>
    <label for="name">Contacts Name:</label>
    <input type="text" name="name" id="name" class="textbox" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
    <?php if ($message_name) echo ''.$message_name.''; ?>
    </br>
    </li>

    <li>
    <label for="contact">Email / Phone:</label>
    <input type="text" name="contact" id="contact" class="textbox" value="<?php if (isset($_POST['contact'])) echo $_POST['contact']; ?>"/>
    <?php if ($message_contact) echo ''.$message_contact.''; ?>
    </br>
    </li>

    <li>
    <label for="message">Your Message:</label>
    <textarea rows="3" cols="70" class="textarea" name="message" id="message"><?php if (isset($_POST['message'])) echo stripslashes($_POST['message']); ?></textarea>
    </li>

    <li>
    <input type="submit" name="submit" id="submit" class="submit" value="Submit Message" onmouseover="this.className='submitnhov'" onmouseout="this.className='submit'">
    </li>

</ol>
</fieldset>
</form>	



Link to comment
Share on other sites

I've tried a little javascript but it seems to have no effect:

 

<?php
echo '
           <script type="text/javascript">
           document.getElementById("name").focus();
           </script>   
           '; 
?>

 

The JS error reads  document.getElementById is null or not an object.  :(

Link to comment
Share on other sites

I've also tried this with no luck:

 


<html>
     <head>
          <script type="text/javascript">
               function jump()
                    {
                         document.getElementById("name").focus();
                    }
          </script> 
     </head>
     <body>
          <input type ="submit" onclick = "jump()">
          <input type="text" id="name">
     </body>
</html>

 

It works by itself but not when integrated into my php page. No JS error this time.

Link to comment
Share on other sites

I've just tried replacing

 

document.getElementById("name").focus();

 

with

 

document.contactform.name.focus();

 

but no luck either.

 

I'm sure the problem is related to where I position the basic javascript. I'll be your best freind if you can help  ;D

Link to comment
Share on other sites

Here's the fill script without the JS focus:

 


<?php

if (isset($_POST['submit']))
{ 


     if (strlen($_POST['name']) > 0)
  {$name=TRUE;}
     else {$name=FALSE;
          $message_name=" *Please complete this field!";


}

     if (strlen($_POST['contact']) > 0)
  {$contact=TRUE;}
     else {$contact=FALSE;
          $message_contact=" *Please complete this field!";}


if 
        ($name && $contact)
     {                       
                   include("dbinfo.php");
           mysql_connect(localhost,$username,$password);
           @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

                   $name = mysql_real_escape_string($_POST['name']);
                   $contact = mysql_real_escape_string($_POST['contact']);
                   $message = mysql_real_escape_string($_POST['message']);
                   $ipaddress = getenv('REMOTE_ADDR');

                   $query = "INSERT INTO eventcontact VALUES ('','$name','$contact','$message','$ipaddress',NOW())";
           mysql_query($query);

                   echo '</br></br>
                   *Your message has been sent. We will be in touch shortly. Thank you for your interest.</br></br>
                   <a href="index.php">Send another message</a>?>
                   '; 

                   
                   exit();
              }

}


                                     

?>
<!------------------------------------------------------------------>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Joyce Promotions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="jprevised.css" rel="stylesheet" type="text/css">
</head>

<body TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">

<table border="0" cellpadding="0" cellspacing="0">
     <tr>
          <td rowspan="4" width="166" height="1200" class="leftbar"></td>
          <td width="631" height="67" class="nav">
	  
	  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="631" height="67" id="nav" align="middle">
          <param name="allowScriptAccess" value="sameDomain" />
          <param name="movie" value="flash/nav.swf" />
          <param name="quality" value="high" />
          <param name="bgcolor" value="#000000" />
          <embed src="flash/nav.swf" quality="high" bgcolor="#000000" width="631" height="67" name="nav" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
          </object>
	  
	  </td>
     </tr>
     <tr>
          <td width="631" height="309" class="flash">
	  
	  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="631" height="309" id="flash" align="middle">
          <param name="allowScriptAccess" value="sameDomain" />
          <param name="movie" value="flash/flash.swf" />
          <param name="quality" value="high" />
          <param name="bgcolor" value="#000000" />
          <embed src="flash/flash.swf" quality="high" bgcolor="#000000" width="631" height="309" name="flash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
          </object>
	  
	  </td>
    </tr>
    <tr>
          <td width="631" height="520" class="blurb"></td>
   </tr>
   <tr>
          <td width="631" height="304" class="form" valign="top">

<!--------------------------------------------------------------------->


<!--BEGIN FORM AND SET IT TO PROCESS SELF AND HANDLE MULTIPART/FORM-DATA-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<fieldset>

<ol>

    <li>
    <label for="name">Contacts Name:</label>
    <input type="text" name="name" id="name" class="textbox" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
    <?php if ($message_name) echo ''.$message_name.''; ?>
    </br>
    </li>

    <li>
    <label for="contact">Email / Phone:</label>
    <input type="text" name="contact" id="contact" class="textbox" value="<?php if (isset($_POST['contact'])) echo $_POST['contact']; ?>"/>
    <?php if ($message_contact) echo ''.$message_contact.''; ?>
    </br>
    </li>

    <li>
    <label for="message">Your Message:</label>
    <textarea rows="3" cols="70" class="textarea" name="message" id="message"><?php if (isset($_POST['message'])) echo stripslashes($_POST['message']); ?></textarea>
    </li>

    <li>
    <input type="submit" name="submit" id="submit" class="submit" value="Submit Message" onmouseover="this.className='submitnhov'" onmouseout="this.className='submit'">
    </li>

</ol>
</fieldset>
</form>	

<!--------------------------------------------------------------------------------->

  </td>
   </tr>
</table>

</body>
</html>

Link to comment
Share on other sites


<?php

if (isset($_POST['submit']))
{ 


     if (strlen($_POST['name']) > 0)
     {$name=TRUE;}
     else {$name=FALSE;
          $message_name=" *Please complete this field!";


}

     if (strlen($_POST['contact']) > 0)
     {$contact=TRUE;}
     else {$contact=FALSE;
          $message_contact=" *Please complete this field!";}


   if 
        ($name && $contact)
        {                       
                   include("dbinfo.php");
              mysql_connect(localhost,$username,$password);
              @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

                   $name = mysql_real_escape_string($_POST['name']);
                   $contact = mysql_real_escape_string($_POST['contact']);
                   $message = mysql_real_escape_string($_POST['message']);
                   $ipaddress = getenv('REMOTE_ADDR');

                   $query = "INSERT INTO eventcontact VALUES ('','$name','$contact','$message','$ipaddress',NOW())";
              mysql_query($query);

                   echo </br></br>
                   *Your message has been sent. We will be in touch shortly. Thank you for your interest.</br></br>
                   <a href="index.php">Send another message</a>?>
                   ';

                   
                   exit();
              }

}


                                     

?>
<!------------------------------------------------------------------>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Joyce Promotions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="jprevised.css" rel="stylesheet" type="text/css">
</head>

<body TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">

<table border="0" cellpadding="0" cellspacing="0">
     <tr>
          <td rowspan="4" width="166" height="1200" class="leftbar"></td>
          <td width="631" height="67" class="nav">
       
        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="631" height="67" id="nav" align="middle">
          <param name="allowScriptAccess" value="sameDomain" />
          <param name="movie" value="flash/nav.swf" />
          <param name="quality" value="high" />
          <param name="bgcolor" value="#000000" />
          <embed src="flash/nav.swf" quality="high" bgcolor="#000000" width="631" height="67" name="nav" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
          </object>
       
        </td>
     </tr>
     <tr>
          <td width="631" height="309" class="flash">
       
        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="631" height="309" id="flash" align="middle">
          <param name="allowScriptAccess" value="sameDomain" />
          <param name="movie" value="flash/flash.swf" />
          <param name="quality" value="high" />
          <param name="bgcolor" value="#000000" />
          <embed src="flash/flash.swf" quality="high" bgcolor="#000000" width="631" height="309" name="flash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
          </object>
       
        </td>
    </tr>
    <tr>
          <td width="631" height="520" class="blurb"></td>
   </tr>
   <tr>
          <td width="631" height="304" class="form" valign="top">

<!--------------------------------------------------------------------->


<!--BEGIN FORM AND SET IT TO PROCESS SELF AND HANDLE MULTIPART/FORM-DATA-->
<a name="mainform"></a>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>#mainform" method="post" enctype="multipart/form-data">
<fieldset>

<ol>

    <li>
    <label for="name">Contacts Name:</label>
    <input type="text" name="name" id="name" class="textbox" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
    <?php if ($message_name) echo ''.$message_name.''; ?>
    </br>
    </li>

    <li>
    <label for="contact">Email / Phone:</label>
    <input type="text" name="contact" id="contact" class="textbox" value="<?php if (isset($_POST['contact'])) echo $_POST['contact']; ?>"/>
    <?php if ($message_contact) echo ''.$message_contact.''; ?>
    </br>
    </li>

    <li>
    <label for="message">Your Message:</label>
    <textarea rows="3" cols="70" class="textarea" name="message" id="message"><?php if (isset($_POST['message'])) echo stripslashes($_POST['message']); ?></textarea>
    </li>

    <li>
    <input type="submit" name="submit" id="submit" class="submit" value="Submit Message" onmouseover="this.className='submitnhov'" onmouseout="this.className='submit'">
    </li>

</ol>
</fieldset>
</form>   

<!--------------------------------------------------------------------------------->

  </td>
   </tr>
</table>

</body>
</html>


 

Try that

Link to comment
Share on other sites

change your php to this one

 

<?php

if (isset($_POST['submit']))
{ 


     if (strlen($_POST['name']) > 0)
     {$name=TRUE;}
     else {$name=FALSE;
          $message_name=" *Please complete this field!";


}

     if (strlen($_POST['contact']) > 0)
     {$contact=TRUE;}
     else {$contact=FALSE;
          $message_contact=" *Please complete this field!";}


   if 
        ($name && $contact)
        {                       
                   include("dbinfo.php");
              mysql_connect(localhost,$username,$password);
              @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

                   $name = mysql_real_escape_string($_POST['name']);
                   $contact = mysql_real_escape_string($_POST['contact']);
                   $message = mysql_real_escape_string($_POST['message']);
                   $ipaddress = getenv('REMOTE_ADDR');

                   $query = "INSERT INTO eventcontact VALUES ('','$name','$contact','$message','$ipaddress',NOW())";
              mysql_query($query);

                   echo '</br></br>
                   *Your message has been sent. We will be in touch shortly. Thank you for your interest.</br></br>
                   <a href="index.php">Send another message</a>';

                   
                   exit();
              }

}


                                     

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.