1

These are my codes And i'm getting error

Notice: Undefined index: command in E:\xampp\htdocs\Shop_cart\products.php on line 17

Javascript:

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

HTML Code:

<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 Code:

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

3 Answers3

3

In your PHP code add isset check to the condition:

if ( isset($_REQUEST['command']) && $_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 ) {}
hsz
  • 148,279
  • 62
  • 259
  • 315
0

Change

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

to

if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
rakeshjain
  • 1,791
  • 11
  • 11
0

Same to other answer but I would literally proceed something like this for security purposes and better layout of my code.

//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
}
Drixson Oseña
  • 3,631
  • 3
  • 23
  • 36