我正在开发一个PHP Web应用程序
在类别页面上显示所有类别,我想要在
我还希望在提示框(min-length='5'
)上进行一些验证,如果用户点击取消按钮返回false(现在返回null),如果验证没问题,则不提交表单,然后单击确定提交带有类别ID和新类别名称(提示值)的表单以更新数据。
代码示例:
echo "<table>";
$strCats=$con->query("SELECT * FROM categories ORDER BY catName");
$CatsRows=$strCats->rowCount();
if ($CatsRows==0) {
echo "NO DATA FOUND";
}else{
while($CatsRow = $strCats->fetch()){
echo "<tr><td>$CatsRow[catName]</td>
<td><button type='submit' name='btnEditCategory' value='$CatsRow[catId]' onclick='return prompt(\"Rename category\",\"$CatsRow[catName]\");'>EDIT</button></td></tr>";
}
}
echo "</table>";
答案 0 :(得分:0)
您需要使用按钮点击运行的javascript函数,并通过ajax提交值。
<script type="text/javascript">
function btnEditCategory(catID){
//edit function with ajax call to edit category
}
</script>
<input type="submit" onclick="return btnEditCategory('1');" value="Edit">
在编辑功能
中传递类别ID进行编辑答案 1 :(得分:0)
最后我解决了。
if(isset("cmdEditCategory")){
$catName = str_replace('"',"`",str_replace("'","`",$_POST['txtCatName']));
$catId = $_POST['cmdEditCategory'];
$sqlEditCategory=$con->prepare("UPDATE categories SET catName=? WHERE catId=?");
$sqlEditCategory->bindParam(1, $catName, PDO::PARAM_STR, 25);
$sqlEditCategory->bindParam(2, $catId, PDO::PARAM_INT, 11);
if ($sqlEditCategory->execute()) {
echo "<script type='text/javascript'>notif('success','". MSG_SUCCESS ."','". MSG_UPDATED ."');</script>";
}else{
echo "<script type='text/javascript'>notif('error','". MSG_WARNING ."','". MSG_ERROR ."');</script>";
}
}
$strCats=$con->query("SELECT * FROM categories ORDER BY catName");
$CatsRows=$strCats->rowCount();
if ($CatsRows==0) {
echo "<div class='alert alert-danger'>". MSG_NO_DATA_FOUND ."</div>";
}else{
echo "<form action='/..$_SERVER[REQUEST_URI]' method='post'>
<table>";
while($CatsRow = $strCats->fetch()){
echo "<tr><td>$CatsRow[catName]</td>
<td>
<button type='submit' name='cmdEditCategory' value='$CatsRow[catId]' onclick='return editCategory(\"$CatsRow[catName]\");'>". EDIT ."</button>
</td>
</tr>";
}
echo "</table>
<div style='display:none'>
<input type='text' id='txtCatName' name='txtCatName' />
</div>
</form>
<script type='text/javascript'>
function editCategory(catName){
var msg = prompt('Rename category',catName);
if(msg != null){
document.getElementById('txtCatName').value=msg;
return true;
}else{
return false;
}
}
</script>";
}
如果还有其他更好的解决方案,请回复。