0

I have a form which uses event.preventdefault. I then use Ajax to start the script which works fine. I then use PHP to send an email which also works fine apart from I'm not getting any values from my form inserted into the email - any ideas to why? The email still sends but only with the hardcoded message.

Form:

<form action="post.php" class="enquire-form" method="post">

<label for="" class="darktext">Contact details</label>
<div class="form-group">
    <input class="form-control" type="text" name="name" id="name" placeholder="NAME" min="2" maxlength="75" required>
</div>

<div class="form-group">
    <input class="form-control" type="number" name="contact-number" id="contact-no" placeholder="PHONE NUMBER" min="6" maxlength="20" required>
</div>
<div class="form-group">
    <input class="form-control" type="email" name="email" id="email" placeholder="EMAIL ADDRESS" maxlength="40" min="4" required>
</div>
<label for="" class="darktext">Job details</label>
<div class="form-group ">
    <input class="form-control" type="text" name="job-title" id="job-title" value="" placeholder="TYPE OF JOB" maxlength="300" min="5" required>
</div>
<div class="form-group ">
    <input class="form-control" type="text" name="job-location" id="job-location" value="" placeholder="JOB LOCATION" maxlength="75" required>
</div>
<div class="g-recaptcha" data-sitekey="REMOVED"></div>

<button type="submit" class="submit-job signbuttons btn btn-primary">
        <span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> 
        Send enquiry
</button>

PHP

$name = $_POST['#name'];
$number = $_POST['#contact-no'];
$email = $_POST['#email'];
$jobTitle = $_POST['#job-title'];
$jobLocation = $_POST['#job-location'];
$message = "Job request: \n Name: $name \n Contact Number: $number \n Email: 
$email \n Job Title: $jobTitle \n Job location: $jobLocation";
$to      = 'me@example.com';
$subject = "Enquiry from $name";
$headers = 'From: me@ex.com' . "\r\n" .
    'Reply-To: me@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Ajax:

$('.enquire-form').submit(function(e) {
    e.preventDefault();
    $this = $(this);
    $.ajax({
        type: "POST",
        url: "post.php",
        data: $this.serialize()
    }).done(function(data) {
        alert("Email sent, we'll be in touch soon.");
        $(".submit-job").attr("disabled", true).prop('title', 'Your request has already been sent');
    }).fail(function( jqXHR, textStatus ) {
        alert("Failed to send the request: " + textStatus);
    });
});

Thanks

Zain
  • 1,246
  • 4
  • 15
  • 26
  • 2
    What the heck is `#name`??? Just `$_POST['name']`. `print_r($_POST);` will show you what's there. – AbraCadaver Jul 18 '17 at 18:28
  • darn those hashtags! – Funk Forty Niner Jul 18 '17 at 18:28
  • because you use ajax? post your ajax call, post variables probably aren't passed – Garr Godfrey Jul 18 '17 at 18:28
  • 1
    undefined index dupe worthy – Funk Forty Niner Jul 18 '17 at 18:28
  • 2
    Yes, because PHP !== Twitter. – AbraCadaver Jul 18 '17 at 18:29
  • Also, use `error_reporting(E_ALL); ini_set('display_errors', '1');` to see the errors. – AbraCadaver Jul 18 '17 at 18:29
  • @GarrGodfrey Still no luck, I've updated the original post with the Ajax call – Zain Jul 18 '17 at 18:36
  • 1
    [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Jul 18 '17 at 18:36
  • @JayBlanchard I'll check out the link you sent me. I don't really use PHP/AJAX but I'm in a rush to get this out so I've had to fudge it. I'll post again after I've read it Error - Internal Server Error. Running this on a website – Zain Jul 18 '17 at 18:44
  • 1
    If it is an Internal Server Error you'll need to look at your web server's error logs. – Jay Blanchard Jul 18 '17 at 18:45
  • 1
    @JayBlanchard The internal error was from a typo I made. Thank you for helping me, from the bottom of my heart it means a lot - I felt a bit bad from everyone else's reaction but you went the extra mile and it was very motivating.. I've read the link you sent me + the questions you asked me made me think about the problem more rather than getting fixated on a it should be working mentality. Again, thank you – Zain Jul 18 '17 at 18:59
  • 1
    Glad you found the problem and glad I could help @Zain. I totally understand the single-minded focus, we all do that. When we do it is a clue we should step back and take a breath! – Jay Blanchard Jul 18 '17 at 19:01

0 Answers0