如何通过Java Script将发布的值获取到操作页面?
test.php:
<form action="/shop/cart.php" method="post">
<input type="hidden" name="price" value="300"/>
<div name="mysubmitbutton" id="mysubmitbutton" class="customButton" onclick="this.parentNode.submit()">
Button Text
</div>
</form>
cart.php:
<?php
if(isset($_POST['mysubmitbutton'])){
echo (int)$_POST['price'];
}
?>
但是因为我是用javascript发送的,所以在cart.php中没有任何输出,那么如何在cart.php中获取值300和回显呢?
发布于 2018-10-10 06:49:16
提交可以工作,但div
不是表单元素,值不会被发送,因此永远不会进入if
块。将php更改为:
if(isset($_POST['price'])){
echo (int)$_POST['price'];
}
发布于 2018-10-10 06:15:26
PHP在服务器上执行,而JS - in在浏览器上执行。您需要的是:
document.querySelector('form').addEventListener('submit', (e) => {
e.preventDefault();
console.log(e.currentTarget.querySelector('[name="price"]').value);
})
https://stackoverflow.com/questions/52730044
复制相似问题