I can not understand why it returns an empty string and not echo "success"

JS code

function submitForm(){ var category = $("#commentCategory").val(); var nickname = $("#commentNickname").val(); var message = $("#commentMessage").val(); var email = $("#commentEmail").val(); var title = $("#commentTitle").val(); $.ajax({ type: "POST", url: "includes/commentprocess.php", data: "category=" + category + "&nickname=" + nickname + "&comment=" + message +"&email=" + email + "&title=" + title, //$(this).serialize(), success : function(text){ if (text == "success"){ formSuccess(); console.log('b'); } else { formError(); submitMSG(false,text); console.log(text); } } }); 

}

Php

 <?php $errorMSG = ""; if(!empty($_POST['commentsubmit'])){ $category = $_POST['commentCategory']; $nick = $_POST['commentNickname']; $commessage = $_POST['commentMessage']; $email = $_POST['commentEmail']; $title = $_POST['commentTitle']; if ($title == '' OR $nick == '' OR $email == '' OR $category == '' OR $commessage == '') { $errorMSG = "Заполните поля"; exit(); } else { $user=R::dispense( 'comments' ); $user->category = $category; $user->nickname = $nick; $user->comment = $commessage; $user->email = $email; $user->title = $title; R::store($user); if ($errorMSG == "") { echo "success"; } } } ?> 

Connection is made by RedBeanom. New to this thread.

sample form field

  <form role="form" id="commentForm" data-toggle="validator" method="POST"> <div class="row"> <div class="form-group col-sm-6"> <label for="commentTitle" class="h4">Тема</label> <input type="text" class="form-control" id="commentTitle" placeholder="Тема" required> <div class="help-block with-errors"></div> </div> 

    1 answer 1

    why returns an empty string and not echo "success"

    Because of this condition:

     if(!empty($_POST['commentsubmit'])){ ... 

    In the data that you send to the server, there is no item with the key 'commentsubmit' .

    (Obviously, this is the name button with type="submit" , which was previously (before ajax -a) responsible for submitting the form.)

    You can add it:

     data: "commentsubmit=true&category=" + category + "&nickname=" + nickname + "&comment=" + message +"&email=" + email + "&title=" + title, 

    Or remove the condition in php.