olly79 Posted January 31, 2009 Share Posted January 31, 2009 Hi all, Just wondering if someone can help me with the following: I have a web form as per the following code: <form id="form1" action="" enctype="multipart/form-data"> <div class="row-box indent-top form"> <div class="row-box-1"> Enter Your Name: <div class="indent-form"><input type="text" /></div> Enter Your Phone: <div class="indent-form"><input type="text" /></div> Enter Your E-mail: <div class="indent-form"><input type="text" /></div> Enter Your State: <div class="indent-form"><input type="text" /></div> </div> <div class="row-box-2"> Enter Your Message: <textarea cols="2" rows="2"></textarea> <a href="#" class="link-1 form-link" onclick="document.getElementById('form1').reset()">Reset</a><a href="#" class="link-1" onclick="document.getElementById('form1').submit()">Submit</a> </div> </div> </form> And I now wish to make this dynamic; however, the above form is interlinked with a CSS style sheet: /*================== index-5.html ==================*/ #page6 .form{ font-weight:bold;} #page6 .form input{ height:18px; width:286px;} #page6 .form textarea{ overflow:auto; display:block; height:158px; width:246px; margin-bottom:15px;} #page6 .form textarea, #page6 .form input{ border:1px solid #a09b96; color:#a0b1b4; font-weight:bold; padding-left:3px; font-size:1em;} #page6 .form .form-link{ margin-left:94px; margin-right:41px;} #page6 .indent-form{ height:30px;} I have the following code for a form, which also performs validation on the fileds themselves that I would like to add to my code in order to make it work: <form name="contact" method="post" action="contactprocess.php"> <strong>Name:</strong><br/> <input type="text" name="ename" size="30"><br/> <strong>Email:</strong><br/> <input type="text" name="eemail" size="30"><br/> <strong>Subject:</strong><br/> <input type="text" name="esubject" size="30"><br/> <strong>Message:</strong><br/> <textarea name="emessage" cols="30" rows="10"></textarea><br/> <input type="submit" name="esubmit" value="Send Mail" style="cursor:pointer"> <input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>"> </form> And the PHP: <?php //Some variables $mymail = "[email protected]"; $ename = $_POST['ename']; $eemail = $_POST['eemail']; $esubject = $_POST['esubject']; $emessage = $_POST['emessage']; $eip = $_POST['eip']; //Function to check email address function checkemail($eemail) { if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$eemail)) { return true; } else { return false; } } //Mail Processing if ($_POST['esubmit']) { //Check for blank fields if ($ename == "" || $eemail == "" || $esubject == "" || $emessage == "") { echo "<p>It appears that you left a blank field.<br/> Please make sure you fill everything in.</p>"; } //Check to see if the email address is valid else if (checkemail($eemail) == false) { echo "<p>It appears that you enter an invalid email address.<br/> Please check your email again.</p>"; } //Send the email if there's no error else { $body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip"; mail($mymail,$esubject,$body,"From: $eemail\n"); echo "<p>Thank you for your email $ename!</p>"; } } ?> Can anyone please guide me on how I can make my form work based on the above code? Many thanks Link to comment https://forums.phpfreaks.com/topic/143244-sending-web-form/ Share on other sites More sharing options...
chronister Posted January 31, 2009 Share Posted January 31, 2009 My first suggestion, would be to get rid of the javascript in the buttons. There is nothing more annoying than having a form use javascript to trigger the submit and not work because javascript is off. I feel very strongly about not relying on client side scripting to do core functionality of the form. Yes, most people have javascript turned on, but some don't. I disable it sometimes. So if a person has it turned off, purposely or unknowingly, then your form does not work any longer. On to your question... Your form elements are missing name attributes, which are needed to receive the variables on the processing side. For each item in your form, you should have a piece like this to receive the data. $name = $_POST['name']; $state = $_POST['state']; etc.... The processing code you have looks pretty standard. I suggest you learn what it is doing and apply that to your situation. In PHP, form processing is one of the most common tasks. You should learn valid HTML, and ensure that your HTML form elements have all the attributes you need. The PHP, is pretty standard... here is explanation of what your processing script is doing. <?php //Some variables /* SET ALL THE FORM FIELDS IN EASY TO USE VARIABLES SO THEY CAN BE REFERENCED BY EASY NAMES ADD OR REMOVE ANY FIELDS YOU NEED TO .... INSIDE THE $_POST[] IS THE NAME OF YOUR FIELD. */ $mymail = "[email protected]"; $ename = $_POST['ename']; $eemail = $_POST['eemail']; $esubject = $_POST['esubject']; $emessage = $_POST['emessage']; $eip = $_POST['eip']; //Function to check email address /* THIS CHECKS THE EMAIL ADDRESS TO ENSURE IT IS VALID. YOU PASS AN EMAIL ADDRESS INTO IT BELOW */ function checkemail($eemail) { if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$eemail)) { return true; } else { return false; } } //Mail Processing if ($_POST['esubmit']) { //Check for blank fields /* THIS IS A CHECK TO MAKE SURE NO FIELDS ARE BLANK. THE || MEANS OR...... IF ENAME EQUALS BLANK OR EEMAIL EQUALS BLANK OR ....ETC.. ADD OR REMOVE THE CORRECT VARIABLES THAT WERE SET ABOVE */ if ($ename == "" || $eemail == "" || $esubject == "" || $emessage == "") { echo "<p>It appears that you left a blank field.<br/> Please make sure you fill everything in.</p>"; } //Check to see if the email address is valid /* PART OF THE IF STATEMENT ABOVE. THIS IS WHERE YOU WOULD PASS YOUR EMAIL VARIABLE YOU SET ABOVE TO CHECK IF IT IS VALID */ else if (checkemail($eemail) == false) { echo "<p>It appears that you enter an invalid email address.<br/> Please check your email again.</p>"; } //Send the email if there's no error /* IF YOU GET TO THIS POINT, THE FIELDS ARE NOT EMPTY AND THE EMAIL IS CORRECT FORMAT... SEND MAIL.*/ else { $body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip"; mail($mymail,$esubject,$body,"From: $eemail\n"); echo "<p>Thank you for your email $ename!</p>"; } } ?> This is an overview of the script. I suggest you study what it is doing and learn the concepts of how it is doing it. Check out the php manual at http://www.php.net/manual/en/funcref.php to see what things do. You will be worlds ahead if you learn how to do this stuff yourself rather than trying to "hack" together scripts. Sorry if this is not what your looking for, but hope it helps. Nate Link to comment https://forums.phpfreaks.com/topic/143244-sending-web-form/#findComment-751379 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.