2

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)

4
  • 2
    Welcome! One tip it would be better to paste your code into the question rather than providing links Commented Nov 19, 2014 at 10:43
  • I tried and it messed up the marks so i used pastebin since it has better code recognition. Commented Nov 19, 2014 at 10:45
  • You can use both. The idea is that if your pastebin link gets broken the question will become useless for other users since they won't be able to see the code. Commented Nov 19, 2014 at 10:56
  • Ok thnx for the tip, I was gonna replace them with public links to my account since i came accros that in my mind already. thnx anyway! :) Commented Nov 19, 2014 at 11:07

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

13 Comments

replacing the . Would be fine then ?
I think you need to replace alert(result.html).fadeIn(); with alert.html(result.html).fadeIn();. I am not sure though whether you can use alert as a name for a variable... A more suitable name would be notice imho.
ahhh damn.. it needs double .html.. hmmm gonna try it and post the results. :)
Ok ok cool it works, every way i wanted it to. Can somebody please vote this up since i cant? Still got a little "style" question regarding my js. the: submit.html('Sending....'); // change submit button text now doesnt work not.. aldo it worked with a other type of AJAX datatype and other succes function.. strange.. can you explain this?
Ah and with the object i needed to target the alert. to the .HTML i get it.. thnx again :)
|
1

We cannot use jquery chaining method in alert function.

1 Comment

Thanks dude! this info adds to the above so i can understand better what the difference is. I Didn't know jquery is all about chaining ^^,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.