[英]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
. 但插入
productid
像PF161202
其不加载shoppingcart.php
。 how can I pass values like PF161202
to php code through js. 如何通过js将
PF161202
值PF161202
给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.