2

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);
         }
    });

});
8
  • What does your form's HTML look like? Commented Sep 5, 2018 at 16:59
  • My bad, it's in the blade now Commented Sep 5, 2018 at 17:02
  • What happens if you remove the if ($request->ajax()) { check? Commented Sep 5, 2018 at 17:13
  • @Adam the same unfortunately Commented Sep 5, 2018 at 17:15
  • Any error logs filling up at this point or the stacktrace error page? Commented Sep 5, 2018 at 17:18

1 Answer 1

1

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">
Sign up to request clarification or add additional context in comments.

7 Comments

Good point, I did that and I do now get a 500 error in the console
Then it's likely any issue in the controller.php or fruitService.php at this point. Will update answer if I track it down.
SO the ajax is passing with a 200, is there a better way for me to return an error from my prepare or execute?
Haven't worked with ODBC much, but this may help? php.net/manual/en/function.odbc-errormsg.php - Though in my experience, it doesn't always return the previous message (var_dumping sometimes is blank).
I think it's an issue with the form data. It needs to be passed as a string in single quotes like 'category' . Is there something I should be doing to make sure it passes it that way?
|

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.