duanji7182 2019-02-13 05:04
浏览 56

从下拉列表选择中保存db中的多个字段

this is what i successfully done.

  1. User key in the data into the text field and preview back the data.

this is what i failed to do:

  1. capture back the data from db because the data that i select from drop down didn't store in the db. thats why i cannot print back the page after clicked submit.
  2. it works well for other field but not for my drop down selection. is there somethings wrong with my code for this part?

this is my index.html file

<tr>
    <td colspan="4" align="left" class="form-group form-inline"><b>Section 
     B</b></td>
    </tr>

    <script type="text/javascript">
                function show() { 

                var dropdown = document.getElementById("employment");
                var current_value = 
        dropdown.options[dropdown.selectedIndex].value;


                    if(current_value =="--Choose one--"){
                    didfv3.style.display = 'none';
                    didfv2.style.display = 'none';


                    }
                         if(current_value == "Unemployed"){
                        didfv3.style.display = 'none';
                        didfv2.style.display = '';


                        }

                         if(current_value == "Working"){
                        didfv3.style.display = '';
                        didfv2.style.display = 'none';

                        }

          }                     

                </script>





    <td>employment</td>
   <td colspan="4">
    <select name="employment" id="employment" onChange="show();">
    <option value="--Choose one--">--Choose one--</option>
        <option value="Unemployed">Unemployed</option>
        <option value="Working">Working</option>

       </select>



  <table id="didfv2" width="90%" border="0" align="center" 
  style="display:none">
<tr>
<td width="50%">Balance</td>
<td width="1%" align="center">:</td>
<td width="73%" ><input type="text" name="balance" readonly  maxlength="10" 
value="<?php echo $balance?>" size="5" >&nbsp;months</td>
      </tr>
</table>

    <table id="didfv3" width="90%" border="0" align="left" 
   style="display:none">

       <tr> 
    <td class="form-group form-inline">Basic Salary</td>
  <td width="0.5%" align="knk" class="form-group form-inline">:</td>
  <td width="73%" class="form-group form-inline">
  <input name="basic" type="text" id="basic" maxlength="20"/></td>
</tr>

<tr>    
    <td>Gross Salary</td>
  <td width="0.5%" align="center" class="form-group form-inline">:</td>
  <td width="73%" class="form-group form-inline">
  <input name="gross" type="text" id="gross" maxlength="20"/></td>
</tr>

 <tr>   
    <td class="form-group form-inline">Nett Salary</td>
  <td width="0.5%" align="center"  class="form-group form-inline">:</td>
  <td width="73%" class="form-group form-inline">
  <input name="nett" type="text" id="nett" maxlength="20"/></td>
</tr>
<tr> 
</table> 

here is my insert.php

  <?

$employment = $_POST['xxx'];

if (isset($_POST['xxx'])) {
     $employment =$_POST['xxx'];
    }   

if($employment =='Working'){

      $basic = $_POST['basicsalary00'];
      $gross = $_POST['grosssalary00'];
      $nett = $_POST['nettsalary00'];

}

    elseif($xxx =='Unemployed'){

    $balance = $_POST['balance'];
} 


$sqlinsert = "INSERT INTO application
(
xxx,basicsalary00,grosssalary00,nettsalary00)
VALUES(0,'$xxx','$basic','$gross','$nett')";

?>
  // here is to print back the output

<form name="formName" action="form1" method="post" class="form-group form- 
inline">
 <tr>
    <td rowspan="2">Employment</td>
    <td colspan="3">
        <option value="<?php echo $employment;?>"><?php echo $employment;?> 
 </option>        

    <table id="didfv2" width="90%" border="0" align="center" 
style="display:none">

    <tr>
    <td colspan="3">
    <input type="hidden" id="balance" value="<?php echo $balance;?>" />
    </td>
    </tr>
        </table>



  <table id="didfv3" width="90%" border="0" align="center" 
  style="display:none">
   <tr></tr>
   <tr>
    <td>Basic Salary</td>
    <td>
  <input name="basic" type="hidden" id="basic" value="<?php echo $basic;?>" />RM <?php echo $basic;?></td>
    </tr>

    <tr>
    <td>Gross Salary</td>
  <td>
  <input name="gross" id="gross" type="hidden" value="<?php echo $gross;?>" 
 />RM <?php echo $gross;?></td>
    </tr>


   <tr>
   <td>Nett Salary</td>
   <td>
  <input name="nett" type="hidden" id="nett" value="<?php echo $nett;?>" 
  />RM <?php echo $nett;?></td>
    </tr>
        </table>
  </td>
    </tr>
  </form>

So there will be 4 pages,

1) for user input (index.html)

2) Preview back the input (i didnt include the file in here - successful)

3) Insert the input to db (insert.php)

4) Print back the result (code at the bottom of the insert.php...fetch from db- which i failed. Working for other field but not for drop down section)

I really need your help.. i've been stuck here for a week.... thank you

  • 写回答

1条回答 默认 最新

  • 普通网友 2019-02-13 08:52
    关注

    A proper dropdown should have a <select> and the <option> elements should be inside it!

    In your insert.php, I can see that you have only used this line:

    <option value="<?php echo $employment;?>"><?php echo $employment;?></option>  
    

    How about your try it like this:

    <select readonly>
      <option value="--Choose one--">--Choose one--</option>
      <option <?php echo ($employment == 'Unemployed') ? 'selected' : ''; ?> value="Unemployed">Unemployed</option>
      <option <?php echo ($employment == 'Working') ? 'selected' : ''; ?>  value="Working">Working</option>
    </select>
    

    Here, we are checking whether any of those <option> element is same as the one that the user has submitted. If so, make it as selected

    Hope it helps.


    EDIT

    If you want to store the value of $employment to display it later, you definitely have to store it in your db.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP