doucong1853
2014-12-27 23:31
浏览 62
已采纳

将radiobutton中的两个变量插入数据库

Before Insert Three data from radiobutton, I have to display some data from database. three radiobuttons and other display data is the same place (in a form), as follows:

    <?php
    $fetchdata = $db->prepare("SELECT * FROM input_voucher ORDER BY id");
    $fetchdata->execute();
    $getAllz = $fetchdata->fetchAll();
    foreach ($getAllz as $rowz) {
      $ch1=$rowz['price1'];
      $ch2=$rowz['price2'];
      $ch3=$rowz['price3'];
    ?>

<form action="" method="post" name="form">
    <input name="var" id="voucher1" type="radio" value="1 Month">1 Month
    <span>USD.<input type="text" name="txtprice1" id="txtprice1" value="<?php echo $ch1; ?>" readonly="readonly"></span>

    <br />

    <input name="var" id="voucher2" type="radio" value="2 Month">2 Month
    <span>USD.<input type="text" name="txtprice2" id="txtprice2" value="<?php echo $ch2; ?>" readonly="readonly"></span>

    <br />

    <input name="var" id="voucher3" type="radio" value="3 Month">3 Month
    <span>USD.<input type="text" name="txtprice3" id="txtprice3" value="<?php echo $ch3; ?>" readonly="readonly"></span>

    <br />
    <input type="submit" class="btn btn-primary" name="btnsubmit" id="btnsubmit" value="Contiue">
    </form>

    <?php
    }
    ?>

Then, User must choose ONE CHOICE from the tree radiobuttons. When user check one of the buttons that means user also choose type of prices (price1,price2 or price3). The result from the choice picked is inserted into database, as follows:

UPDATED:

if (isset($_POST['btnsubmit'])){
   $var= strip_tags($_POST['var']);
   if ($var=='1 Month'){
      $numprice=strip_tags(isset($_POST['txtprice1']));
      }
   elseif ($var=='2 Month'){
      $numprice=strip_tags(isset($_POST['txtprice2']));
      }
   elseif ($var=='3 Month'){
      $numprice=strip_tags(isset($_POST['txtprice3']));
      }
      //insert now
      $mydata = $db->prepare("INSERT INTO voucher (colmonth,price) VALUES (:var,:numprice)");
      $mydata->execute(array(':var'=>$var,':numprice'=>numprice));
      echo "successfully saved!";
      header('location:setup.php');
      exit();
   }

But it saved "1", not price. What does it mean? How to make it right price?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqsw7529 2014-12-28 07:20
    已采纳

    finally I found the answer! I haven't declared yet $_POST for txtprice1,2 and 3. Here's the final:

    $txtprice1 = strip_tags(isset($_POST['txtprice1'])) ? strip_tags($_POST['txtprice1']) : '';
    $txtprice1=strip_tags($txtprice1);
    $txtprice2 = strip_tags(isset($_POST['txtprice2'])) ? strip_tags($_POST['txtprice2']) : '';
    $txtprice2=strip_tags($txtprice2);
    $txtprice3 = strip_tags(isset($_POST['txtprice3'])) ? strip_tags($_POST['txtprice3']) : '';
    $txtprice3=strip_tags($txtprice3);
    $var = strip_tags($_POST['var']);
    if ($var=='1 Month'){
        if (isset($_POST['txtprice1'])){
            $numprice=$txtprice1;
            }
        }
    elseif ($var=='2 Month'){
        if (isset($_POST['txtprice2'])){
            $numprice=$txtprice2;
        }
    }
    elseif ($var=='3 Month'){
        if (isset($_POST['txtprice3'])){
            $numprice=$txtprice3;
        }
    }
    
    点赞 评论
  • dongrui6787 2014-12-27 23:39

    Please define the problem you get: what errors appear? Any unexpected behaviour of the script? Anyway, afaik radio buttons of same group must have same name attribute, so name for all of your 3 radio buttons be like name="var". Also, $_POST['var'] == accepts value="" attribute of the radio button.

    As for saving price, you can just apply different values for each of the radio button and put that value into db

    UPD. About ur comment Lets say ur 3 radio buttons contain following:

    <input name="var" type="radio" value="0" />1 Month
    <input name="var" type="radio" value="1" />2 Month
    <input name="var" type="radio" value="2" />3 Month
    

    In your PHP script create following arrays:

    $subscription = array('1 Month', '2 Month', '3 Month');
    $price = array($ch1, $ch2, $ch3);
    $id = $_POST['var'];                        #if we selected 1 month, value will be '0'
    echo $subscription($id).' for '$price($id); #prints '1 Month for 10$'
    
    点赞 评论

相关推荐 更多相似问题