0

Im working on an inventory website for my company so we can keep track of all assets and where they are incase we ever get audited. They want to be able to send each employee an email listing their currently assigned equipment to make sure the database is up to date and all equipment is where it should be. So far I have successfully emailed a form with all the proper data and have been able to retrieve the results when the client submits the form in their email. PROBLEM: I do not want the client to get redirected to where the form is being submitted. I know an ajax request or something like it is required but Im not sure how to send the ajax code inside the php script. If i had to guess, Id say the ajax code will somehow get added as a string inside the $message variable and the action of the form will be removed. Note all the code here is working as I want it to, Im just giving it so you have an idea of my thought process. I took a lot of this code from: https://css-tricks.com/html-forms-in-html-emails/. Heres some of the code:

enter code here
                        $message = '<html><body>';
                        $message .= '<form action = "emailAudit.php" method = "post" target = "_blank">';
                        $message.='<label> Are you using this equipment?</label><br>';
                        foreach($equipment as $eq)
                        {

                            if(!empty($equipment))
                            {                                    
                                $message.= '<input name = "id[]" type = "checkbox" value = '.$eq.'/>' . $eq. '<br>';
                            }
                        }
                        $message .= '<input type = "submit" />Submit <br>';
                        $message .= '</form></body></html>';
                        if(!empty($to)){
                            if(mail($to, $subject, $message, $headers))}
  • Basically I just want to know how I can send ajax code with the php mail function so the emailed client can just submit with the button and not get redirected. Thanks in advance for any comments/tips – Mike DeMilia Nov 04 '16 at 18:51
  • 1
    Possible duplicate of [HTML email with Javascript](http://stackoverflow.com/questions/1088016/html-email-with-javascript) – Patrick Q Nov 04 '16 at 18:56
  • so if this isnt possible how do I always get emailed surveys where my response is posted to some server. Thats exactly what Im trying to do and its definitely possible – Mike DeMilia Nov 04 '16 at 19:01

1 Answers1

0

Firstly - you should send AJAX request using JavaScript, not PHP.

You $message variable is HTML. (IMHO it's not good solution.) The easiest way to do it is attach jQuery library and your AJAX function. Try this:

$message = '<html><body>';
$message .= '<form id="myForm">';
$message.='<label> Are you using this equipment?</label><br>';
foreach($equipment as $eq)
{

    if(!empty($equipment))
    {                                    
        $message.= '<input name="id[]" type="checkbox" value='.$eq.'/>' . $eq. '<br>';
    }
}
$message .= '<input type="submit"/>Submit <br>';
$message .= '</form>';
$message .= '<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function() {
    $('#myForm').submit(function(e) {
        e.preventDefault();
        var datastring = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "emailAudit.php",
            data: datastring,
            dataType: "json",
            success: function(data) {
                //var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
                // do what ever you want with the server response
            },
            error: function() {
                alert("error handing here");
            }
        });
    });
});
</script></body></html>';

if(!empty($to)){
if(mail($to, $subject, $message, $headers))}
  • This is exactly what I was looking for my friend, I really appreciate the quick response. Ill test it and let you know how if it worked. Thanks again! – Mike DeMilia Nov 04 '16 at 19:27
  • It looks like it worked but Ill have to fiddle around with it some more to be able to use the posted data. Thanks a bunch I had a feeling this is how it was suppose to be done but didnt have any luck when I tested it myself. – Mike DeMilia Nov 04 '16 at 19:40
  • also ill note that when i submitted the form in my email, it reloaded my emailed tab into a new tab so i had two tabs viewing the email I sent with this code. Is there anyway to avoid that also, I want no redirection at all, I though the e.preventDefault() would take care of that but It did not work for some reason. I also checked by inspecting the page and it looked like the ajax request did in fact get sent but when I check the page it was sent to it doesnt look like anything was received – Mike DeMilia Nov 04 '16 at 19:50