传递字段未定义错误的值

时间:2012-03-16 10:19:50

标签: php

我收到了未定义的错误,我不明白为什么......

通知:未定义的索引:测试...”

这是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>

这是PAGEB的完整代码

<?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)
?>

错误是:


注意 10 C:\ wamp \ www \ pageB.php 中的未定义索引:id 已添加1条记录

测试212345

3 个答案:

答案 0 :(得分: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在此"pageB.php?id=" + id发送带有ajax请求的id。使用它,您现在可以使用

抓取第B页上的$id = (int)$_GET['id'];
{{1}}

另请查看SQL Injection

答案 1 :(得分:1)

您应该在执行POST数据时使用此功能。

$test = $_POST['test'];

更新: 你的PageA的这一部分看起来很时髦

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

当你试图回复那里没有在上面的代码中设置的ID时,至少我找不到$ id的设置位置。

$ id在表单中设置,无法在表单标题中调用,因为当您提交表单/调用它时它还不存在。

您应该在表单操作中调用$ id之前设置$ id。

我还认为你应该在表单动作中调用$ id而不是id。

答案 2 :(得分:1)

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

和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