javascript - 我们可以使用提交表单将 id 发送到文件本身吗
问题描述
我试图将 id 发布到 php 中。但它什么也没发布。我试过的这段代码
//if can it should echo 1 and point 232
<?php
if ($_POST['submit'] == 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners" onclick="location.href='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML'">
</form>
解决方案
如果您尝试传递查询字符串等参数,则不需要使用表单。
因为您将表单配置为发送发布请求,所以您可以将查询字符串的参数传递给隐藏类型的输入值。测试如下
<form action="reward-pollingx.php" enctype="multipart/form-data" method="post" style="width:auto;">
<input type="hidden" name="A" value="1">
<input type="hidden" name="POIN" value="">
<input type="hidden" name="submit" value="winners">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners">
</form>
<script>
document.querySelector('input[name=POIN]').value = document.getElementById('timespan').innerHTML;
</script>
您的 php 代码应更改为使用 $_POST 而不是 $_GET
<?php
if ($_POST['submit'] === 'winners') {
$a = $_POST['A'];
echo $a;
$point = $_POST['POIN'];
echo $point;
}
或者使用查询字符串将参数传递给 PHP 脚本
<a href="reward-pollingx.php?submit=winners&A=1&POIN=...">Submit</a>
和PHP
if ($_GET['submit'] === 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
要构建查询字符串,因为您需要从视图中获取信息,您可能需要动态构建它
推荐阅读
- javascript - 根据函数的返回值动态改变div的背景
- php - 无法连接到 SQL Server 本地主机 PHP 5.6
- c++ - 是否可以对任何语句字符串使用准备好的语句?
- .net - 使用事务范围的实体框架回滚
- angular - 如何沟通两个插座
- jbpm - jbpm 找不到人工任务的工作项处理程序
- javascript - node js中的异步编程通过mongoose模型传递常量/预定义的强制值
- sql - 在一个 firebird 过程中显示两个不相关且没有相互字段的选择查询
- python - 在 Python 中操作具有相同计数的元素的顺序
- django - Youtube 按钮在 django-CKEditor 中没有显示