Click here to Skip to main content
14,694,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all
I have a php and jquery/ajax call I am using to create a file/send an email. I get all the contents from my textboxes but I want a message to display on the screen afterwards being success or fail. In my .ajax call My call is exiting right before the ‘success:’ part. Why is my call not succeeding? Any tips/help will be appreciated
Thank you
Html page just a form with a submit button
$(document).ready(function () {
    var form = $("#formData");
    $(form).on("submit", function (event) {
            type: "post",
            url: "file.php",
            data: $(this).serialize(),
            beforeSend: function () {
                $("#status").html('<span style="color:red;">Loading everything actually creating a .TXT file of contents...</span>')  //--works
            success: function (data) { 
                var responseMsgType = "alert-" + data.type;
                var responseMsgText = data.message;
                // message below
                var alertBox = '<div class="alert ' + responseMsgType + ' alert-dismissable"><button type="button" class="close" ' +
                    'data-dismiss="alert" aria-hidden="true">×</button>' + responseMsgText + '</div>';
                if (responseMsgType && responseMsgText) {
        return false;
$controls = array('txtName' => 'Name', 'txtEmail' => 'Email', 'txtSubject' => 'Subject', 'txtMessage' => 'Message');
try {
	if(count($_POST)==0) { throw new \Exception ('Contact Form Message is empty'); }

	$headers = array('Content-Type: text/plain; charset="UTF-8";',
					 'From: ' . $email,
					 'Reply-To: ' . $email,
					 'Return-Path: ' .$email);

	$emailMessage = "You have a new message from your contact form" . PHP_EOL;
	$emailMessage .= "-------------------------------------------------------" . PHP_EOL;

	foreach($_POST as $key => $value){
        if(isset($controls[$key])) {
            $emailMessage .= "$controls[$key]: $value". PHP_EOL;
$mailMsg = "email6.txt";
	if(file_exists($filename) == false) {
		$fh = fopen($mailMsg, "w");
		fwrite($fh, $headers);
		fwrite($fh, $emailMessage);
	} else {
		$fhexists = fopen($filename, "a");
		fwrite($fhexists, $content);

	$responseMessage = array("type" => "success", "message" => $successMessage);
catch (\Exception $ex) {
	$responseMessage = array("type" => "errorM", "message" => $errorMessage);
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	$encodedJSON = json_encode($responseMessage);
	header("Content-Type: application/json");
	echo $encodedJSON;
} else {
	echo $responseMessage["message"];

What I have tried:

text file works but email i need on a server i read. I want a success message to pop up on html part #successArea its just a
i have testing codes pop up in #status
Updated 8-Jan-19 19:17pm

1 solution

In my ajax call i added

beforeSend: function () {

I used this to make sure my ajax was actually getting called

After my success:

I added -

error: function (errorThrown) { console.log(errorThrown); }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900