Не срабатывает Javascript
Есть такой код.
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'>
function add_news()
{
v_news_date = document.getElementById('news_date').value;
v_caption = document.getElementById('caption').value;
v_img = document.getElementById('img').value;
v_news_text = document.getElementById('news_text').value;
v_anounce = document.getElementById('anounce').value;
/*
$.ajax(
{
url: 'add_news.php',
data: 'news_date=v_news_date&caption=v_caption&img=v_img&news_text=v_news_text&anounce=v_anounce',
success: function()
{
alert('News deleted.');
}
error: function()
{
alert(id);
}
}
);
*/
Но при
echo "
<form enctype='multipart/form-data' action='add_news.php' method='post'>
<p><label>Дата</label><input id='news_date' name='news_date' type='date' required></input></p>
<p><label>Заголовок</label><input id='caption' name='caption' type='text' required></input></p>
<p><label>Картинка</label><input id='img' name='img' type='file'></input></p>
<p><label>Новость</label><textarea id='news_text' name='news_text' required></textarea></p>
<p><label>Анонс</label><textarea id='anounce' name='anounce'></textarea></p>
<p><input type='button' onclick='add_news();' value='+'></p>
</form>
" ;
не работает. Пишет
ReferenceError: add_news is not defined[Подробнее]
Почему и как победить?
То что меня не убивает, делает меня умнее.
Re: Не срабатывает Javascript
Здравствуйте, Funny Rabbit, Вы писали:
FR>Почему и как победить?
Не знаю что там у тебя за echo, но вот вся страница в одном файле, и всё работает.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" ></script>
</head>
<body>
<form enctype='multipart/form-data' action='add_news.php' method='post' >
<p><label>Дата</label><input id='news_date' name='news_date' type='date' required></input></p>
<p><label>Заголовок</label><input id='caption' name='caption' type='text' required></input></p>
<p><label>Картинка</label><input id='img' name='img' type='file' ></input></p>
<p><label>Новость</label><textarea id='news_text' name='news_text' required></textarea></p>
<p><label>Анонс</label><textarea id='anounce' name='anounce' ></textarea></p>
<p><input type='button' onclick='add_news();' value='+' ></p>
</form>
</body>
<script>
$(document).ready(function () {
console.log('Hello' );
});
function add_news() {
console.log('add_news called' );
}
</script>
</html>
Спасибо за внимание
Re: Не срабатывает Javascript
От:
vsb
Дата: 24.01.19 08:32
Оценка:
4 (1)
Ты или пиши <script src='
https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script> или <script>тут код</script>. Одновременно грузить файл и задавать код нельзя. Т.е. в твоём случае надо писать так:
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script>
function add_news() { ... }
</script>
PS RSDN-овский форматтер лажает, но думаю, ты понял.
Re[2]: Не срабатывает Javascript
Здравствуйте, vsb, Вы писали:
Теперь ругается на это.
<html>
<head>
<link href='active_time.css' rel='stylesheet' type='text/css' >
<link href='calendar.css' rel='stylesheet' type='text/css' >
<link href='buttons.css' rel='stylesheet' type='text/css' >
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js' ></script>
<script >
function add_news()
{
v_news_date = document.getElementById('news_date' ).value;
v_caption = document.getElementById('caption' ).value;
v_img = document.getElementById('img' ).value;
v_news_text = document.getElementById('news_text' ).value;
v_anounce = document.getElementById('anounce' ).value;
$.ajax(
{
url: 'add_news.php' ,
data: 'news_date=v_news_date&caption=v_caption&img=v_img&news_text=v_news_text&anounce=v_anounce' ,
success: function ()
{
alert('News deleted.' );
}
error: function ()
{
alert(id);
}
}
);
};
function delete_news(p_id)
{
$.ajax(
{
url: 'delete_news.php' ,
data: 'id=p_id' ,
success: function ()
{
alert('News deleted.' );
}
error: function ()
{
alert(id);
}
}
);
};
</script>
</head>
<body>
<table class ='main wide' width='96%' border='0' align='center' cellpadding='0' cellspacing='0' >
<tr><th>Номер строки</th><th>Картинка</th><th>Заголовок</th><th width='50%' >Новость</th><th colspan='2' >Действие</th></tr>
<tr>
<td class ='light' align='center' ><a href='view_news.php?id=60' >1</a></td>
<td class ='dark' align='center' ><img height='50' width='50' src='/var/www/html/img.jpg' ></td>
<td class ='light' >vFVF</td>
<td class ='dark' align='center' >SGDGSDF</td>
<td class ='light' align='center' ><input type='button' onclick='delete_news(60);' class ='button' value='X' ></td>
</tr>
<tr>
<td class ='light' align='center' ><a href='view_news.php?id=61' >2</a></td>
<td class ='dark' align='center' ><img height='50' width='50' src='/var/www/html/img.jpg' ></td>
<td class ='light' >vFVF</td>
<td class ='dark' align='center' >SGDGSDF</td>
<td class ='light' align='center' ><input type='button' onclick='delete_news(61);' class ='button' value='X' ></td>
</tr>
</table><br>
<form enctype='multipart/form-data' action='add_news.php' method='post' >
<table border='0' cellspacing='1' cellpadding='3' align='center' class ='main wide' >
<tr>
<th colspan='2' >Введите новость</th>
</tr>
<tr>
<td class ='light' >Дата:</td>
<td class ='light' >
<input id='news_date' name='news_date' type='date' required></input>
</td>
</tr>
<tr>
<td class ='dark' >Заголовок:</td>
<td class ='dark' >
<input id='caption' name='caption' type='text' required></input>
</td>
</tr>
<tr>
<td class ='light' >Новость:</td>
<td class ='light' >
<textarea id='news_text' name='news_text' required></textarea>
</td>
</tr>
<tr>
<td class ='dark' >Картинка:</td>
<td class ='dark' >
<input id='img' name='img' type='file' ></input>
</td>
</tr>
<tr>
<td class ='light' >Анонс:</td>
<td class ='light' >
<textarea id='anounce' name='anounce' ></textarea>
</td>
</tr>
<tr>
<th align='center' colspan='2' >
<input type='button' onclick='add_news();' value='+' class ='button' >
<input type='reset' value='-' class ='button' >
</th>
</tr>
</table>
</form>
<br>
<a href='print_news.php?page=1' >1</a>   </body></html>
Говорит
SyntaxError: missing } after property list[Подробнее] print_news.php:25:3 note: { opened at line 18, column 2
То что меня не убивает, делает меня умнее.
Re[3]: Не срабатывает Javascript
Здравствуйте, Funny Rabbit, Вы писали:
FR>SyntaxError: missing } after property list[Подробнее] print_news.php:25:3 note: { opened at line 18, column 2
Глазами вижу, что как минимум пропущена запятая:
success: function()
{
alert('News deleted.');
} !,!
error: function()
{
alert(id);
}
Не бойтесь совершенства. Вам его не достичь. © Сальвадор Дали
Re[4]: Не срабатывает Javascript
Здравствуйте, Senyai, Вы писали:
Спасибо мужики. Совсем заработался
То что меня не убивает, делает меня умнее.
Re[2]: Не срабатывает Javascript
Здравствуйте, vsb, Вы писали:
Ребят. Помогите последний раз. Вот в этом
<html>
<head>
<link href='active_time.css' rel='stylesheet' type='text/css' >
<link href='calendar.css' rel='stylesheet' type='text/css' >
<link href='buttons.css' rel='stylesheet' type='text/css' >
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js' ></script>
<script>
function refresh_news()
{
location.reload();
};
function add_news()
{
v_news_date = document.getElementById('news_date' ).value;
v_caption = document.getElementById('caption' ).value;
v_img = document.getElementById('img' ).value;
v_news_text = document.getElementById('news_text' ).value;
v_anounce = document.getElementById('anounce' ).value;
$.ajax(
{
url: 'add_news.php' ,
data: 'news_date=' +v_news_date+'&caption=' +v_caption+'&img=' +v_img+'&news_text=' +v_news_text+'&anounce=' +v_anounce+'' ,
success: function ()
{
refresh_news();
}
}
);
};
function delete_news(p_id)
{
$.ajax(
{
url: 'delete_news.php' ,
data: 'id=' +p_id+'' ,
success: function ()
{
refresh_news();
}
}
);
};
</script>
</head>
<body>
<table class ='main wide' width='96%' border='0' align='center' cellpadding='0' cellspacing='0' >
<tr><th>Номер строки</th><th>Картинка</th><th>Заголовок</th><th width='50%' >Новость</th><th colspan='2' >Действие</th></tr>
<tr>
<td class ='light' align='center' ><a href='view_news.php?id=63' >1</a></td>
<td class ='dark' align='center' ><img height='50' width='50' src='/var/www/html/img.jpg' ></td>
<td class ='light' >2</td>
<td class ='dark' align='center' >xcbxvcxvb</td>
<td class ='light' align='center' ><input type='button' onclick='delete_news(63);' class ='button' value='X' ></td>
</tr>
<tr>
<td class ='light' align='center' ><a href='view_news.php?id=64' >2</a></td>
<td class ='dark' align='center' ><img height='50' width='50' src='/var/www/html/img.jpg' ></td>
<td class ='light' >3</td>
<td class ='dark' align='center' >xcbxvcxvb</td>
<td class ='light' align='center' ><input type='button' onclick='delete_news(64);' class ='button' value='X' ></td>
</tr>
</table><br>
<form enctype='multipart/form-data' action='add_news.php' method='post' >
<table border='0' cellspacing='1' cellpadding='3' align='center' class ='main wide' >
<tr>
<th colspan='2' >Введите новость</th>
</tr>
<tr>
<td class ='light' >Дата:</td>
<td class ='light' >
<input id='news_date' name='news_date' type='date' required></input>
</td>
</tr>
<tr>
<td class ='dark' >Заголовок:</td>
<td class ='dark' >
<input id='caption' name='caption' type='text' required></input>
</td>
</tr>
<tr>
<td class ='light' >Новость:</td>
<td class ='light' >
<textarea id='news_text' name='news_text' required></textarea>
</td>
</tr>
<tr>
<td class ='dark' >Картинка:</td>
<td class ='dark' >
<input id='img' name='img' type='file' ></input>
</td>
</tr>
<tr>
<td class ='light' >Анонс:</td>
<td class ='light' >
<textarea id='anounce' name='anounce' ></textarea>
</td>
</tr>
<tr>
<th align='center' colspan='2' >
<input type='button' onclick='add_news();' value='+' class ='button' >
<input type='reset' value='-' class ='button' >
</th>
</tr>
</table>
</form>
<br>
<a href='print_news.php?page=1' >1</a>   </body></html>
Картинка должна добавляться в базу. При обычном сабмите все хорошо, а вот средствами JQuery не хочет, хотя значение ее я получаю.
То что меня не убивает, делает меня умнее.
Re[3]: Не срабатывает Javascript
От:
vsb
Дата: 24.01.19 10:24
Оценка:
4 (1)
Здравствуйте, Funny Rabbit, Вы писали:
FR>Картинка должна добавляться в базу. При обычном сабмите все хорошо, а вот средствами JQuery не хочет, хотя значение ее я получаю.
С файлами так не работают, это не строка. Разбирайся, как послать файл в виде MIME-вложения (так же, как его посылает браузер при самбите). Заодно и остальные поля передавай в виде POST-данных, а не в URL-е. И значения экранировать нужно, в твоём варианте текст с символом "&", например, не передастся как положено.
Пока на собственное сообщение не было ответов, его можно удалить.
Удалить