mathewballard Posted February 23, 2010 Share Posted February 23, 2010 I have this form almost complete. But, I can't seem to figure out how to move "Sorry there was an error processing your request" from the top of the page into the body. Also, I am trying to figure out how to put a red "*" by each field that isn't filled out correctly. And one last thing, how do you go about limiting a field to just numbers? Sorry, but I am pretty new to PHP. I've been strictly a designer for a few years now and I'm trying to expand my skills into coding other then HTML/CSS and some Javascript. Below is a code, but you can also visit the page -> http://mathewballard.com/devriesdental/appointmentform.php. <?php if ( isset($_POST['submit']) ) { $error = ''; if ( empty($_POST['name']) ) { $error .= "Error: Please enter your name<br />\n"; } if ( empty($_POST['phonenumber']) ) { $error .= "Error: Please enter your phone number<br />\n"; } if ( empty($_POST['appointmentdate']) ) { $error .= "Error: Please enter a date<br />\n"; } if ( empty($_POST['appointmenttime']) ) { $error .= "Error: Please enter an appointment time<br />\n"; } if ( empty($_POST['appointmenttype']) ) { $error .= "Error: Please enter an appointment type<br />\n"; } if ( !$error ) { $to = "[email protected]"; $subject = "Someone Has Requested An Appointment!"; $name_field = $_POST['name']; $phonenumber_field = $_POST['phonenumber']; $appointmentdate_field = $_POST['appointmentdate']; $appointmenttime_field = $_POST['appointmenttime']; $appointmenttype_field = $_POST['appointmenttype']; $body = "From: $name_field\n Phone Number: $phonenumber_field\n Requested Appointment Date:\n $appointmentdate_field\n Requested Appointment Time:\n $appointmenttime_field\n Requested Appointment Type:\n $appointmenttype_field"; if (mail($to, $subject, $body)) { header("location: http://www.mathewballard.com/devriesdental/thankyou.html"); } } else { echo "Sorry, there was an error processing your request."; } } ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>DeVries Family Dentistry</title> <meta name="description" content="Welcome to DeVries Family Dentistry! Your hometown family dentistry in Brandenbrug, KY." /> <meta name="keywords" content="dentist, dentistry, family dentistry, Brandenburg, KY, Kentucky, Meade County, Ft. Knox" /> <meta http-equiv="imagetoolbar" content="no" /> <meta name="MSSmartTagsPreventParsing" content="true" /> <meta name="author" content="Mathew Ballard" /> <link href="master.css" rel="stylesheet" type="text/css" /> <link type="image/x-icon" href="favicon.ico" rel="icon" /> <link type="image/x-icon" href="favicon.ico" rel="shortcut icon" /> <script type="text/javascript"> <!-- function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } //--> </script> </head> <body onload="MM_preloadImages('images/btns/navhover_btn.png','images/btns/navhover_btn-02.png','images/btns/navhover_btn-03.png','images/btns/navhover_btn-04.png','images/btns/navhover_btn-05.png','images/btns/navhover_btn-06.png','images/btns/navhover_btn-07.png')"> <div id="pagecontainer" align="center"> <div id="header"> <img src="images/header.png" alt="DeVries Family Dentistry" /></div> <div id="nav"> <a href="index2.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','images/btns/navhover_btn.png',1)"><img src="images/btns/nav_btn.png" alt="home" name="home" border="0" id="home" /></a><a href="about.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('About','','images/btns/navhover_btn-02.png',1)"><img src="images/btns/nav_btn-02.png" alt="About" name="About" border="0" id="About" /></a><a href="procedures.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('services','','images/btns/navhover_btn-03.png',1)"><img src="images/btns/nav_btn-03.png" alt="Procedures/Services" name="services" width="186" height="25" border="0" id="services" /></a><a href="smilegallery.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('smilegallery','','images/btns/navhover_btn-04.png',1)"><img src="images/btns/nav_btn-04.png" alt="Smile Gallery" name="smilegallery" border="0" id="smilegallery" /></a><a href="patientforms.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('patientforms','','images/btns/navhover_btn-05.png',1)"><img src="images/btns/nav_btn-05.png" alt="Patient Forms" name="patientforms" width="138" height="25" border="0" id="patientforms" /></a><a href="financingoptions.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('finacingoptions','','images/btns/navhover_btn-06.png',1)"><img src="images/btns/nav_btn-06.png" alt="Financing Options" name="finacingoptions" border="0" id="finacingoptions" /></a><a href="contactus.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('contactus','','images/btns/navhover_btn-07.png',1)"><img src="images/btns/nav_btn-07.png" alt="Contact Us" name="contactus" border="0" id="contactus" /></a></div> <div id="content2" align="left"> <?php if ( !empty($error) ) { ?> <p class="error"><?php echo $error;?></p> <?php } ?> <form method="post" action="#" name="apptform"> <label for="name"><b>Name</b></label><br /> <input type="text" name="name" size="20" value="<?php echo $_POST['name'];?>" /><br /><br /> <label for="phonenumber"><b>Phone Number</b></label> (Starting with areacode)<br /> <input type="text" name="phonenumber" size="20" value="<?php echo $_POST['phonenumber'];?>" /><br /><br /> <label for="apptdate"><b>Appointment Date</b></label><br /> <input type="text" name="appointmentdate" size="20" value="<?php echo $_POST['appointmentdate'];?>" /><br /><br /> <label for="appttime"><b>Appointment Time</b></label><br /> <input type="text" name="appointmenttime" size="20" value="<?php echo $_POST['appointmenttime'];?>" /><br /><br /> <label for="appttype"><b>Apointment Type</b></label> (i.e. Annual Checkup, drilling, etc.)<br /> <textarea rows="10" name="appointmenttype" cols="30" ><?php echo $_POST['appointmenttype'];?></textarea><br /><br /> <input type="submit" value="submit" name="submit" /><br /> </form> </div> <div id="footer"> <img src="images/bottom.png" /><br /> DeVries Family Dentistry ©2009 </div> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/193092-need-help-putting-finishing-touches-on-form/ Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Share Posted February 23, 2010 Where exactly do you want the error? In order to add the red stars then you will have to do a check to see if the POST variable was blank and echo a red * next to it. <input type="number"> Link to comment https://forums.phpfreaks.com/topic/193092-need-help-putting-finishing-touches-on-form/#findComment-1016889 Share on other sites More sharing options...
mathewballard Posted February 23, 2010 Author Share Posted February 23, 2010 I want it to be above the listed errors in the body area. Could you elaborate on how to do that? Thanks. Where exactly do you want the error? In order to add the red stars then you will have to do a check to see if the POST variable was blank and echo a red * next to it. <input type="number"> Link to comment https://forums.phpfreaks.com/topic/193092-need-help-putting-finishing-touches-on-form/#findComment-1016890 Share on other sites More sharing options...
mikesta707 Posted February 23, 2010 Share Posted February 23, 2010 echo the errors inside the body tag? <body> if (isset($_POST['submit']) && !empty($errors)){ echo $errors; } as for the stars, there are multiple ways you could do it. a simple way would be to create a star variable for each item in the form, and at the beginning of the script, set them to an empty string and echo them next to each individual input, IE $star_name = ""; .... etc ... html and stuff <?php echo $star_name ?><input name="name" type="text" /> when you find an error, you can just set the individual star variables to a star or whatever. You can use CSS to make it red if you want to limit a field to just numeric characters, you can use the function is_numeric() to test. example if (is_numeric($string)){ echo "its a string of numbers"; } else { echo "There are some letters or non numeric characters in that string"; } Link to comment https://forums.phpfreaks.com/topic/193092-need-help-putting-finishing-touches-on-form/#findComment-1016893 Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Share Posted February 23, 2010 I want it to be above the listed errors in the body area. Could you elaborate on how to do that? Thanks. Where exactly do you want the error? In order to add the red stars then you will have to do a check to see if the POST variable was blank and echo a red * next to it. <input type="number"> The way that I handle my errors is I store each of them in an array. So create an array called error. Then have a second variable that acts as a flag. Start your flag off being 0. Anytime you get even 1 error set it to 1. Your code would look like this $error = array(); $errflag = 0; if(empty($variable)) { $error[] = "Error message" $errflag = 1; } Then once you get down to where it's printing out just use a foreach loop to print out each error message. You must of course put this loop in a if to see if the error flag has been set. Inside of that if but above the loop you can put your message that you want in the body Something like this if($errflag) { echo "You got some damn errors"; foreach($error as $err){ echo $err; } } Link to comment https://forums.phpfreaks.com/topic/193092-need-help-putting-finishing-touches-on-form/#findComment-1016897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.