@rollex_x93

Не работает скрипт js ajax,что не так?

Есть код
<?php
	require "scripts/connect.php";
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script> 
    function showContent(link) { 
        var cont = document.getElementById('main'); 
        var loading = document.getElementById('loading'); 
        cont.innerHTML = loading.innerHTML;   
        var http = createRequestObject(); 
        if( http )  
        { http.open('get', link); 
            http.onreadystatechange = function ()  
            {   if(http.readyState == 4)  
                {   cont.innerHTML = http.responseText;  }    } 
            http.send(null);  } 
        else  
        {  document.location = link;   }   } 
    // ajax объект
    function createRequestObject()  
    {  try { return new XMLHttpRequest() } 
        catch(e)  
        {  try { return new ActiveXObject('Msxml2.XMLHTTP') } 
            catch(e)  
            {   try { return new ActiveXObject('Microsoft.XMLHTTP') } 
                catch(e) { return null; }   } } } 
</script>
	<title></title>
</head>
<body>
<?php if(isset($_SESSION['logged_user'])) : ?>
<div id="wrapper">
<div id="header">
	<div class="modules">
	<ul>
		<a href="#" onClick="showContent('documents.php')"><li>Документы</li></a>
	</ul>
	</div>

</div>
<div class="clr"></div>
<div id="main">

</div>
<div id="loading" style="display: none"> 
Идет загрузка... 
</div> 
<div id="footer"></div>
</div>
<?php else : ?>
	Не авторизован
<?php endif; ?>
</body>
</html>

Тут скрипт отрабатывает отлично,но когда я такой же скрипт хочу сделать на странице documents.php(получается скрипт внутри скрипта),то там он уже не работает.
echo "<div id='buttons'><a href='#' onClick='showContent('test.php')>Добавить</a></div>";
echo "<div id='cells'></div>";
echo "<div id='loading2' style='display: none'> 
Идет загрузка... 
</div> ";
?>
<link rel="stylesheet" type="text/css" href="css/documents.css">
<script> 
    function showContent(link) { 
        var cont = document.getElementById('cells'); 
        var loading = document.getElementById('loading2'); 
        cont.innerHTML = loading.innerHTML;   
        var http = createRequestObject(); 
        if( http )  
        { http.open('get', link); 
            http.onreadystatechange = function ()  
            {   if(http.readyState == 4)  
                {   cont.innerHTML = http.responseText;  }    } 
            http.send(null);  } 
        else  
        {  document.location = link;   }   } 
    // ajax объект
    function createRequestObject()  
    {  try { return new XMLHttpRequest() } 
        catch(e)  
        {  try { return new ActiveXObject('Msxml2.XMLHTTP') } 
            catch(e)  
            {   try { return new ActiveXObject('Microsoft.XMLHTTP') } 
                catch(e) { return null; }   } } } 
</script>
  • Вопрос задан
  • 378 просмотров
Решения вопроса 1
@flx12
вижу ошибку тут, нет закрывающей кавычки у onClick
echo "<div id='buttons'><a href='#' onClick='showContent('test.php')>Добавить</a></div>";

нужно либо заменить echo на
?><div id='buttons'><a href='#' onClick="showContent('test.php')">Добавить</a></div><?

либо экранировать кавычки в echo
onClick=\"showContent('test.php')\"
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы