简体   繁体   English

将数据从Javascript传递到Laravel控制器

[英]passing data from Javascript to Laravel controller

I'm getting the error of MethodNotAllowedHttpException when I run the below code: 运行以下代码时,出现MethodNotAllowedHttpException错误:

<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.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. 我想要的是,当用户单击添加按钮时,传递了变量data add中的值,并且在控制器上,我将检查变量的值,如果它是add ,则执行添加操作。

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... 同时,如果用户单击下面在表单中提供的edit按钮,则该行将被填充在表单元素中,并且添加按钮被更改为更新按钮,并且变量data值现在将被update并且我要执行更新操作。 。

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.. 此外,使用POST方法传递数据时出现错误,我不知道如何使用POST方法传递数据。

for GET method I am using $id = Input::get('id'); 对于GET方法,我正在使用$id = Input::get('id'); method and its working 方法及其工作

Here is JavaScript Function : 这是JavaScript函数:

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. 为您的表单(例如#form)添加一个ID,然后将表单中的序列化数据传递给addUpdateData函数。

$('#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. 还将一个输入字段作为隐藏类型添加到表单中,该表单应具有现有资源的ID。 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. 然后,在控制器操作上,您可以获取传递的数据的数组,如果资源的ID存在,则执行更新。 If not then create them. 如果没有,则创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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