Jump to content

mail sending function not returning value


muzaffar527

Recommended Posts

, 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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.