JQuery Ajax表单提交返回索引页面的html

时间:2010-03-10 19:57:04

标签: php jquery ajax

嘿伙计我需要帮助。有一个我使用JQUERY ajax提交的HTML表单,而不是使用服务器响应进行更新,它正在使用索引页面html进行更新。

表格

  <form   id="publishimages" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
    <input type="submit"  name="publishimages"  value="Publish Selected"  />

</form> 

PHP

if(isset($_POST['publish'])&&$_POST['publish']=="Publish Images"){
//array of sections that may be allowed to have multiple images
$allow_multiple=Array(1 ,7);
$id=filter_input(INPUT_POST,"id",FILTER_SANITIZE_STRING);
$section=filter_input(INPUT_POST,"section",FILTER_SANITIZE_STRING);
//change the string into an array
$id=explode(",", $id);
    //use the array as id
$count=0;
//ensure only 1 pic is published for other sections which do not need gallery
if(!in_array($section, $allow_multiple )){

$query="UPDATE photos SET publish='0' WHERE publish='1' AND section='$section'";

$mysqli->query($query);
echo $mysqli->error;
}
foreach($id as $key=>$value){
    $value=(int)($value);

    if(is_int($value)){

 $sql="UPDATE photos SET publish='1' WHERE id='$value'";

    $result=$mysqli->query($sql);
    echo $mysqli->error;
    if($mysqli->affected_rows>0){$count++;}

    }

}
echo "$count images have been set and will now appear in their respective sections";
}

JQUERY

$('#publishimages').submit(function() {

    //get selected
    var section=$(this).siblings("form").find("#imagesection").val();
    // inside event callbacks 'this' is the DOM element so we first
    // wrap it in a jQuery object and then invoke ajaxSubmit
    var val = [];
    if($(this).find(':checkbox').length >0){
            $(this).find(':checkbox:checked').each(function(i){
            val[i] = $(this).val();
            });
        }
    if($(this).find(':radio').length >0){
            $(this).find(':radio:checked').each(function(i){
            val[i] = $(this).val();
            });
        } 

    $.ajax({
        data:"publish=Publish Images&id="+ val + "&section="+ section,
        type: "POST",
        url: "phpscripts.php",
        success: function(msg){$("#notice").html(msg)
                        }
        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
    });
    return false;
})

这就是服务器响应的样子,即使它插入到数据库中并且使用PHP我想要的

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

http://www.w3.org/TR/html4/loose.dtd” &GT;

    

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title></title>
     <link type="text/css" rel="stylesheet" href="css/admin.css" />
    <script type="text/javascript" src="../scripts/jquery.js"></script>
    <script type="text/javascript" src="jscripts/jquery.MultiFile.js"></script>
    <script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript" src="jscripts/sitescript.js"></script>
</head>
     <body>
    <div class="logd_div"><span class="logd">You are logged as <span>admin</span></span>&nbsp;|&nbsp:<a href="index.php?page=changepwd">Change Password</a>&nbsp;|&nbsp; <a href="logout.php">Sign Out</a></div>
              <div class="admincontent">

问题是:为什么我的服务器响应整个页面的HTML而不仅仅是我在PHP中回应的响应?

2 个答案:

答案 0 :(得分:0)

要确认 - 您是否看到主页插入#notice,或者浏览器是否重定向到主页?也就是说,默认表单提交操作是否被取消?

您使用的是Zend Framework,Wordpress,PHPCake等任何类型的Web框架吗?

EDIT1 :这是一个不寻常的问题。一些后续问题:

  1. 问题中是否包含phpscripts.php的完整代码?如果没有,请包含更多代码。请务必将其粘贴到StackOverflow.com用户界面中的<code></code>标记之间,以便我们轻松阅读

  2. 尝试将代码行die('this is a test');放在phpscripts.php的顶部。重复此过程,看看您是否看到主页,或this is a test。如果是后者,请将die()函数向下移动几行并重复。这将帮助您诊断出错了什么。如果您仍然被卡住,请发布结果......

答案 1 :(得分:0)

谢谢你乔希。事实证明,在尝试阻止直接访问phpscripts.php的过程中,已将此代码if(!isset($allowed)){header('Location:index.php');}重定向到主页。

感谢您的帮助!