[ad_1]
I know this type of question has been asked here before, but I have tried the proposed solutions to no avail. I have been trying to figure this out for days, but I am truly stumped. Help!
I have a form being processed with php via ajax. In the php file I echo a response that is then supposed to populate a div on the page. On my local environment, this works as expected. However, on my live server, instead of the echoed string, ajax returns the entirety of the html on my index.php page. I cannot figure out why this is happening. Code:
index.php:
<!-- index.php -->
<?php include("partials/header.php");
include("functions/register.php");
include("functions/invitefriend.php"); ?>
<!-- ...preceding html.... -->
<!-- Invite a friend form modal -->
<div class="modal fade" tabindex="-1" role="dialog" id="emailFriend" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Share with a friend or colleague via email: </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="loginForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="formRefer">
<div class="form-group">
<label for="fromName">Your full name: </label> <span id="fromNameFeedback"></span>
<input class="form-control" type="text" name="fromName" id="fromName" placeholder="Your full name" required autofocus="true">
</div>
<div class="form-group">
<label for="fromEmail">Your email: </label> <span id="fromEmailFeedback"></span>
<input class="form-control" type="email" name="fromEmail" id="fromEmail" placeholder="Your email" required>
</div>
<div class="form-group">
<label for="toName">Your friend's name: </label> <span id="toNameFeedback"></span>
<input class="form-control" type="text" name="toName" id="toName" placeholder="Your friend's name" required>
</div>
<div class="form-group">
<label for="toEmail">Your friend's email: </label> <span id="toEmailFeedback"></span>
<input class="form-control" type="email" name="toEmail" id="toEmail" placeholder="Your friend's email" required>
</div>
<div class="btn-group" data-toggle="buttons">
<p>This person is a: </p>
<label class="btn btn-info active">
<input type="radio" name="relationship" id="friend" value="friend" autocomplete="off" checked>Friend
</label>
<label class="btn btn-info">
<input type="radio" name="relationship" id="colleague" value="colleague" autocomplete="off">Colleague
</label>
</div>
<div>
<div id="message2" class="questions"><?php echo $message2; ?> </div>
</div>
<div>
<div id="message3" class="questions"> </div>
</div>
<div class="modal-footer">
<input class="btn btn-info loginlogin loginBtn" type="submit" name="send" id="referSubmitButton" value="Send" href="javascript:void(0)">
<button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include("partials/footer.php"); ?>
invitefriend.php:
<?php
//require PHPMailer and autoload
use PHPMailerPHPMailerPHPMailer;
require 'vendor/autoload.php';
//Invite a friend via email form handler
$fromName = strip_tags($_POST["fromName"]);
$fromEmail = strip_tags($_POST["fromEmail"]);
$toName = strip_tags($_POST["toName"]);
$toEmail = strip_tags($_POST["toEmail"]);
// $emailMsg = $_POST["emailMsg"];
$relationship = $_POST["relationship"];
$message2 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
//Validate form
$err = false;
//fromEmail cannot be empty or invalid
if(array_key_exists("fromEmail", $_POST) && PHPMailer::validateAddress($_POST["fromEmail"]))
$fromEmail = strip_tags($_POST["fromEmail"]);
else
$err = true;
echo "<h3>You must provide a valid email address for yourself.</h3>";
if(array_key_exists("toEmail", $_POST) && PHPMailer::validateAddress($_POST["toEmail"]))
$toEmail = strip_tags($_POST["toEmail"]);
else
$err = true;
echo "<h3>You must provide a valid email address for your friend or colleague.</h3>";
if(!$err)
// Insert emails into db (new table for referral addresses)
$query = "INSERT INTO invitees (`toName`, `toEmail`, `fromName`, `fromEmail`, `date`) VALUES ('".mysqli_real_escape_string($conn, $toName)."', '".mysqli_real_escape_string($conn, $toEmail)."', '".mysqli_real_escape_string($conn, $fromName)."', '".mysqli_real_escape_string($conn, $fromEmail)."', CURDATE())";
$result = mysqli_query($conn, $query);
//and send the email using PHPMailer
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.stackmail.com';
$mail->Username = 'invite@heal-the-nation.com';
$mail->Password = 'PMQP1RCngk';
$mail->Port = 587;
//Recipients
$mail->setFrom('invite@heal-the-nation.com', 'Heal the Nation');
$mail->addAddress($toEmail); // Add a recipient
$mail->addReplyTo($fromEmail, $fromName);
// $mail->addCC('cc@example.com');
//Add an image in the email body
$mail->AddEmbeddedImage('./img/HTN_Logo.png', 'HTN_Logo');
$msg = ''; //Many, many lines of an html email
//Content
$mail->isHTML(true);
$mail->Subject = 'An invitation to learn about Heal the Nation: The Healthcare Workers' Network';
$mail->Body = $msg;
$mail->AltBody = strip_tags($msg);
if(!$mail->send())
echo "<h3 class='bg-danger text-info'>Unable to send invitation. Please try again.</h3><br>";
echo "Mailer Error: " . $mail->ErrorInfo;
//exit();
else
echo "<h3 class='bg-success text-info'>Invitation sent!</h3>";
//exit();
;
?>
invitefriend.js:
$(document).ready(function(){
var formRefer = $("#formRefer")
var fromName = $("#fromName").val();
var fromEmail = $("#fromEmail").val();
var toName = $("#toName").val();
var toEmail = $("#toEmail").val();
formRefer.submit(function(event)
event.preventDefault();
showSpinner();
disableButton();
var fromName = $("#fromName").val();
var fromEmail = $("#fromEmail").val();
var toName = $("#toName").val();
var toEmail = $("#toEmail").val();
var formData = $(formRefer).serialize();
var url = $(formRefer).attr('action');
$.ajax(
url:url,
data:formData,
type:'POST',
dataType:"text",
).done(function(response)
console.log(response);
console.log(response.responseText);
if($.trim(response) == "<h3 class='bg-danger text-info'>Unable to send invitation. Please try again.</h3><br>")
console.log("The response says unable to send invite....");
$("#message3").html(response);
else if($.trim(response) == "<h3 class='bg-success text-info'>Invitation sent!</h3>")
console.log("Success!!!");
$("#message3").html(response);
//Clear the form...
).fail(function(data)
console.log(data);
console.log(data.responseText);
if (data.responseText !== '')
$("#message3").text(data.responseText);
else
$("#message3").text('Oops! An error occurred.');
); //end ajax request
); //end formRefer.submit() function
); //end document.ready function
So on my local environment, when I process this form, my console.log(response) gives me the form data (fromName: John, fromEmail: john@john.com, etc.), and the console.log(response.responseText) gives me the appropriate echo value from the php (either "<h3 class='bg-danger text-info'>Unable to send invitation. Please try again.</h3>
" or "<h3 class='bg-success text-info'>Invitation sent!</h3>
"), and this responseText populates the div on the page.
However, when I try this on my live server, it behaves differently. The console.log(response) returns the entire index.php html content, and the console.log(response.responseText) returns 'undefined' and the div on the page remains empty. Why???? I know the form is indeed being processed by php because the data is inserted into the database and the email is sent. So it must be my ajax. I have tried many things suggested on here and elsewhere but none have worked. Please help! Thank you!!
[ad_2]
لینک منبع