webmaster1 Posted December 6, 2008 Share Posted December 6, 2008 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(); } } ?> Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 I've tried adding header("Location: #formanchor"); but the form resets and the error messages no longer shows. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 6, 2008 Share Posted December 6, 2008 can you post your form please? this kind of thing is not a problem .. i've done something similar many times before where users were directed to certain areas of the page after a form submission. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 Eep, no takers yet? 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> Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 Hello? Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 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> Quote Link to comment Share on other sites More sharing options...
gevans Posted December 6, 2008 Share Posted December 6, 2008 <?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 Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 Parse error: syntax error, unexpected '<' in xxx/test.php on line 37 Quote Link to comment Share on other sites More sharing options...
gevans Posted December 6, 2008 Share Posted December 6, 2008 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(); } } ?> Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 Perfect! Did we just add #mainform to the the form action or did I miss something? Quote Link to comment Share on other sites More sharing options...
gevans Posted December 6, 2008 Share Posted December 6, 2008 As well as <a name="mainform"></a> That gets used as a marker for the browser to move to! Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Author Share Posted December 6, 2008 Understood. Thanks so much, I was stuck on this for some time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.