how do i send an email when the form is submitted by using ajax

Refresh

2 weeks ago

Views

19 time

2

i have a form on a page called buyback.php and that form is at the bottom of the page. the coding of the form is:-

<div class="mktoForm" id="elqForm">
        <form id="elqForm" method="post" name="elqForm" action="buyback.php">
            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718959"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_First_Name"><strong>First Name</strong> *:</label>
                <input type="text" required="required" id="Contact_First_Name" name="Contact_First_Name" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718960"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_Last_Name"><strong>Last Name</strong> *:</label>
                <input type="text" required="required" id="Contact_Last_Name" name="Contact_Last_Name" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718961"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_Email"><strong>Business Email</strong> *:</label>
                <input type="text" required="required" pattern="[+a-zA-Z0-9._-][email protected][a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$" id="Contact_Email" name="Contact_Email" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718962"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Job_Responsibility"><strong>Job Title</strong> *:</label>
                <input type="text" required="required" id="Job_Responsibility" name="Job_Responsibility" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718963"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Company_Name"><strong>Company Name</strong> *:</label>
                <input type="text" required="required" id="Company_Name" name="Company_Name" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718964"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Company_Country"><strong>Country</strong> *:</label>
                <input type="text" required="required" id="Company_Country" name="Company_Country" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718965"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Company_State_Province"><strong>City</strong> *:</label>
                <input type="text" required="required" id="Company_State_Province" name="Company_State_Province" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718966"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_Phone_Number"><strong>Business Phone Number</strong> *:</label>
                <input type="text" required="required" id="Contact_Phone_Number" name="Contact_Phone_Number" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718966"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Number_Of_Employees"><strong>Number of Employees</strong> *:</label>
                <select id="Number_Of_Employees" name="Number_Of_Employees">
                    <option value="bet10and49">Between 10 and 49</option>
                    <option value="bet50and99">Between 50 and 99</option>
                    <option value="bet100and199">Between 100 and 199</option>
                    <option value="ab200">Above 200</option>
                </select>
            </div>

            <div class="elqFormRow text-center">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718967"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <div class="elqFormRow text-center">
                    <input type="submit" id="elqFormSubmitBut" name="elqFormSubmitBut" class="orange_btn" value="Submit" checkrequired="true">
                </div>
            </div>
        </form>
        <div id="result"><?php if(isset($result)){ echo $result; } ?></div>
    </div>

in that form there is a div which is <div id="result"></div>and i want to print a success message in that form. currently i am reading the field values and sending the email by php by saying:-

if ($_POST['elqFormSubmitBut']) {
$Contact_First_Name = $_POST['Contact_First_Name'];
$Contact_Last_Name = $_POST['Contact_Last_Name'];
$Contact_Email = $_POST['Contact_Email'];
$Job_Responsibility = $_POST['Job_Responsibility'];
$Company_Name = $_POST['Company_Name'];
$Company_Country = $_POST['Company_Country'];
$Company_State_Province = $_POST['Company_State_Province'];
$Contact_Phone_Number = $_POST['Contact_Phone_Number'];
$Number_Of_Employees = $_POST['Number_Of_Employees'];

$to = "[email protected]";
$from = "[email protected]";

switch ($Number_Of_Employees) {
    case "bet10and49": $Number_Of_Employees = "Between 10 and 49"; break;
    case "bet50and99": $Number_Of_Employees = "Between 50 and 99"; break;
    case "bet100and199": $Number_Of_Employees = "Between 100 and 199"; break;
    case "ab200": $Number_Of_Employees = "Above 200"; break;
}

$message = "
  Contact First Name: $Contact_First_Name<br/>
  Contact Last Name: $Contact_Last_Name<br/>
  Contact Email: $Contact_Email<br/>
  Job Responsibility: $Job_Responsibility<br/>
  Company Name: $Company_Name<br/>
  City: $Company_State_Province<br/>
  Business Phone Number: $Contact_Phone_Number<br/>
  Number Of Employees: $Number_Of_Employees";

$subject = "New Message from the Buyback Form";
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'. $from  . " \r\n";

if (mail($to, $subject, $message, $header)) {
    $result = "Thank you for contacting us. We will reply you as soon as possible";
} else {
    $result = "Message Sending Failed, try again";
}

}

and i am echoing $result in the div. how do i write the equivalent ajax code to send the email and write the result message in that div? i need someone to plz tell me exactly what to write

1 answers

0

У вас есть два варианта, ванильный JavaScript или JQuery.

HTML

Вам не нужны какие - либо серьезные изменения в HTML, вам просто нужно добавить onsubmit="return function()"атрибут с функцией вы хотите позвонить, и в этом случае мы будем использовать sendForm().

<form id="elqForm" method="post" name="elqForm" onsubmit="return sendForm()">

JavaScript

Где-то в вашем коде JavaScript, вы должны добавить эти строки:

Vanilla JavaScript:

function sendForm(){
   var form = document.querySelector('elqForm');
   var data = new FormData(form);
   const Http = new XMLHttpRequest();
   const url='path/to/your/phpfile.php';
   Http.open("POST", url);
   Http.send(data);
   Http.onreadystatechange = function () {
    if(Http.readyState === XMLHttpRequest.DONE && Http.status === 200) {
        document.getElementById("result").innerHTML = Http.responseText;
    }
};
return false;
}

С JQuery:

https://api.jquery.com/

function sendForm(){
   var data = $("#elqForm").serialize();
   $.ajax({
     type: "POST",
     url: "path/to/your/phpfile.php",
     data: data,
     success: function(response){
        $("#result").html(response);
     }
   });
}

PHP

Просто измените эти строки, чтобы повторить текст, который вы хотите вывести в ваших делах.

if (mail($to, $subject, $message, $header)) {
    echo "Thank you for contacting us. We will reply you as soon as possible";
} else {
    echo "Message Sending Failed, try again";
}