Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:20:38+00:00 2026-06-02T05:20:38+00:00

So I have a contact field which sends an email when submitted, I want

  • 0

So I have a contact field which sends an email when submitted, I want to have the javascript alert when complete but am returning issues.

The javascript:

jQuery(document).ready(function() {

    $.ajax({
        url: './contactengine.php',
        type: 'GET',
        dataType: 'JSON',
        success: function(response) {
                        alert("GOOD");
                },
        error: function() {
                        alert("BAD");
                }
    });

});

The php:

<?php

$EmailFrom = "emailname@gmail.com";
$EmailTo = "emailto@gmail.com";
$Subject = "new contact from website";
$Name = Trim(stripslashes($_POST['Name'])); 
$company = Trim(stripslashes($_POST['company'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "company: ";
$Body .= $company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success)
{
    $result = array('success' => true);
}
else
{
    $result = array('success' => false, 'message' => 'Something happened');
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
  echo json_encode($result);
?>

When I submit the form I get redirected to a page which says the following:

{"success":true}

When I should be staying on the same page and just alerting good.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:20:40+00:00Added an answer on June 2, 2026 at 5:20 am

    The problem here is in your script I think, from the behaviour you describe it sounds like your PHP is doing the job.

    I’ve made a few corrections to the ajax call based on the PHP code, try this and see if it helps.

    $.ajax({
            url: 'contactengine.php', //removed ./ - that's wrong I think
            type: 'POST',  //you are accessing $_POST not $_GET in the PHP file
            data: $('myform').serialize(), //you need to send some data atleast
            dataType: 'JSON',
            success: function(response) {
                            alert("GOOD");
                    },
            error: function() {
                            alert("BAD");
                    }
        });
    

    What I’ve done is removed ./ from the front of your URL – it should be either nothing, / for the root directory, or ../ for one directory up from the current directory. If the file is in the same directory as your page, keep it like I’ve done.

    I’ve changed type from GET to POST – you are accessing the PHP $_POST global in your script, so the PHP would have been looking in the wrong place for variables.

    Last but not least, I’ve put some data in. You need to pass in those variables that the PHP expects – so I’ve used a serialize() call. Replace the jQuery selector so that it will target your form, and it should pass in everything in your form to the PHP.

    For more detail on the jQuery ajax function, see here: http://api.jquery.com/jQuery.ajax/

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contact form that can be hidden using .slideToggle() but I want
I'm making a contact form which will be submitted with jQuery and I'm stuck
I have a contact form in my flash file with name/email/message fields which a
I have a basic PHP form here that sends an email when submitted: <div
I have a daily-generated CSV file containing contact data which I want load entries
I have an MVC application which has a set of fields for contact details
i have a contact form sending itself to me by email using ASP classic,
I have a contact form where the email is actually accessible in the source,
I want to have a contact us on my site such that a user
I have a contact form, the values of which I'm passing to a php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.