2

I want to clear the input values when the form is submitted correctly, But there is a problem, if not inserted, the inputs are erased. I will be grateful for help me. This is my code:

$(document).ready(function(){
    $("#Insert").click(function(){
        var email = $("#email").val();
        var title = $("title").val();
        var text = $("text").val();
        var cSend = true;
$.post("ajax.php",{email:email,title:title,text:text,cSend:cSend},function(data){
        $("#contactResualt").html(data);

        });
    });
});

//ajax.php

if(isset($_POST['cSend'])){
  if (empty($_POST['email']) || empty($_POST['title']) || empty($_POST['text'])){
    echo '<div class="alert alert-warning">Fill empty fields</div>';
}
else{
    $email = $_POST['email'];
    $title = $_POST['title'];
    $text = $_POST['text'];
    $Contactus = new Contactus();
    $resualt = $Contactus->SendPM($email,$title,$text);
        if($resualt){
        echo '<div class="alert alert-success">Insertion was successful</div>';
        }
        else{
        echo '<div class="alert alert-warning">Insertion failed</div>';
        }
    }
}

So, I just want the information to be correctly inserted, Inputs will be cleared.

3
  • If the inputs are in a form, and blank when the page loads, you could use $("#form-id").reset(); Commented Jul 8, 2017 at 11:32
  • I have not used the form Commented Jul 8, 2017 at 11:36
  • The selectors for title and text are incorrect Commented Jul 8, 2017 at 11:38

2 Answers 2

1

If you only want to erase the fields when the insert was successful, you can put an indicator attribute into the response HTML:

if($resualt) {
  echo '<div class="alert alert-success" data-success="true">Insertion was successful</div>';
}
else {
  echo '<div class="alert alert-warning" data-success="false">Insertion failed</div>';
}

Then use the indicator in the AJAX callback to determine whether you should reset the form:

$.post("ajax.php", {
  email: email,
  title: title,
  text: text,
  cSend: cSend
}, function(data) {
  $("#contactResualt").html(data);
  if($("#contactResualt [data-success]").attr("data-success") === "true") {
    $("#email, #title, #text").val("");
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

With this method, my problem was solved. thank you very much.
If this answer solved the problem, please mark it as the solution.
0

You can clear the input field by using $('input').val('');

$(document).ready(function(){
$("#Insert").click(function(){
    var email = $("#email").val();
    var title = $("title").val();
    var text = $("text").val();
    var cSend = true;
    $.post("ajax.php",
    {email:email,title:title,text:text,cSend:cSend},function(data){
    var json_obj = $.parseJSON(data);//parse JSON
    $("#contactResualt").html(json_obj.msg);
          if(json_obj.status){
            $('input').val('');
         }
    });
    });
});

Ajax.php

if(isset($_POST['cSend'])){
 if (empty($_POST['email']) || empty($_POST['title']) || 
 empty($_POST['text'])){
 $result['status'] = false;         
  $result['msg'] =  '<div class="alert alert-warning">Fill empty 
 fields</div>';
 }
 else{
 $email = $_POST['email'];
$title = $_POST['title'];
$text = $_POST['text'];
$Contactus = new Contactus();
$resualt = $Contactus->SendPM($email,$title,$text);
    if($resualt){
    $result['status'] = true;
    $result['msg'] = '<div class="alert alert-success">Insertion was successful</div>';
    }
    else{
    $result['status'] = false;          
    $result['msg'] =  '<div class="alert alert-warning">Insertion failed</div>';
    }
   }
   echo json_encode($result);
}

2 Comments

I do not want this method, this way, if the fields are empty, inputs are cleared I just want the inputs to be cleared when the information is inserted
Now i changed my code. Now if status true that input fields will be empty

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.