2010-03-10 132 views
0

嘿傢伙,我需要幫助。有一個我使用JQUERY ajax提交的HTML表單,而不是使用服務器響應進行更新,而是使用索引頁面html進行更新。JQuery Ajax表單提交返回索引頁面的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; 
}) 

這是服務器respo NSE的樣子,即使它插入到數據庫中並執行笏我想用PHP

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

http://www.w3.org/TR/html4/loose.dtd」>

<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"> 

questi on是:爲什麼我的服務器響應整個頁面的html而不僅僅是我在PHP中迴應的響應?

+0

你的問題是什麼? – Jeremy 2010-03-10 20:18:52

回答

0

確認 - 你看到主頁插入#notice,還是瀏覽器重定向到主頁?也就是說,默認表單提交操作是否被取消了?

您是否使用任何類型的Web框架,如Zend Framework,Wordpress,PHPCake等?

EDIT1:這是一個不尋常的問題。一些後續問題:

  1. 包含在您的問題phpscripts.php整個代碼?如果沒有,請包含更多的代碼。請務必在StackOverflow.com UI <code></code>標籤之間以粘貼,因此很容易讓我們讀

  2. 嘗試把代碼行:die('this is a test');phpscripts.php頂部。重複此過程並查看您是否看到主頁或this is a test。如果是後者,請將die()函數向下移動幾行並重復。這將幫助您診斷錯誤。請發佈結果,如果你仍然卡...

+0

否主頁正被插入到#notice div中。已更新的表單顯示在插入的主頁下面 – Hones 2010-03-10 20:48:06

+0

您是否在使用任何類型的Web框架? – Josh 2010-03-10 20:55:24

+0

沒有我沒有使用任何框架我正在編寫我的ow – Hones 2010-03-10 21:07:37

0

謝謝你喬希。原來在試圖阻止直接訪問phpscripts.php的過程中,把這個代碼if(!isset($allowed)){header('Location:index.php');}重定向到了主頁。

感謝您的幫助!

+0

很高興我能幫到你。你應該通過點擊綠色的勾號來接受你自己的答案,這會讓你獲得一個徽章,並且讓別人知道你的問題已經被回答,所以他們不會再試着回答它。 – Josh 2010-03-10 22:15:09