samedi 25 avril 2015

Issues adding Ajax to php submission


Trying to resolve my issue of the redirect with a php form but can't seem to get it working with ajax. Any help would really be great. Code for the form and php are below:

HTML - simple html form, working fine, works the way I want it to:

<form id="form" method="POST" action="index.php" enctype="multipart/form-data">
        <fieldset class="grid_12">
            <div>
            <input type="text" name="first_name" class="grid_6" placeholder="First Name" data-validetta="required">
            </div>
            <div>
            <input type="text" name="last_name" class="grid_6 omega" placeholder="Last Name" data-validetta="required"> 
            </div>
            <div>
            <input type="text" name="school" class="grid_6" placeholder="College / University" data-validetta="required">
            </div>
            <div>
            <input type="text" name="title" class="grid_6 omega" placeholder="Title" data-validetta="required">
            </div>
            <div>
            <input type="text" name="email" class="grid_6" placeholder="Email Address" data-validetta="email">
            </div>
            <div>
            <label>
                <select id="mySelect1" type="sel" name="subject" class="dropdown grid_6 omega" >
                    <option placeholder="Select subject below">Select Subject</option>
                    <option value="enroll">How To Enroll</option>
                    <option value="job">Job Opportunities</option>
                    <option value="more-info">I would like more information about FISH </option>
                    <option value="welcome">When will sessions begin?</option>
                </select>
            </label>
            </div>
            <div>
            <textarea type="text" name="message" placeholder="Enter Message Here:" ></textarea>
            </div>
            <button id="submit" type="submit" name="submit" value="submit">SUBMIT</button>
        </fieldset>
    </form>

PHP - submission works properly, but would like to remove the "alert" and replace it with a sweetAlert plugin, but that only seems possible with an ajax submission.

<?php
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $school = $_POST['school'];
    $title = $_POST['title'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $mail_to = 'XYZ';
    $subject = 'Information Request'.$field_name;

    $body_message = 'First Name: '.$first_name."\n";
    $body_message .= 'Last Name: '.$last_name."\n";
    $body_message .= 'Email: '.$school."\n";
    $body_message .= 'School: '.$title."\n";
    $body_message .= 'Title: '.$email."\n";
    $body_message .= 'Subject: '.$subject."\n";
    $body_message .= 'Message: '.$message."\n";

    $headers = 'From: '.$cf_email."\r\n";
    $headers .= 'Reply-To: '.$cf_email."\r\n";

    $mail_status = mail($mail_to, $subject, $body_message, $headers);


if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
sweetAlert('Congratulations!', 'Your message has been successfully     sent', 'success');
 window.location = 'index.html#sign-up';
</script>

<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to XYZ');
window.location = 'index.html';
</script>

<?php
}
?>


Aucun commentaire:

Enregistrer un commentaire