muzaffar527 Posted January 10, 2016 Share Posted January 10, 2016 , 02:03 PM //My contact.php <!DOCTYPE html> <html> <body> <div class="col-sm-6"> <form onsubmit="return false"> <div class="form-group"> <label for="cname" class="control-label">Full Name:</label> <input type="text" class="form-control" name="cname" id="cname"> </div> <div class="form-group"> <label for="mob" class="control-label">Phone Number:</label> <input type="text" class="form-control" name="mob" id="mob"> </div> <div class="form-group"> <label for="mail" class="control-label">Email Address:</label> <input type="email" class="form-control" name="mail" id="mail"> </div> <div class="form-group"> <label for="msg" class="control-label">Message:</label> <textarea class="form-control" rows="4" name="msg" id="msg" style="resize:none"></textarea> </div> <div id="sendmsg"> </div> <button type="submit" name="sendemail" id="sendemail" class="btn btn-success">Send Message</button> </form> </div> <script src="../res/js/jquery.min.js"></script> <script src="../res/js/bootstrap.min.js"></script> <script> $(function(){ $("#sendemail").click(function(){ var err = reqfield_val(['cname', 'mob', 'mail', 'msg']); if(err == 0){ $.ajax({ type: 'post', data: $('form').serialize() }); <?php require_once('../res/php/code.php'); $res = sendemailfn(); // I want to use the function return value here ?> $("#sendmsg").text('<?=$res?>').css("opacity",'1'); } }); }); </script> </body> </html> //mail function code in code.php file <?php function sendemailfn() { if($_POST) // to prevent form submission on page reload (& on not using this condition function is returning the value) { $res = ""; $cname = $_POST['cname']; $email_from = $_POST['mail']; $mobile = $_POST['mob']; $msg = $_POST['msg']; $to = 'muzaffar527@gmail.com'; $body = 'Name'.$cname. '\nEmail'.$email_from. '\nMobile'.$mobile. '\nMessage'.$msg; $subject = 'Enquiry from www.zuhaconsultants.com'; $headers = 'From: '.$email_from."\r\n" . 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); $mail_sent = mail($to, $subject, $body, $headers); if($mail_sent == true) $res = "Thank you for contacting us. We will be in touch with you very soon."; else $res = "Seems Some Error Occured."; return $res; // this value is not returned to contact.php page // if the about condition if($_POST) is not given then it is returning the value // if it is not given the page is submitting the form on page reload } } ?> Please help me get the return value $res... in the same way please note that the form should not be submitted on page reload Thanking you Quote Link to comment https://forums.phpfreaks.com/topic/300250-mail-sending-function-not-returning-value/ Share on other sites More sharing options...
mac_gyver Posted January 10, 2016 Share Posted January 10, 2016 php is a server-side scripting language. ALL THE PHP CODE YOU HAVE ON THE PAGE RUNS WHEN THE PAGE GETS REQUESTED. javascript is a client-side scripting language. by the time any of it runs, the php code has long since finished running. the ajax request would need to submit the form data to the url of a .php page that contains php code to call to the sendemailfn() function and then output the value returned from the function back to the client-side code as a response to the ajax request. since using ajax to submit form data to a php page is a common task, i recommend that you read through some web based tutorials to see how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/300250-mail-sending-function-not-returning-value/#findComment-1529373 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.