PHP/Javascript not properly moving up and down the folders -
so have simple contact form. in directory there main.html , 2 folders. folder1 contains js code , folder2 contains php file. js file calls php file analyze form data , php file returns js file outputs on main.html.
when js file being called main.html, there no data being sent php file. use in js file
$.post("../folder2/contact_form.php",
however, if place main.html folder 3 , call js file using line works.
<script src="../test/contact_form.js"></script>
basically, when main.html inside folder, code works. if main.html in root folder, php file isn't called when js file accessed. again, code works when main.html inside folder.
main.html
<html> <head> <title>simple jquery contact form validation</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <link rel="stylesheet" href="../css/contact.css" /> <script src="../test/contact_form.js"></script> </head> <body> <div id="mainform"> <form id="form"> <input type="text" id="name" placeholder="name"/><br/> <input type="text" id="email" placeholder="email"/><br/> <input type="text" id="subject" placeholder="subject"/><br/> <textarea id="message" placeholder="message"></textarea><br/> <input type="button" id="submit" value="send"/></td> <p id="returnmessage"></p><br/></td> </form> </div> </body>
contact_form.js
$(document).ready(function(){ $("#submit").click(function(){ var name = $("#name").val(); var email = $("#email").val(); var message = $("#message").val(); var subject = $("#subject").val(); $("#returnmessage").empty(); //to empty previous error/success message. //checking blank fields if(name==''||email==''||subject=='') { alert("please fill required fields"); } else{ // returns successful data submission message when entered information stored in database. $.post("../test2/contact_form.php",{ name1: name, email1: email, message1:message, subject1: subject}, function(data) { $("#returnmessage").append(data);//append returned message message paragraph if(data=="your message sent successfully! thank-you contacting us!"){ $("#form")[0].reset();//to reset form fields on success } }); } }); });
contact_form.php
<?php //fetching values url $name = $_post['name1']; $email = $_post['email1']; $message = $_post['message1']; $subject = $_post['subject1']; //sanitizing email $email = filter_var($email, filter_sanitize_email); //after sanitization validation performed if (filter_var($email, filter_validate_email)) { // send html mail, content-type header must set $headers = 'mime-version: 1.0' . "\r\n"; $headers .= 'content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'from:' . $email. "\r\n"; // sender's email $headers .= 'cc:' . $email. "\r\n"; // carbon copy sender $template = '<div style="padding:50px; color:white;">hello ' . $name . ',<br/>' . '<br/>thank you...! contacting us.<br/><br/>' . 'name:' . $name . '<br/>' . 'email:' . $email . '<br/>' . 'subject:' . $subject . '<br/>' . 'message:' . $message . '<br/><br/>' . 'this contact confirmation mail.' . '<br/>' . 'we contact possible .</div>'; $sendmessage = "<div style=\"background-color:#7e7e7e; color:white;\">" . $template . "</div>"; // message lines should not exceed 70 characters (php rule), wrap $sendmessage = wordwrap($sendmessage, 70); // send mail php mail function mail("asd@gmail.com", $subject, $sendmessage, $headers); echo "your message sent successfully! thank-you contacting us!"; } else { echo "<span>please enter valid email.</span>"; } ?>
if folder structure :
root/main.html
root/css/contact.css
root/test/contact_form.js
root/test2/contact_form.php
then urls don't need ../
exemple: $.post("test2/contact_form.php",{},function(){})
Comments
Post a Comment