dongyi1524 2014-11-18 15:18
浏览 63
已采纳

提交表单到行动php文件

I have a form where when the user clicks submit, I need a php file to be ran. below is the form and the php file.

<form action="php_scripts/test.php" method="POST">
        <input name="feature"     type = "text"     placeholder="Feature"   /> 
        <input name="feature2"   type = "text"  placeholder="Feature2"  /> 
        <input type="submit" value = "submit"/>
</form>

test.php

<?php

    if( isset($_GET['submit']) )
    {
        $feature = $_POST['feature'];
        // do stuff (will send data to database)
    }
?>

The problem I am having is that when I press Submit on the form,

if( isset($_GET['submit']) )

Always returns false.

Can anyone explain why that is? Have I totally misunderstood how to implement form sending data to php scripts?

Apologies if I have made any syntax errors and many thanks for any help you can give.

  • 写回答

3条回答 默认 最新

  • doushengyou2617 2014-11-18 15:28
    关注

    There are a few things wrong with your code.

    You're mixing GET with POST methods. Plus, add values to your inputs and your submit button isn't named, which you're trying to use as a conditional statement for.

    HTML

    <form action="php_scripts/test.php" method="POST">
            <input name="feature"  value="feature" type = "text" placeholder="Feature" /> 
            <input name="feature2" value="feature2" type = "text"  placeholder="Feature2"  /> 
            <input type="submit" name="submit" value = "submit"/>
    </form>
    

    PHP

    <?php
    
        if( isset($_POST['submit']) )
        {
            $feature = $_POST['feature'];
            $feature2 = $_POST['feature2'];
            // do stuff (will send data to database)
        }
    ?>
    

    Sidenote: You could/should also check against empty values.

    if(isset($_POST['submit']) 
        && !empty($_POST['feature']) 
        && !empty($_POST['feature2']) ) {...}
    

    Footnotes:

    Seeing that you're intending on sending to DB:

    I hope you plan on using mysqli with prepared statements, or PDO with prepared statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题