2

I want the user to be notified when there is a validation error from php. i am using laravel 5.3 to create the form and Ajax to get the error message.

here is my JavaScript code

 $.ajax({
    url: '/artistPost',
    type:'POST',
    data: {
        _token: Artisttoken,
        Name: Name,
        Phone: Phone,
    },

     error:function (data, response, xhr, status, error) {
        var errors = data.responseJSON;
        console.log(errors);
    }

this is what i am seeing in the Network section of the Chrome deveper tools

<?php{"name":["The name field is required."],"phone":["The phone field is required."]}

and this is what I am seeing in the consoleenter image description here

Update

this is from the controller

 public function create(ArtistRequest $request){

    dd($request->all());


    return redirect()->back();
}
5
  • Is that all of your php code for artistPost? or is it the output? Commented Feb 25, 2017 at 14:48
  • its the output when I click on Save Commented Feb 25, 2017 at 14:51
  • Can we see the code that produces that output? Commented Feb 25, 2017 at 14:51
  • the "/artistpost" url invokes the "create()" function which i have updated in my question Commented Feb 25, 2017 at 15:06
  • show the code of ArtistRequest Commented Feb 27, 2017 at 11:32

2 Answers 2

1

Your server should not be sending <?php as part of it's response - this either suggests that the server is not parsing the file as PHP, or you are echoing a <?php tag somehow.

This post seems to be addressing a very similar issue:

https://laracasts.com/discuss/channels/general-discussion/php-tag-in-ajax-response?page=1

Look in particular at this response:

RouteServiceProvider.php contains:

public function map(Router $router) { $router->group(['namespace' => $this->namespace], function ($router) { foreach (File::allFiles(app_path('Http/Routes')) as $partial) { require_once($partial->getPathname()); } }); }

One of the route files loaded by the above method currently has nothing in it except for a <?php tag and NO NEW LINE. The absence of a new line was the problem. Added a comment to the file and all works perfectly. I don't know why this was more of a problem on the production server than locally. The PHP versions are both 5.6.

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

Comments

0

You are supposed to return a json output from the controller. Eg:

    return Response::json(['message'=> 'Your message', 'status'=> 'success'], 200);

You should have an if/else so you can capture success and error.

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.