我们可以通过提交表单将ID发送到文件本身吗

时间:2019-02-09 03:55:28

标签: javascript php mysql

我试图发布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>

3 个答案:

答案 0 :(得分:1)

如果您尝试传递诸如查询字符串之类的参数,则无需使用表格。

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

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

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

答案 1 :(得分:1)

如果您需要向自己提交表单,可以使用

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" ....

或者您可以使用:

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST" ....

答案 2 :(得分:1)

您想使用JavaScript在PHP的 span GET 值。尝试这样的事情:

Error   1-'string' does not contain a definition for 'valueOf'  
Error   2-Argument 2: cannot convert from 'int' to 'System.IFormatProvider'
Error   3- The best overloaded method match for 'int.Parse(string, System.IFormatProvider)' has some invalid ar`enter code here`guments