Skip to content Skip to sidebar Skip to footer

Hide Jquery Elements In An Ajax Function

I am trying to submit a form using jquery and the $.ajax () method. When submitting the information to the server I want only the result to remain and the rest of the page to be hi

Solution 1:

Try this

   $('#display').text(result);

I Hope it Helps

I think it's better to addclass. example in your css

.test{ background-color:#000000;}

on success

  $('#display').addClass("test");

or you can add directly

$('#display').css('background-color','block');

I thought your problem is in your css.

you forgot to add in your ajax.

type: "post"

Solution 2:

Since you have not shown any PHP code that does validation I do not know how your code looks but you will have to do something like this:

The following should be put at top of your form.php file:

if(isset($_POST['name'])){

    // return either true/false depending on validation// By default set the success message$success = true;
    $message = 'My success message';

    // now do your conditional checks and based on that change to false// Example: when validation fails on name:if($_POST['name']==''){
        $success = false;
        $message = 'My error message: (Name is a required field)';
    }   

    // convert array to json, so we can read it out nicely on ajax returnecho json_encode($result);

    // stop rest of our codeexit;
}

Then change your ajax success to:

Plese note:

As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.

success: function (result) {
   // Read the json string to proper object, for jQuery 3.0+ use native method insteadvar$data = jQuery.parseJSON(result);

   // Check if there where no validation problemsif($data.success==true){
       // Write success message to DOM element
       $('#display').html($data.message);
   }else{
       // Write error message to DOM element
       $('#display').html($data.message);
   }
},

Post a Comment for "Hide Jquery Elements In An Ajax Function"