未定义的索引:命令

时间:2013-09-11 08:15:23

标签: php javascript html

这些是我的代码而且我收到了错误

  

注意:第17行的E:\ xampp \ htdocs \ Shop_cart \ products.php中的未定义索引:命令

使用Javascript:

<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>  

HTML代码:

<form name="form1" action="">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
<input type="button" value="Add to Cart" onclick="addtocart(1)" />
</form>  

PHP代码:

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();

3 个答案:

答案 0 :(得分:3)

在您的PHP代码中添加isset检查条件:

if ( isset($_REQUEST['command']) && $_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 ) {}

答案 1 :(得分:0)

更改

 if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){

if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){

答案 2 :(得分:0)

与其他答案相同,但出于安全目的和更好的代码布局,我会按照这样的方式进行。

//Check if request has been made
if (isset($_REQUEST['command'])){

  //proceed for checking
  if($_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 )
  {
    //Your code here
  }

}else{
   //redirect or whatsoever
}