使用 tinymce 时无法将内容插入 mysql 数据库
can not insert content to mysql database when use tinymce
我尝试使用像这样的代码在我的表单中使用 tinymce
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery/jquery-1.11.3.js"></script>
<script type="text/javascript" src="tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "#isi",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
</head>
我的表单代码是这样的
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<p>
<label for="judul"></label>
<label for="idkategory"></label >
<select name="idkategory" class="login" id="idkategory" title="<?php echo $_SESSION['MM_UserName']; ?>" required="required" >
<option value="">Pilih Jenis Informasi</option>
<?php
do {
?>
<option value="<?php echo $row_kategori['idkategory']?>"><?php echo $row_kategori['nama']?></option>
<?php
} while ($row_kategori = mysql_fetch_assoc($kategori));
$rows = mysql_num_rows($kategori);
if($rows > 0) {
mysql_data_seek($kategori, 0);
$row_kategori = mysql_fetch_assoc($kategori);
}
?>
</select>
<input name="username" type="hidden" id="username" value="<?php echo $_SESSION['MM_Username']; ?>" />
</p>
<p>
Judul <br /><textarea name="judul" id="judul" cols="55" rows="2" required="required"></textarea>
</p>
<p>
Pengantar <br />
<textarea name="pengantar" id="pengantar" cols="55" rows="3" required="required"></textarea>
</p>
<p>
Isi <br />
<textarea name="isi" id="isi" cols="55" rows="9" required="required"></textarea>
<input name="foto" type="hidden" id="foto" value="no-image.png" />
</p>
<p>
Tanggal <br />
<?php
$today = date("Y-m-d H:i:s"); // 20010310
?>
<input name="tgl" type="text" required="required" id="tgl" value="<?php echo $today; ?>" size="30" />
</p>
<p>
<input name="button2" type="reset" class="login" id="button2" value="Batal" />
<input name="button" type="submit" class="login" id="button" value="Tambah Informasi" />
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form>
我的sql查询是这样的
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO content (username, idkategory, judul, pengantar, isi, foto, tgl) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['username'], "text"),
GetSQLValueString($_POST['idkategory'], "int"),
GetSQLValueString($_POST['judul'], "text"),
GetSQLValueString($_POST['pengantar'], "text"),
GetSQLValueString($_POST['isi'], "text"),
GetSQLValueString($_POST['foto'], "text"),
GetSQLValueString($_POST['tgl'], "date"));
mysql_select_db($database_konek, $konek);
$Result1 = mysql_query($insertSQL, $konek) or die(mysql_error());
$insertGoTo = "informasi.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
当我点击提交按钮 "Tambah Informasi" 时,我无法将内容保存到数据库 mysql,页面甚至不闪烁。当我尝试从页面中删除我的 tinymce 脚本时,一切正常。我的代码有问题吗?有帮助吗?
Trim 插入前的值。默认情况下,tinymce 换行。
使用trim:
trim($_POST['isi']);
或更改插件
force_br_newlines : true,
force_p_newlines : false,
gecko_spellcheck : true,
remove_linebreaks : false,
这可能是因为 tinymce 隐藏了文本区域,因为通常如果未填写所需的表单控件,您会收到一条消息。尝试从 isi textarea 中删除 required 看看会发生什么?
如果您仍然需要确保文本区域已填充,那么您可以使用 JavaScript 在提交表单之前检查是否存在值。看起来好像你已经加载了 Jquery 所以我将使用它作为我的例子。
JQuery:
//on submit function for form1
$("#form1".on("sumbit",function(event){//begin event
//if the length of the isi textarea value is equal to 0
if($("#isi")val().length === 0){//begin if then
//stop the form from submitting
event.preventDefault();
//Do something else here like alert an error or make the text area a
//different color
}//end if then
});//end event
我尝试使用像这样的代码在我的表单中使用 tinymce
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery/jquery-1.11.3.js"></script>
<script type="text/javascript" src="tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "#isi",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
</head>
我的表单代码是这样的
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<p>
<label for="judul"></label>
<label for="idkategory"></label >
<select name="idkategory" class="login" id="idkategory" title="<?php echo $_SESSION['MM_UserName']; ?>" required="required" >
<option value="">Pilih Jenis Informasi</option>
<?php
do {
?>
<option value="<?php echo $row_kategori['idkategory']?>"><?php echo $row_kategori['nama']?></option>
<?php
} while ($row_kategori = mysql_fetch_assoc($kategori));
$rows = mysql_num_rows($kategori);
if($rows > 0) {
mysql_data_seek($kategori, 0);
$row_kategori = mysql_fetch_assoc($kategori);
}
?>
</select>
<input name="username" type="hidden" id="username" value="<?php echo $_SESSION['MM_Username']; ?>" />
</p>
<p>
Judul <br /><textarea name="judul" id="judul" cols="55" rows="2" required="required"></textarea>
</p>
<p>
Pengantar <br />
<textarea name="pengantar" id="pengantar" cols="55" rows="3" required="required"></textarea>
</p>
<p>
Isi <br />
<textarea name="isi" id="isi" cols="55" rows="9" required="required"></textarea>
<input name="foto" type="hidden" id="foto" value="no-image.png" />
</p>
<p>
Tanggal <br />
<?php
$today = date("Y-m-d H:i:s"); // 20010310
?>
<input name="tgl" type="text" required="required" id="tgl" value="<?php echo $today; ?>" size="30" />
</p>
<p>
<input name="button2" type="reset" class="login" id="button2" value="Batal" />
<input name="button" type="submit" class="login" id="button" value="Tambah Informasi" />
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form>
我的sql查询是这样的
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO content (username, idkategory, judul, pengantar, isi, foto, tgl) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['username'], "text"),
GetSQLValueString($_POST['idkategory'], "int"),
GetSQLValueString($_POST['judul'], "text"),
GetSQLValueString($_POST['pengantar'], "text"),
GetSQLValueString($_POST['isi'], "text"),
GetSQLValueString($_POST['foto'], "text"),
GetSQLValueString($_POST['tgl'], "date"));
mysql_select_db($database_konek, $konek);
$Result1 = mysql_query($insertSQL, $konek) or die(mysql_error());
$insertGoTo = "informasi.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
当我点击提交按钮 "Tambah Informasi" 时,我无法将内容保存到数据库 mysql,页面甚至不闪烁。当我尝试从页面中删除我的 tinymce 脚本时,一切正常。我的代码有问题吗?有帮助吗?
Trim 插入前的值。默认情况下,tinymce 换行。
使用trim:
trim($_POST['isi']);
或更改插件
force_br_newlines : true,
force_p_newlines : false,
gecko_spellcheck : true,
remove_linebreaks : false,
这可能是因为 tinymce 隐藏了文本区域,因为通常如果未填写所需的表单控件,您会收到一条消息。尝试从 isi textarea 中删除 required 看看会发生什么?
如果您仍然需要确保文本区域已填充,那么您可以使用 JavaScript 在提交表单之前检查是否存在值。看起来好像你已经加载了 Jquery 所以我将使用它作为我的例子。
JQuery:
//on submit function for form1
$("#form1".on("sumbit",function(event){//begin event
//if the length of the isi textarea value is equal to 0
if($("#isi")val().length === 0){//begin if then
//stop the form from submitting
event.preventDefault();
//Do something else here like alert an error or make the text area a
//different color
}//end if then
});//end event