有人可以帮助我..我尝试在没有页面刷新的情况下发布fomr,但只有在我尝试inserto或delete时才能正常工作。现在在一起。此示例有效,但当我删除“//” 从PHP表单不插入或删除到SQL。有人能帮助我吗?
当我从PHP喜欢“IF”关闭时工作
PHP
function addToFav() {
global $Itemid;
$user =& JFactory::getUser();
$db2 =& JFactory::getDBO();
$mediaid = $_POST['addMediaId'];
$delid = $_POST['delRow'];
当我从行中删除“//”时,从这个地方出现问题
//if(isset($_POST['submit2']) and $_POST['submit2'] == '1') {
$query = ' INSERT INTO `#__mediamall_favourite_media` (`id`, `userid`, `mediaid`) VALUES ("","'.$user->id.'","'.$mediaid.'")';
// }
//elseif(isset($_POST['submit2']) and $_POST['submit2'] == '0') {
//$query = ' DELETE FROM #__mediamall_favourite_media WHERE id = "'.$delid.'" ';
//}
if($query) {
$db2->setQuery($query);
$db2->query();
mosRedirect('index.php?option=com_mediamall&task=viewdetails&id='.$mediaid.'&Itemid=' . $Itemid);
}
}
FORM
<form action="index.php" method="post" id="myForm" onsubmit="return false">
<input type="hidden" name="option" value="com_mediamall" ></input>
<input type="hidden" name="task" value="addToFav" ></input>
<input type="hidden" name="addMediaId" value="<?php echo $media->id; ?>" ></input>
<input type="hidden" name="delRow" value="<?php echo $del->id; ?>" > </input>
<input type="submit" name="submit2" id="sub" value="<?php if($del->id) { echo '0'; } else { echo '1'; } ?>" ></input>
</form>
JQUERY
<script>
$("#sub").click( function() {
var url=$("#myForm").attr("action");
$.post(url, $("#myForm :input").serializeArray(),function(){
var val=$("#sub").val()== 1 ? 0 : 1;// toggle value of submit for insert or delete
$("#sub").val(val);
});
});
</script>