0

I have two contact forms on same page (bootstrap modal window). The problem is that one of them doesn't work correctly and return 404 Not Found error.

I have this in web.php

Route::post('/b2b_contact','HomeController@b2b_contact')->name('b2b_contact');
Route::post('/','HomeController@contact')->name('contact');

HomeController.php functions

public function contact(Request $request)
{ 

    $data = array(
        'first_name' => $request->first_name,
        'company_name' => $request->company_name,
        'country'  => $request->country,
        'email'  => $request->email,
    );

    DB::table('contact')->insert($data);         
    return Redirect::to('/')->with('message', 'Thank you!');
}

public function b2b_contact(Request $request)
{ 

    $data = array(
        'first_name' => $request->first_name,
        'company_name' => $request->company_name,
        'country'  => $request->country,
        'email'  => $request->email,
    );

    DB::table('b2b_contact')->insert($data);         
    return Redirect::to('/')->with('message', 'Thank you!');
}

And what I have in the blade template are the forms

{{Form::open(array('route'=>'b2b_contact','files' => true,'method'=>'post', ))}}

// input fields here

{{Form::close()}}  

{{Form::open(array('route'=>'contact','files' => true,'method'=>'post', ))}}

// input fields here

{{Form::close()}}  

The second form contact works perfectly but the b2b_contact return Error 404.

I've also tried to make both routes in web.php like Route::post('/',.... but then the page return error that b2b_contact route isn't find.

So, can anyone help me here how to have 2 contact forms on same page and when they are submitted to reload the page and show the success message?

UPDATE: this is what I see when I submit the page and yes, debug is set to true

Not Found

The requested URL /applyNow was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

UPDATE 2: .htaccess file in public directory

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
S.I.
  • 3,250
  • 12
  • 48
  • 77
  • Anything in Laravel logs? – Tarasovych Apr 12 '19 at 12:08
  • Nothing in the log – S.I. Apr 12 '19 at 12:13
  • Do you have debug enabled in `.env`? Can you put `dd(1)` at the top of `b2b_contact` method and retry to submit a form? Than tell me if you get `1` in browser. – Tarasovych Apr 12 '19 at 12:15
  • I've updated my question with screenshot of the page. – S.I. Apr 12 '19 at 12:19
  • Do you have more routes, specifically a `GET` route for `/` – Thomas Apr 12 '19 at 12:19
  • Yes, one more - `Route::get('/','HomeController@index');` for the main page – S.I. Apr 12 '19 at 12:20
  • 1
    The error doesn't look like it went through laravel (perhaps a wrong domain). Also the URL is `/applyNow` which isn't visible in your code. How/Where are you navigating to `/applyNow`? – Thomas Apr 12 '19 at 12:23
  • I just tried to change the `b2b_contact` .. just in case if Laravel doesn't like it – S.I. Apr 12 '19 at 12:24
  • The URL is `http://localhost/b2b_contact` is not wrong. I mean I don't have blade with that name, but I don't think I need one since I redirect back to home page – S.I. Apr 12 '19 at 12:27
  • But your screenshot says `The requested URL /applyNow was not found on this server` – Thomas Apr 12 '19 at 12:30
  • It was just for a test, I've changed `b2b_contact` everywhere with `applyNow` and after that I've put it back `b2b_contact`. – S.I. Apr 12 '19 at 12:31
  • I've tried to put `index.php` in the url `Route::post('/index.php/b2b_contact','HomeController@b2b_contact')->name('index.php/b2b_contact');` and I've got the native laravel 404 error page, but I don't see the 1 from `dd(1)` on it. – S.I. Apr 12 '19 at 12:37
  • What is your application root directory? Are you executing laravel via Apache or `php artisan serve`? – Johannes Apr 12 '19 at 12:45
  • Okay, I've found something strange. If I load the site with this URL `https://example.com/index.php` and then open the form and submit it -> it's works perfectly. If I open the site without index.php `https://example.com/` and submit the form, I've got 404 error. – S.I. Apr 12 '19 at 12:46
  • it is via Apache – S.I. Apr 12 '19 at 12:48
  • You need to adapt Apache in order to allow Laravel's router to work: https://stackoverflow.com/questions/49184358/setting-document-root-for-laravel-project-on-apache-virtual-host – Johannes Apr 12 '19 at 12:51
  • What happens if you run your project with `php artisan serve` from the command line? It's working then, right? – Johannes Apr 12 '19 at 12:53
  • @Johannes, no, error 500 because `[Fri Apr 12 15:53:45 2019] PHP Warning: require_once(/var/www/html/public/index.php): failed to open stream: No such file or directory in /var/www/html/server.php on line 21` – S.I. Apr 12 '19 at 12:55
  • But what about if on shared hosting for example godaddy.. the apache should be okay there? – S.I. Apr 12 '19 at 12:58
  • How is your directory called ? – Johannes Apr 12 '19 at 14:45
  • Have a look at this .htaccess example here: https://laravel.com/docs/master#web-server-configuration – Johannes Apr 12 '19 at 15:12

1 Answers1

1

I tried your setup locally with Laravel 5.8 and XAMPP. When I copy the Laravel project to /htdocs then my path to the Laravel home page is /htdocs/public. In the browser I have to enter http://localhost/public/ or I need to change the Apache config to, for example, provide the public folder via laravel.local.

Having this setup described above and clicking on the form link your browser tries to POST to this URL: http://localhost/b2b_contact - this is not existing because we need to POST to http://localhost/public/b2b_contact.

Why is it working for /contact and not for /b2b_contact?

In your routes file you define the routes as follows:

Route::post('/b2b_contact','HomeController@b2b_contact')->name('b2b_contact');
Route::post('/','HomeController@contact')->name('contact');

The /contact route isn't even called contact but / - that is your vhosts root directory. According to your comment on your initial post your Laravel installation (vendor folder etc.) seems to be located inside of /var/www/html and you are calling your public directory. I'm not 100% sure about this but please try to fix your directory structure.

Johannes
  • 1,478
  • 1
  • 12
  • 28