我正在使用bootstrap和一些更多的库创建一个输入字段(请参阅下面的HTML,是的,它是荷兰语)。来自该字段的数据使用jQuery发送到我的PHP(见下文)。我使用jQuery将markdown输入转换为HTML,使用showdown。
PHP生成一个文件,其中正确的输入数据由' |' s分开存储。作者'输入是可选的。一切正常
出现的问题是我想使用回声向用户提供反馈。只是,这似乎没有完成?! PHP中的所有其他函数都会被执行,它会在目标目录中创建一个文件,但回显除外。
我无法在Stackoverflow或其他任何地方找到答案。我做错了什么?
<div class="panel panel-default">
<div class="panel-heading">
Nieuw artikel toevoegen
</div>
<div class="panel-body">
<div class='edit'>
<form action='' method='post'>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Kop*</span>
<input type='text' class='form-control' placeholder='Kop' aria-describedby='basic-addon1' id='title'>
</div>
<br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Datum*</span>
<input type='text' class='form-control' placeholder='Datum' aria-describedby='basic-addon1' id='date'>
</div>
<br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Auteur (evt.)</span>
<input type='text' class='form-control' placeholder='Auteur (evt.)' aria-describedby='basic-addon1' id='author'>
</div>
<br>
<div>
<h4>Inhoud van het artikel*</h4>
<textarea name='textArt' data-provide='markdown' language='nl' rows='20' id='text'></textarea>
</div>
<!--<input type='file' id='filetag'>-->
<br>
<button name='save' class='btn btn-default' id='submit'>Opslaan</button><br>
</form>
</div>
</div>
$(document).ready(function(){
$("#submit").click(function(){
//Request input
var author = $("#author").val();
var date = $("#date").val();
var title = $("#title").val();
var markdown = $("#text").val();
//Convert Markdown to HTML
var converter = new showdown.Converter(),
html = converter.makeHtml(markdown);
//Send input
if($.post("edit.php?loc=nieuws", {text: html, title: title, date: date, author: author})){
console.log("succes");
}
})
})
if ($_POST['text'] != "") {
$directory = "some_target_dir/";
$articles = scandir($directory);
$num_files = count($articles) - 2;
$int = $num_files + 1;
$dir = $directory.$int.".txt";
$end = $_POST['date']."|".$_POST['title']."|".$_POST['text'];
if(!$_POST['author'] == "") {
$end = $end."|".$_POST['author'];
}
$file = fopen($dir, "w") or die("Oops a mistake");
fwrite($file, $end);
fclose($file);
//This one doesn't work?!
echo '<div class="alert alert-success">Succes!</div>';
}
答案 0 :(得分:1)
也许您的PHP代码的一部分失败了,调试返回的内容如下:
$.post("url", {params}).done(function(result){
console.log(result);
// This will show you what the returning output is.
});
编辑:将您的代码更改为:
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
//Request input
var author = $("#author").val();
var date = $("#date").val();
var title = $("#title").val();
var markdown = $("#text").val();
//Convert Markdown to HTML
var converter = new showdown.Converter(),
html = converter.makeHtml(markdown);
//Send input
if($.post("edit.php?loc=nieuws", {text: html, title: title, date: date, author: author})){
console.log("succes");
}
})
})