E
E
Evgeny Ivanov2018-03-12 21:52:56
JavaScript
Evgeny Ivanov, 2018-03-12 21:52:56

What is the best way to send/receive data with ajax?

A bit confused when working with ajax.
Specifically - about how and where the result is returned.

Part of the code

$.ajax({
url: 'actions.php?show,success: function(data){$('#input_area').html(data);}
});


As I understand it, the result will be returned to the element with id=input_area.
How will he get back there?
I will explain

Here is the function that displays the html code
function show($show)
{
echo '<div id="input_area"><a href="#" class="link">Link</a></div>';
}


And here is the page code Total html code
<div><?php echo show(''); ?></div>

<div>
<div id="input_area">
<a href="#" class="link">Link</a>
</div>
</div>


What happens when you click on a link? If there is js code above?
There will be an ajax request passing the show parameter, which will call the show function (I did not write this code here - it does not matter).
In general, a function will be called that will return echo.
And there is a div id="input_area". But we already have a div id="input_area" on the page (the initial function call).
total error? The block with id="input_area" should contain the code containing the block with id="input_area".
But no - we'll get exactly one div with id="input_area" and a link. What happened? Where did the download take place?

But there is a simpler and more understandable option.
Here is the function that outputs the html code
function show($show)
{
echo '<a href="#" class="link">Link</a>';
}


But the page code AND html code and the result is similar. Where and how does the loading (response) take place in the first case, and which of the options (to determine the id of the div in the loaded function or in the html code of the page, regardless of the function) is better?
<div id="input_area"><?php echo show(''); ?></div>


Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim, 2018-03-12
@ShVad

It is difficult to understand what the task is. What should be done?

$.ajax({
  url: "some.php", // адрес куда отправляем запрос
  success: function(data){
    alert( "Прибыли данные: " + data ); // data можем вставлять в любое место $('#element_id').html(data);
  }
});

T
ThunderCat, 2018-03-13
@ThunderCat

As I understand it, the result will be returned to the element with id=input_area.

$.ajax({
   url: 'actions.php?show',
   success: function(data){ // <-- сюда вернутся данные из скрипта php, попав в data
          $('#input_area').html(data); // здесь они будут вставлены в соответствующий DOM элемент
   }
});

PS: since you missed the quote, the script will most likely not work for you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question