简体   繁体   中英

passing data from Javascript to Laravel controller

I'm getting the error of MethodNotAllowedHttpException when I run the below code:

<h1>Temp Form</h1>
<form method="post" action="" role="form">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="panel panel-default ">
    <div class="container">
      <div class="panel-body">
        <div class="form-group">
          <label for="firstName">First Name *</label>
          <input name="fname" type="text" class="form-control" id="firstName" placeholder="Enter First Name" required>
        </div>
        <div class="form-group">
          <label for="lastName">Last Name *</label>
          <input name="lname" type="text" class="form-control" id="lastName" placeholder="Enter Last Name" required>
        </div>
        <div class="form-group">
          <label for="qualification">Qualification *</label>
          <input name="qualification" type="text" class="form-control" id="qualification" placeholder="BE, MCA, MBA Etc." required>
        </div>
        <div class="form-group">
          <label for="emailAddress">Email address *</label>
          <input name="email" type="email" class="form-control" id="emailAddress" placeholder="Enter Email" required>
        </div>
        <div class="form-group">
          <label for="contactmessage">Message</label>
          <textarea name="desc" type="text" class="form-control" id="contactmessage" placeholder="Message" rows="2"></textarea>
        </div>
        <input type="submit" id="add" class="btn btn-primary" onclick="addUpdateData(id)" value="Add"></button>
      </div>
    </div>
  </div>
</form>

Code for routes.php :

Route::post('welcome/addupdate','FormController@addUpdateData');

code for controller :

 public function addUpdateData(Request $req)
 {
    $id = $req->input('id');
    if($id=="add")
    {
        $bs = new Basicusers;

        $bs->fname = $req->fname;
        $bs->lname = $req->lname;
        $bs->qualification = $req->qualification;
        $bs->email = $req->email;
        $bs->desc = $req->desc;

        $bs->save();

        return "Data Successfully Added";
    }
  }

What I want is, when user clicks on add button, value in variable data add is passed, and on controller, I will check value for variable and if its add , than I will perform add operation.

meanwhile if the user click on edit button which is provided below in form, that row will be filled in form elements and the add button is changed to update button and value of variable data will be now update and I want to perform update operation...

I'm getting error when I am passing data using POST method moreover I don't know how to get data passed using POST method..

for GET method I am using $id = Input::get('id'); method and its working

Here is JavaScript Function :

function addUpdateData(data) {
  $(function() {
    $.ajax({
      method: "post",
      url: "welcome/addupdate",
      data: {
        id: data
      },
      success: function(response) {
        alert(response);
      }
    });
  });
}

尝试在ajax请求中使用绝对路径:url:“ / welcome / addupdate”

Add a some ID for your form (ex. #form) and pass into the addUpdateData function serialized data from the form.

$('#add').on('click', function(e) {
  e.preventDefault();

  var data = $('#form').serialize();

  addUpdateData(data);
});

Add also a input field as hidden type into the form which should have an ID of the existing resource. Then on the controller action you can get an array of the passed data and if if the ID of the resource exists the perform update. If not then create them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM