从输入中检索数据-Laravel

时间:2018-06-21 12:25:21

标签: javascript laravel

使用下面的代码,当我提交表单时,数据库中的名称和电子邮件字段将保存为空。这表明即使我使用正确的name elements作为输入,我也无法检索输入。

我在这里做错了什么?   HTML

  <form  action="/" class="fixed-form icons-tab-steps wizard-circle">
                  <!-- Step 1 -->
                  <h6><i class="step-icon ft-user"></i> Step 1</h6>
                  <fieldset>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group">
                          <label for="firstName2">Full Name :</label>
                          <input type="text" class="form-control square" id="name" value="{{$current_user->name}}" name="fullname">
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group">
                          <label for="lastName2">Email Address :</label>
                          <input type="text" class="form-control square" value="{{$current_user->email}}" id="lastName2" name="email">
                        </div>
                      </div>

                  <!-- Step 4 -->
                  <h6><i class="step-icon ft-layout"></i>Step 4</h6>
                  <fieldset>
                  <div class="row">
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="firstName2">Your Company :</label>
                          <input type="text" class="form-control square" id="name" name="company">
                        </div>
                      </div>                      
                    </div>

                      <div class="col-md-6">
                        <div class="form-group">
                          <label for="location2">Location :</label>
                          <input type="text" class="form-control square" id="name" name="location">

                        </div>
                      </div>

                  </fieldset>
                </form>

JS

$(".icons-tab-steps").steps({
    headerTag: "h6",
    bodyTag: "fieldset",
    transitionEffect: "fade",
    titleTemplate: '<span class="step">#index#</span> #title#',
    labels: {
        finish: 'Submit'
    },
    onFinished: function (event, currentIndex) {

        alert("Forms submitted.");

        $.ajax({
            method: 'GET', // Type of response and matches what we said in the route
            url:'/user/update/2',
            success: function(response){ // What to do if we succeed
                console.log("paased"); 

            },
            error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
               console.log(JSON.stringify(jqXHR));
               console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
               console.log("failed"); 

           }
           });
    }

});


// Validate steps wizard

// Show form
var form = $(".steps-validation").show();

$(".steps-validation").steps({
    headerTag: "h6",
    bodyTag: "fieldset",
    transitionEffect: "fade",
    titleTemplate: '<span class="step">#index#</span> #title#',
    labels: {
        finish: 'Submit'
    },
    onStepChanging: function (event, currentIndex, newIndex)
    {
        // Allways allow previous action even if the current form is not valid!
        if (currentIndex > newIndex)
        {
            return true;
        }
        // Forbid next action on "Warning" step if the user is to young
        if (newIndex === 3 && Number($("#age-2").val()) < 18)
        {
            return false;
        }
        // Needed in some cases if the user went back (clean up)
        if (currentIndex < newIndex)
        {
            // To remove error styles
            form.find(".body:eq(" + newIndex + ") label.error").remove();
            form.find(".body:eq(" + newIndex + ") .error").removeClass("error");
        }
        form.validate().settings.ignore = ":disabled,:hidden";
        return form.valid();
    },
    onFinishing: function (event, currentIndex)
    {
        form.validate().settings.ignore = ":disabled";
        return form.valid();
    },
    onFinished: function (event, currentIndex)
    {
        alert("Submitted!");
    }
});

// Initialize validation
$(".steps-validation").validate({
    ignore: 'input[type=hidden]', // ignore hidden fields
    errorClass: 'danger',
    successClass: 'success',
    highlight: function(element, errorClass) {
        $(element).removeClass(errorClass);
    },
    unhighlight: function(element, errorClass) {
        $(element).removeClass(errorClass);
    },
    errorPlacement: function(error, element) {
        error.insertAfter(element);
    },
    rules: {
        email: {
            email: true
        }
    }
});

控制器

$user = User::whereId($id)->firstorFail();
        $user->name = $request->get('fullname');
        $user->email = $request->get('email');
        $user->save();

3 个答案:

答案 0 :(得分:1)

检索输入参数的正确语法是使用方法input,而不是get

$request->input('fullname')

此外,尝试使用ddvar_dump等方法调试代码,以查看变量包含的内容。例如:

$email = $request->input('email');
dd($email);

除此之外,还请确保您要分配它们的模型上的属性emailname$fillable

答案 1 :(得分:0)

  1. 我可以看到您公司的ID和位置字段都是名称。这将在javascript中失败。因此,将其更改为
  2. 我不确定您是否要向ajax函数发布任何数据。 你应该做类似的事情 $.ajax({ method: "POST", url: "some.php", data: { name: $('name').val(), location: $('email').val() } })

最后,您的方法应该是发布而不是获取

答案 2 :(得分:0)

首先使用jquery获取全名和电子邮件$('#name').val()的值

调整get方法以匹配以下内容:

$.ajax({
        method: 'GET', // Type of response and matches what we said in the route
        url:'/user/update/2?fullname=' + fullname + '&email=' + email
        success: function(response){ // What to do if we succeed
            console.log("paased"); 

        },
        error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
           console.log(JSON.stringify(jqXHR));
           console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
           console.log("failed"); 

       }
       });

在那之后,您应该调整控制器中的功能以使用输入代替get

$request->input('fullname');