根据选择值存储表单输入字段?

时间:2015-10-18 03:12:35

标签: javascript php jquery

每当用户访问注册页面并且当他选择他的大学但没有找到他的大学时,那么就会有"其他"选项...

当他点击"其他"显示另一个输入字段说明你的大学 现在问题是我不想存储"其他"数据库中的值 如果他从选择选项中选择他的大学,则应插入该值,但如果他选择"其他"选项然后他写入的值"指定"输入应存储在数据库中

这是我的代码

   <select class="job-purpose" name="college" id="college" >
   <option selected="selected" value="">-</option>
   <option value="1">
   NID - National Institute Of Design</option>
   <option value="2">
   NIFT - National Institute Of fashion Technology</option>
   <option value="3">
   Pearl Academy Of Fashion</option>
   <option value="4">
   SOFT - School Of Fashion Technology</option>
   <option value="5">
   Srishti Institute Of Design</option>
   <option value="6">
   MITID</option>
   <option value="7">
   Arch Academy Of Design</option>
   <option value="8">
   Satyam Fashion Institute</option>
   <option value="9">
   NIIFT</option>
  <option value="10">
  UID - United World Institute Of Design</option>
  <option value="11">
  International College Of Fashion </option>
  <option value="12">
  Raffles</option>

  <option value="other">
  other</option>
   </select>

这里是更改选项代码

    <div class="form-group" style="display:none;" id="coll">
    <div class="col-md-12">
    <div class="label-wrapper">
    <label class="control-label">College <span 
    class="vd_red">*</span></label>
    </div>

     <input type="text" name="coll" id="col" 
     placeholder="College Name">
     </div>
     </div>

这是Javascript代码

    < script type="text/javascript" >
 $('#college').change(function(){
  selection = $(this).val();    
   switch(selection)
   { 
   case 'other':
       $('#coll').show();
       break;
   default:
       $('#coll').hide();
       break;
   }
   });
 < /script >

PHP部分

    if(empty($looking)or empty($profession) or empty($experience)or    
    empty($current)or empty($state)or empty($jobtype)or empty($about)){ 

    if($college == 'others'){
    $coll= $_POST['coll'];
     unset($_POST['college']);
    $college= $coll;
     }

1 个答案:

答案 0 :(得分:0)

<?php
        $college = $_POST['college'];
        $coll = $_POST['coll'];
//this below line checks for is college selected and checks for the value selected not the "other" option
        if (isset($collage) && $collage != 'other') {
            //run your database query hear
        }elseif (isset($coll)) { //this line checks for the inputfiled coll isset if yes go to next line
            //run your database query hear
        }
    ?>