简体   繁体   English

通过JavaScript将值传递给php

[英]Passing values through javascript to php

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. 值通过以下javascript代码进行。

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. 当我插入productid喜欢02190249它通过JavaScript代码和PHP代码,并加载shoppingcart.php。 but inserting productid like PF161202 its not loading the shoppingcart.php . 但插入productidPF161202其不加载shoppingcart.php how can I pass values like PF161202 to php code through js. 如何通过js将PF161202PF161202给php代码。

Remove $_REQUEST['productid']>0 from your php code. 从您的php代码中删除$_REQUEST['productid']>0 It is checking only for numbers (02190249) which is greater then 0 but as per your question you are passing a string (PF161202). 它仅检查是否大于0 numbers (02190249),但是根据您的问题,您正在传递string (PF161202)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM