user4269415
user4269415

Reputation:

Json jQuery and php doesn't process value's correct

I have : Html + jQuery + ajax post and a PHP file to process the form values and returning a error(true or false) and a message with html markups.

My javascript code:

$(document).ready(function() {
  var form = $('#form'); // contact form
  var submit = $('#submit');  // submit button
  var alert = $('.alert'); // alert div for show alert message

  // form submit event
  form.on('submit', function(e) {
    e.preventDefault(); // prevent default form submit

    $.ajax({
      url: 'contact.php', // form action url
      type: 'post', // form submit method get/post
      dataType: 'json', // request type html/json/xml
      data: form.serialize(), // serialize form data
      beforeSend: function() {
        alert.fadeOut();
        submit.html('Sending....'); // change submit button text
      },
    success: function(result) {
        if(result.error){
            /* On error stuff */
            alert(result.html).fadeIn();
        }else{
            /* On success stuff */
            alert(result.html).fadeIn();
        }
    }
    });
  });
});

and at last my php:

if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){
$result = array("error" => false, "html" => null);
$vars = array('name', 'email','telefoonnummer', 'message');
$verified = TRUE;
foreach($vars as $v) {
   if(!isset($_POST[$v]) || empty($_POST[$v])) {
      $verified = FALSE;
   }
}      

if(!$verified) {


$result["error"] = true;
$result["html"] = "<b>Error11</b>";
    echo json_encode($result);
    exit;
}    

$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$tel= filter_var($_POST['telefoonnummer'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

    $to = '';
$sent = email($to, $email, $name, $tel, $message);
if ($sent) {
$result["error"] = false;
$result["html"] = "<b>Success</b>";
    echo json_encode($result);
} else {
$result["error"] = true;
$result["html"] = "<b>Error</b>";
    echo json_encode($result);
    }

  return;
}

/**
 * Email send with headers
 *
 * @return bool | void
 **/
function email($to, $name, $email, $tel, $message){
  $header = array();
  $header[] = "MIME-Version: 1.0";
  $header[] = "From: <".$name."> <".$email.">";

  /* Set message content type HTML*/
  $header[] = "Content-type:text/html; charset=iso-8859-1";
  $header[] = "Content-Transfer-Encoding: 7bit";
  if( mail($to, $tel, $message, implode("\r\n", $header)) ) return true;
}

Ok.. now that's clear I know something goes wrong with the error returning, it does show up and then goes away again n my html so I don't know what exactly happens there..

I don't want "fixes" just so i can copy paste the code but an explanation of what happened and what goes wrong and how to solve it (at least then I learn a little)

Upvotes: 2

Views: 75

Answers (2)

vanarajcs
vanarajcs

Reputation: 978

We cannot use jquery chaining method in alert function.

Upvotes: 1

RichardBernards
RichardBernards

Reputation: 3097

You are using alert in two different ways... One time as an object, one time as a function. The latter is probably what causes the undesired effect. Look closely at the brackets after alert;

  • alert() is a function
  • alert. signafies it's an object

Upvotes: 1

Related Questions