0

I'm creating small shopping-cart program, I'm using following code to input values.

<input type="button" value="Add to Cart : <?php echo $row['PART_NO']; ?>" onclick="addtocart('<?php echo $item; ?>')" /> 

value goes through following javascript code.

function addtocart(pid){
    alert(pid);
    document.form1.productid.value=pid;
    document.form1.command.value='add';
    document.form1.submit();
}

<body>
<form name="form1">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
</form>

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

?>

when I'm inserting productid like 02190249 it goes through javascript code and php code and loading the shoppingcart.php. but inserting productid like PF161202 its not loading the shoppingcart.php. how can I pass values like PF161202 to php code through js.

4
  • You are printing the body before you want to send a header. You need to send the header before you print anything. Put the PHP code above the HTML and JS code. Commented May 14, 2016 at 17:47
  • Have you tried the special value alert("I'm in your codez rekking your s***?")? I hear it gives some special results... Commented May 14, 2016 at 17:49
  • yes . special values working in alert, tried that. Commented May 14, 2016 at 17:53
  • It's beause you checked $_REQUEST['productid']>0 which is working fine with numbers but not for string. you have to remove this check. Commented May 14, 2016 at 17:56

1 Answer 1

1

Remove $_REQUEST['productid']>0 from your php code. It is checking only for numbers(02190249) which is greater then 0 but as per your question you are passing a string(PF161202).

<?php
if ( isset($_REQUEST['command']) && $_REQUEST['command'] == 'add' && ($_REQUEST['productid'] != '')){
    $pid=$_REQUEST['productid'];
    addtocart($pid,1);
    header("location:shoppingcart.php");
    exit();
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

the code works. but unable to print it using following code, $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $pname=get_product_name($pid); if($q==0) continue;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.