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 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路