我需要传递 JavaScript 值并通过 PHP 将它们插入数据库。我已经尝试过该 URL,但它不起作用。
需要插入两个变量。
数据库中的所有值都打印在表格中,然后我进行选择。
这个文件的名字是items.php。
<td><input type="text" class="form-control" id="quantity" placeholder="" ></td>
<td><center><button type="submit" onclick="myFunction()" value="<?php echo 'R-' . $id_print ;?>" id="item"> <i class="icon-cart"></i></button></center></td>
我使用这个脚本来获取值:
<script type="text/javascript">
function myFunction() {
var x = document.getElementById("quantity").value;
var y = document.getElementById("item").value;
window.location.href = "items?w1=" + x + "&w2=" + y;
}
</script>
我不知道为什么脚本不使用脚本中的值重定向到另一个 URL。
我在调试时得到了两个值。
最佳答案
如果 <button>
元素有 type="submit"
,单击它会提交表单,因此会阻止执行分配给此元素的 JavaScript 单击事件处理程序。你应该改变它的 type
至 button
, 所以它不会提交表单。
更正后的代码:
<td><input type="text" class="form-control" id="quantity" placeholder="" ></td>
<td><center><button type="button" onclick="myFunction()" value="<?php echo 'R-' . $id_print ;?>" id="item"> <i class="icon-cart"></i></button></center></td>
关于javascript - 将 JavaScript 值传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790678/