JQuery Ajax success: function ()

I send the data to the php processing page as follows:

$insert = mysql_query(
    'INSERT INTO ' . $table . ' (' . substr($addfields,0,-1) . ') ' .
    'VALUES  (' . substr($addvals,0,-1) . ')');

I want to have:

if($insert): echo 'Message 1'; else: echo 'message2'; endif;

What should I do in my success: function () to display the message in <div id="result"></div>?

I tried:

success: function() {
     $(#result).html(html);
}

This code does not display the message in the div tag.

How to send data to a div?

+3
source share
1 answer

Assuming your request $.ajax()uses dataType: 'html'(or a default value that is reasonably guessable), your function successwill receive the returned text as the first parameter:

$.ajax({
  url: '/mypage.php',
  dataType: 'html',
  success: function(data) {
    $('#result').html(data);
  }
});

Gotta take any HTML that you drop mypage.phpand replace the content <div id="result">on the page just fine!

jsfiddle demo, .

+5

Source: https://habr.com/ru/post/1748485/


All Articles