首页 > 解决方案 > 我们可以使用提交表单将 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>

标签: javascriptphpmysql

解决方案


如果您尝试传递查询字符串等参数,则不需要使用表单。

因为您将表单配置为发送发布请求,所以您可以将查询字符串的参数传递给隐藏类型的输入值。测试如下

<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;
}

要构建查询字符串,因为您需要从视图中获取信息,您可能需要动态构建它


推荐阅读