I do not know where I made a mistake. Unable to transfer data via Ajax to a PHP file.

<ul id="myTab" class="nav nav-tabs nav-justified"> <? while ($news = $news_sql->fetch(PDO::FETCH_BOTH)) { $NewsCategoryID = $news['NewsCategoryID']; echo '<li class=""><a id="NewsCategoryID onclick="myFunction()" href="#' . $news['NewsCategoryID'] . '" data-toggle="tab" aria-expanded="true">' . $news['NewsCategoryTitle'] . '</a></li>'; } ?> </ul> <table class="table table-striped" id="content"></table> <script> function myFunction() { var NewsCategoryID = '<?echo $NewsCategoryID?>' $.ajax({ type: "GET", url: "ajax_news.php", data: NewsCategoryID, dataType: "text", timeout: 30000, async: false, error: function(xhr) { console.log('Ошибка!' + xhr.status + ' ' + xhr.statusText); }, success: function(a) { document.getElementById("content").innerHTML = a; } }); } </script> 
  • What is a var NewsCategoryID = '<?echo $NewsCategoryID?>' ? PS Put a semicolon after the declaration. - mix
  • one
    1. A mixture of PHP and HTML and looks gloomy, and bad in terms of architecture, and you can make a lot of mistakes in it. It is worth using a framework or something else to transfer the data received in the model to the View. 2. JS scripts should be placed in separate files, and even more so they should not be a hint of PHP. I think that after bringing the project into the correct form, many errors and problems will disappear by themselves. - Regent
  • Thanks for the answer, but did not help. The script where the request flies works, but the request itself does not reach the script. Inside the table tag, the answer should come from the ajax_news.php script, the correct <tr> and <td> tags should be answered - Bakhtiyar
  • And, yes: you should not save on spaces and line breaks. For async: false , by the way, is usually supposed to be shot on the spot. And once jQuery is used, then the constructions in the style document.getElementById("content").innerHTML There is no sense to use document.getElementById("content").innerHTML . - Regent
  • 2
    That stuck with their models. Maybe a person has his own religion - mJeevas

3 answers 3

try this

data: { "NewsCategoryID":NewsCategoryID }

     var NewsCategoryID = '<?=$NewsCategoryID;?>'; echo "<li class=''><a id='NewsCategoryID' onclick='myFunction()' href='#{$news['NewsCategoryID']}' data-toggle='tab' aria-expanded='true'>{$news['NewsCategoryTitle']}</a></li>"; 

    Check the punctuation again and then debug see what goes where and comes.

      Decided not to suffer and do differently

       <script> function showUser(str) { if (str == "") { document.getElementById("NewsCategoryID").innerHTML = ""; return; } else { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("NewsCategoryID").innerHTML = xmlhttp.responseText; } }; xmlhttp.open("GET", "ajax_news.php?NewsCategoryID=" + str, true); xmlhttp.send(); } } </script> <ul class="nav nav-tabs nav-justified"> <? while ($news=$ news_sql->fetch(PDO::FETCH_BOTH)){ $NewsCategoryID = $news['NewsCategoryID']; echo ' <li class=""><a onclick="showUser('.$NewsCategoryID.');" data-toggle="tab" aria-expanded="true">'.$news['NewsCategoryTitle'].'</a> </li>'; } ?> </ul> <table class="table table-striped" id="NewsCategoryID"> </table>