1

With my code below, when i submit the form, the name and email field in the database is saved empty. This tells i am not able to retrieve the inputs even though i am using the right name elements for the inputs.

What am i doing wrong here? 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
        }
    }
});

Controller

$user = User::whereId($id)->firstorFail();
        $user->name = $request->get('fullname');
        $user->email = $request->get('email');
        $user->save();
2
  • Have you checked whether data is being posted correctly to the controller. Do dd($request->all()) and check name and email are empty or not Commented Jun 21, 2018 at 12:29
  • superrr late but, html elements id and name attributes are not matching. Commented Jun 8, 2021 at 17:35

3 Answers 3

1

THe correct syntax for retrieving input parameters is using the method input, not get:

$request->input('fullname')

Also, try to debug your code with methods like dd, var_dump etc to see what the variable contain. Ex:

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

Except from this, also make sure that the properties email and name are $fillable on the model you are trying to assign them.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. I can see that the ids of your company and location fields are both name. This will fail in javascript. So, change it to
  2. I'm not sure you're posting any data to you ajax function. You should do something like $.ajax({ method: "POST", url: "some.php", data: { name: $('name').val(), location: $('email').val() } })

and lastly, your method should be post and not get

Comments

0

First of all using jquery get the value of fullname and email $('#name').val()

Adjust your get Method to match the following:

$.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"); 

       }
       });

After that you should adjust the function in the controller to use input instead of get

$request->input('fullname');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.