2012-03-16 66 views
0

我得到未定義的錯誤,我不明白爲什麼的價值...傳遞現場未定義的錯誤

注意:未定義指數:測試中...」

這裏是pageA的完整的源代碼:

<?php 
if(isset($_POST['save'])) { 
    print "<pre>"; 
    print_r($_POST); 
    print "</pre>"; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script> 
    $(document).ready(function(){ 
     $('#my_form').submit(function(event) { 
      event.preventDefault(); 
      $.post("pageB.php", {'elm1' : tinyMCE.get('elm1').getContent()}, function (response) { 

alert(response) 
}); 

     }); 

    }); 
</script> 





<!-- TinyMCE --> 
<script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script> 
<script type="text/javascript"> 
    tinyMCE.init({ 
     // General options 
     mode : "textareas", 
     theme : "advanced", 
     plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount", 

     // Theme options 
     theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect", 
     theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor", 
     theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen", 
     theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak", 
     theme_advanced_toolbar_location : "top", 
     theme_advanced_toolbar_align : "left", 
     theme_advanced_statusbar_location : "bottom", 
     theme_advanced_resizing : true, 

     // Example content CSS (should be your site CSS) 
     content_css : "css/content.css", 

     // Drop lists for link/image/media/template dialogs 
     template_external_list_url : "lists/template_list.js", 
     external_link_list_url : "lists/link_list.js", 
     external_image_list_url : "lists/image_list.js", 
     media_external_list_url : "lists/media_list.js", 

     // Replace values for the template plugin 
     template_replace_values : { 
      username : "Some User", 
      staffid : "991234" 
     } 
    }); 
</script> 
<!-- /TinyMCE --> 

<script> 

function doSomething(elem) { 
    var currentval = elem.options[elem.selectedIndex].value; 
    // you could navigate away at that point ? 
    window.location = currentval; 
} 

</script> 

</head> 
<body> 


<?php 
$con = mysql_connect('localhost', 'root', 'dev'); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("myDB"); 

$request = mysql_query("select id FROM pages"); 

echo "<select onchange='doSomething(this)'>"; 
while ($drow = mysql_fetch_assoc($request)) 
{ 

    echo '<option value="view.php?id='.$drow['id'].'">'.$drow['id'].'</option>'; 

} 
echo "</select>"; 
?> 


<form id="my_form" method="post" action="pageB.php?id=<?php echo (int)id; ?>"> 
    <div> 
     <h3>View Page</h3> 



     <!-- Gets replaced with TinyMCE, remember HTML in a textarea should be encoded --> 
     <div> 
      <textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 80%"> 
<?php 
$con = mysql_connect('localhost', 'root', 'dev'); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("myDB"); 

$id = (int)$_GET['id']; 


$query = "SELECT * FROM pages where id=$id"; 


$result = mysql_query($query); 
$row = mysql_fetch_assoc($result); 
$contents = $row['content']; 
echo $contents; 
?> 
      </textarea> 
     </div> 

     <br /> 
     <input id="submit_page_changes" type="submit" name="save" value="Submit" /> 
     <input type="reset" name="reset" value="Reset" /> 
    </div> 
</form> 





</body> 
</html> 

THIS IS網頁B的全碼

<?php 
$con = mysql_connect("localhost","root","dev"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("myDB"); 


$id = (int)$_GET['id']; //ERROR POINTS TO HERE 

$sql="UPDATE pages SET content=('$_POST[elm1]') WHERE id='.$id.'"; 

if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
echo "1 record added"; 



$query = "SELECT * FROM pages where id=1"; 
$result = mysql_query($query); 
$row = mysql_fetch_assoc($result); 
$contents = $row['content']; 

echo $contents; 

mysql_close($con) 
?> 

錯誤是:


通知:未定義指數:ID在C:\瓦帕\ WWW \ pageB.php上線
1結果加入

測試212345

回答

2

編輯:

現在我已經看到了代碼,我可以看到問題。在頁面A上,您正在執行ajax請求並取消原始表單提交。在ajax文章中,您不提供id,因此它會產生undefined index錯誤。

你可以做這樣的:

<script> 

var id = <?php echo (int)$_GET['id']; ?>; 

    $(document).ready(function(){ 
     $('#my_form').submit(function(event) { 
      event.preventDefault(); 
      $.post("pageB.php?id=" + id, {'elm1' : tinyMCE.get('elm1').getContent()}, function   (response) { 

alert(response) 
}); 

     }); 

    }); 
</script> 

正如你所看到的,我已經通過了從PHP代碼id到一個javascript變量。然後id在這裏與ajax請求"pageB.php?id=" + id一起發送。利用這一點,你可以用

$id = (int)$_GET['id']; 

也可以看看到現在SQL Injection搶第B的id

+0

試過並添加了:$ id = $ _GET ['id'];到pageB,但得到undefined :( – Satch3000 2012-03-16 11:55:50

+0

什麼是完整的錯誤信息,它是與'test'或'id'有關嗎? – MrCode 2012-03-16 12:03:31

+0

查看問題...在底部添加完整的信息 – Satch3000 2012-03-16 12:29:50

1

你應該使用這個,因爲你正在做一個POST數據。

$test = $_POST['test']; 

UPDATE: 你的網頁A的這一部分看起來時髦

<form id="my_form" method="post" action="pageB.php?id=<?php echo (int)id; ?>"> 

當你試圖迴應一個ID存在未在代碼中設置它上面的地方,至少我無法找到$ id正在被設置。

$ id在表單中設置,無法在表單標題中調用,因爲在您提交表單/調用它時它不存在。

您應該在表單操作中調用它之前設置$ id。

我也認爲你應該在表單動作中調用$ id而不是id。

+0

試過了,仍然得到相同的結果 – Satch3000 2012-03-16 10:23:40

+0

你在做print_r($ _ POST)時會得到結果嗎?在page_b.php? – GuZzie 2012-03-16 10:40:25

+0

是的,但隱藏的字段值沒有被傳遞......非常奇怪:o/ – Satch3000 2012-03-16 10:43:45

1
do this : 
first of all remove onchange='doSomething(this)'> 
give some id to select 
<select id="xyz"> 

and echo''。$ drow ['id']。'';

change 


$('#my_form').submit(function(event) { 
      event.preventDefault(); 
     id=$('#xyz').val(); 
//get selected value, use id below to pass to pageB.php along with elem1 

    $.post("pageB.php", {'id':id,'elm1' : tinyMCE.get('elm1').getContent()}, function (response) { 

alert(response) 
}); 

     }); 


let me know if it works