dousilie9522 2015-07-27 01:59
浏览 22

如何从输入数组中选择所选值

I have 7 input fields. However the user may only pick a maximum of 3 subjects. How would I be able to update these inputs into my table?

public function searchgrouppost(){

         $subject1 = Input::get('subject1');
         $subject2 = Input::get('subject2');
         $subject3 = Input::get('subject3');
         $subject4 = Input::get('subject4');
         $subject5 = Input::get('subject5');
         $subject6 = Input::get('subject6');
         $subject7 = Input::get('subject7');

My aim is to end up with something like this:

DB::table('mytable')->where('id', 6)
                    ->update(['subject' => $firstchosensubject,
                           'subjecttwo' => $secondchosensubject, 
                           'subjectthree' =>$thirdchosensubject]);

So far I have:

$subjects = [];
$inputs = Input::all();
array_walk($inputs, function ($v, $k) use (&$subjects) {
if(starts_with($k, 'subject')) {
    $subjects[$k] = $v;
}
});

$index = count($subjects);

Shown above I have all inputs in this $subjects variable. How do I extract these values for table input?

  • 写回答

3条回答 默认 最新

  • douqufan9148 2015-07-27 02:07
    关注

    I would suggest validating the inputs on the front end rather than trying to do with php. Here is some JQuery that would get you on your way

    var numSubjects = 0;
    $(".inputs").each(function(i, obj) {
        if($(this).val() != ""){
            numSubjects++;
        }
    });
    
    if(numSubjects > 3){
        // let the user know
    }else{
        // submit the form
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类