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>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题