donglue1886 2017-05-13 22:57
浏览 47
已采纳

PHP根据选择提交表单

I currently have a simple input form page with a submit button. I would like to have that submit button write data into my mysql databased based on one of the 2 selections the user has.

For example, 2 of the selections are: X and Y. If a user has selected the X option, I would like to have all the text/form inputs written/inserted into the X table of my database instead of the Y, and vice versa.

I'm relatively new to web development so any advice would be immensely helpful as this is a small project I'm helping develop for my workplace.

HTML

<!--option buttons-->
<div class="row text-center">
    <div class="col-lg-4">
      <span class="fa-stack fa-3x">
          <i class="fa fa-circle fa-stack-2x text-primary"></i>
          <i class="fa fa-plus fa-stack-1x fa-inverse"></i>
      </span>
    </div> <!--first X selection button here-->
    <div class="col-lg-4">
      <span class="fa-stack fa-3x">
          <i class="fa fa-circle fa-stack-2x text-reading"></i>
          <i class="fa fa-book fa-stack-1x fa-inverse fa-custominverse"></i>
      </span>
    </div>
</div> <!--second Y selection button here-->

PHP

<?php
require('db_connect.php');
require('auth.php');

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$homework = $_POST['Xselection'];
$classwork = $_POST['Yselection'];

$sql = "INSERT INTO "" ....

?>

Thanks!

Edit: here is the full code

https://pastebin.com/16ZjxbDL

my question is- how do i make it so that the 2 options for "math" and "reading" are buttons/ selection which would correspond to writing into their separate respective tables in my database?

  • 写回答

1条回答 默认 最新

  • doulian1852 2017-05-13 23:57
    关注

    1. For example, you can use radiobuttons to let the user make a selection between two values:

    <form action="" method="post">
        <label>Firstname</label><input type="text" name="firstname">
        <label>Lastname</label><input type="text" name="lastname">
        <label>X</label><input type="radio" checked="checked" name="selection" value="x">
        <label>Y</label><input type="radio" name="selection" value="y">
        <button type="submit" name="save" value="save">Save</button>
    </form>
    
    <?php
        if (isset($_POST['save'])) {
            $firstname = $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $selection = $_POST['selection'];
            
            if ($selection == 'x') {
                $table_name = 'table_x';
            } elseif ($selection == 'y') {
                $table_name = 'table_y';
            }
            $sql = 'INSERT INTO '.$table_name.' () VALUES ()';
        }
    ?>

    2. Another way is by using input type select, or

    3. by having two submit buttons with different values.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?