Reputation: 5105
I'm trying to make a POST ajax call that will take a form input value, pass it, through a route, to the contoller which will call an insert function. I've set my route, my insert function, my controller and my blade with the ajax call, but when I fill the input and hit the submit button, there is no response or error in the console and no record inserted into the database.
Maybe I'm totally overlooking something but is it clear what I'm missing or doing wrong here? Perhaps I should be debugging this differently as well?
Route.php
Route::post('insertFruit', 'Controller@insertFruit');
controller.php
public function insertFruit(Request $request)
{
if ($request->ajax()) {
$productComment = $request->productComment;
$fruitService = new fruitService();
$fruitService->insertListRecord($productComment);
}
}
fruitService.php
public function insertListRecord($productComment)
{
$link = DB::conn();
$sql = "CALL fruit.fruitInsert(?, ?)";
$exec = odbc_exec($link, $sql);
$prep = odbc_prepare($link, $sql);
if ($prep === false) {
$error = odbc_errormsg($link);
if (!empty($error)) {
throw new \Exception($error);
}
}
$exec = odbc_execute($prep, array($productComment));
if ($exec === false) {
$error = odbc_errormsg($link);
if (!empty($error)) {
throw new \Exception($error);
}
}
}
blade.php
<form id="productForm" class="uk-form uk-width-1-1">
<div class="uk-grid">
<div class="uk-width-1-1">
<label class="uk-form-label" for="">Comments:</label>
<textarea name="productComment" id="" cols="70" rows="5" placeholder="Optional"></textarea>
</div>
</div>
<div class="uk-grid">
<div class="uk-width-1-1 uk-text-center">
<button id="save" class="uk-button uk-button-primary uk-button-large" style="padding:0 50px;">Save</button>
</div>
</div>
</form>
$("#save").click(function(e){
e.preventDefault();
var productComment = $("input[name=productComment]").val();
$.ajax({
url:'/insertFruit',
data:{
productComment:productComment
},
"_token": "{{ csrf_token() }}",
type:"POST",
success:function(response){
console.log(response);
},
error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});
});
Upvotes: 2
Views: 363
Reputation: 1159
In your form's HTML, you'll need to set it's method to POST, since your route is expecting a POST method.
<form id="productForm" class="uk-form uk-width-1-1" method="post">
Upvotes: 1