hashir Posted October 11, 2016 Share Posted October 11, 2016 I tried to create the inquiry form fronted on html and backhand on php. the purpose of the form use can fill their personal details and attach their image after submit the all data with picture will be email to admin. i wrote this code but fail to execute successfully. <?php//errors$phone = "test number"; // check line 161$_POST['title'] =""; // somehow its turning into an arrayif(!$_POST) exit;// Email address verification, do not edit.function isEmail($email) {return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));}if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");//$file_name = $_FILES['file_name'];$file_name =$_FILES["file_name"]["tmp_name"];$file_data = base64_encode( file_get_contents($file_name));$title = $_POST['title'];$gender = $_POST['gender'];$maritalStatus = $_POST['maritalStatus'];$name = $_POST['name'];$fatherName = $_POST['fatherName'];$dob = $_POST['dob'];$email = $_POST['email'];$postalAddress = $_POST['postalAddress'];$PTCLno = $_POST['PTCLno'];$mobileNumber1 = $_POST['mobileNumber1'];$matricBoard = $_POST['matricBoard'];$intermediateBoard = $_POST['intermediateBoard'];$group = $_POST['group'];$totalMarksMatric = $_POST['totalMarksMatric'];$marksObtnMatric = $_POST['marksObtnMatric'];$examTypeMatric = $_POST['examTypeMatric'];$passingYear = $_POST['passingYear'];$percentageMatric = $_POST['percentageMatric'];$nameOfCollege = $_POST['nameOfCollege'];$collegeEmailAddress = $_POST['collegeEmailAddress'];$postelAddressOfCollege = $_POST['postelAddressOfCollege'];$telephoneNumberOfCollege = $_POST['telephoneNumberOfCollege'];$collegePrincipleName = $_POST['collegePrincipleName'];$collegePrincipleCellNo = $_POST['collegePrincipleCellNo'];$referenceName = $_POST['referenceName'];$reference1InstituteName = $_POST['reference1InstituteName'];$collegePrincipleCellNo = $_POST['reference1InstituteName'];$reference1CellNo = $_POST['reference1CellNo'];$reference1Address = $_POST['reference1Address'];$reference2InstituteName = $_POST['reference2InstituteName'];$reference2CellNo = $_POST['reference2CellNo'];$reference2Address = $_POST['reference2Address'];$question1 = $_POST['question1'];$question2 = $_POST['question2'];$question3 = $_POST['question3'];$question4 = $_POST['question4'];$question5 = $_POST['question5'];$accountTitle = $_POST['accountTitle'];$accountNo = $_POST['accountNo'];$nameOfBank = $_POST['nameOfBank'];$branchAddress = $_POST['branchAddress'];$CNICofAccHolder = $_POST['CNICofAccHolder'];$cellNoOfAccHolder = $_POST['cellNoOfAccHolder'];$examCity = $_POST['examCity'];$terms = $_POST['terms'];if(trim($name) == '') {echo '<div class="alert alert-error">You must enter your name.</div>';exit();} else if(trim($email) == '') {echo '<div class="alert alert-error">You must enter email address.</div>';exit();} else if(!isEmail($email)) {echo '<div class="alert alert-error">You must enter a valid email address.</div>';exit();}if(get_magic_quotes_gpc()) {$comments = stripslashes($comments);}// Configuration option.// Enter the email address that you want to emails to be sent to.// Example $address = "joe.doe@yourdomain.com";//$address = "hashir_252@hotmail.com";$address = "hashir_252@hotmail.com";// Configuration option.// i.e. The standard subject will appear as, "You've been contacted by John Doe."// Example, $e_subject = '$name . ' has contacted you via Your Website.';$e_subject = 'Contact Form';// Configuration option.// You can change this if you feel that you need to.// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.$e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;$e_content = "\"Picture: ".'<img src="data:image/png;base64,'.$file_data.'" alt="beastie.png">'." \nPersonal Information \nTitle: $title \nGender: $gender \nMartial Status: $maritalStatus \nName: $name \nFather Name: $fatherName \nDate of Birth: $dob \nEmail ID: $email \nPostal Address: $postalAddress \nPtcl: $PTCLno \nMobile Number: $mobileNumber1 \nAcademic Information \nMatric Board: $matricBoard \nIntermediate Board: $intermediateBoard \nGroup: $group \nTotal Marks Matric: $totalMarksMatric \nMarks Obtain: $marksObtnMatric \nExam Type: $examTypeMatric \nPassing Year: $passingYear \nPercentage Matric: $percentageMatric \nCollege Information \nName of College: $nameOfCollege \nCollege Email Address: $collegeEmailAddress \nPostal Address College: $postelAddressOfCollege \nTelephone Number: $telephoneNumberOfCollege \nPrinciple Name: $collegePrincipleName \nPrinciple Cell Number: $collegePrincipleCellNo \nReferences \nRefrence Name: $referenceName \nRefrence Institute Name: $reference1InstituteName \nRefrence Cell Number: $reference1CellNo \nRefrence Address: $reference1Address \nRefrence 2 Name: $reference2InstituteName \nRefrence Cell Number: $reference2CellNo \nRefrence Address: $reference2Address \nQuestions and Answers \nQuestion1: $question1 \nQuestion2: $question2 \nQuestion3: $question3 \nQuestion4: $question4 \nQuestion5: $question5 \nBanking Information \nAccount Title: $accountTitle \nAccount Number: $accountNo \nBank Name: $nameOfBank \nBranch Address: $branchAddress \nCnic#: $CNICofAccHolder \nCell Number: $cellNoOfAccHolder \nSelect the city where you would like to take the exam \nExam City: $examCity \nTerms: $terms\"" . PHP_EOL . PHP_EOL;$e_reply = "You can contact $name via email: $email or via phone: $phone";$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );$headers = "From: $email" . PHP_EOL;$headers .= "Reply-To: $email" . PHP_EOL;$headers .= "MIME-Version: 1.0" . PHP_EOL;$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;// //////////////////////////////////////////////////// REMOVE THESE LINES$msg="<html><head></head><body>".$msg."</body></html>";echo $msg;exit();///////////////////////////////////////////////////// REMOVE THESE LINESif(mail($address, $e_subject, $msg, $headers)) {// Email has sent successfully, echo a success page.echo "<div class='alert alert-success'>";echo "<h3>Email Sent Successfully.</h3><br>";echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";echo "</div>";} else {echo 'ERROR!';} Quote Link to comment https://forums.phpfreaks.com/topic/302317-create-php-inquiry-form-with-file-upload-and-send-to-email/ Share on other sites More sharing options...
benanamen Posted October 11, 2016 Share Posted October 11, 2016 I will just say, you have at least fifty percent more code than you need. Quote Link to comment https://forums.phpfreaks.com/topic/302317-create-php-inquiry-form-with-file-upload-and-send-to-email/#findComment-1538198 Share on other sites More sharing options...
hashir Posted October 11, 2016 Author Share Posted October 11, 2016 I will just say, you have at least fifty percent more code than you need. can you help me to customize my code Quote Link to comment https://forums.phpfreaks.com/topic/302317-create-php-inquiry-form-with-file-upload-and-send-to-email/#findComment-1538199 Share on other sites More sharing options...
Destramic Posted October 11, 2016 Share Posted October 11, 2016 i'd suggest displaying errors you should be using this code and put it at the top of you php while developing ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); what errors are you getting? ps. please use the code tags so your code is more visible Quote Link to comment https://forums.phpfreaks.com/topic/302317-create-php-inquiry-form-with-file-upload-and-send-to-email/#findComment-1538202 Share on other sites More sharing options...
mac_gyver Posted October 11, 2016 Share Posted October 11, 2016 if you are just learning, start with one or two form fields and get the code to work so that you are learning and understanding how the code works. if you are doing this for real and you have more than about three form fields, you should NOT write out lines of code for each possible form field. you would instead dynamically process the form data, by defining the expected fields in a data structure (array or database table.) you would then loop over the defining data structure and dynamically reference the data, to first validate it, then dynamically produce the email message body, all without writing out line after line of code that only differs in the form field it corresponds to. next, any data you put into the email message body needs to be passed through htmlentities() so that any html/javascript/css in the submitted data won't be operated on by the receiving email client. any data you put into the email header must be validated that it is exactly and only an expected value. lastly, the email is not being sent FROM the email address that someone entered in the form. the email is being sent from the mail server at your web host. the From: email address should be a valid email address at your web host, so that the From: email address corresponds to the sending mail server and so that bounce messages from the receiving mail server have a place to go to. Quote Link to comment https://forums.phpfreaks.com/topic/302317-create-php-inquiry-form-with-file-upload-and-send-to-email/#findComment-1538203 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.