rickyspires's avatar

two form on one page both submit.

Hello.

I have a login and register for on the same page but if i click signup or signin the the valadation fails because it seems to be looking at both forms ???

Please help. Thanks

this is my form:

@extends('layouts.app')
<!-- meta title -->
@section('title')
    home page
@endsection

@section('content')

 @if(count($errors) > 0 )
        <div class="row">
        <div class="col-md-4 col-md-offset-4">
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
        </div>
    </div>
    @endif
    <div class="row">

    <div class="col-md-6">
        <h3>Sign up</h3>


        <form action="{{ url('/user_signup') }}" method="post"> <!-- conent to the signup route -->
            {!! csrf_field() !!}
            <div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}">
                <label for="email">Your E-mail</label>
                <input class="form-control" type="text" name="email" id="email" value="hello1{{ Request::old('email')}}">
            </div>
            <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
                <label for="name">Your Name</label>
                <input class="form-control" type="text" name="name" id="name" value="hello2{{ Request::old('name')}}">
            </div>
            <div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}" >
                <label for="password">Your Password</label>
                <input class="form-control" type="password" name="password" id="password" value="hello3{{ Request::old('password')}}">
            </div>
            <button type="submit" name="signup" class="btn btn-primary">Signup</button>      
        </form>
    </div>     

    <div class="col-md-6">
        <h3>Login</h3>
        <form action="{{ url('/user_signin') }}" method="post">
            {!! csrf_field() !!}
            <div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}">
                <label for="email">Your E-mail</label>
                <input class="form-control" type="email" name="email" id="email" value="{{ Request::old('email')}}">
            </div>
            <div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}">
                <label for="password">Your Password</label>
                <input class="form-control" type="password" name="password" id="password" value="{{ Request::old('password')}}">
            </div>
            <div class="form-group">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="remember"> Remember Me
                    </label>
                </div>
            </div>

            <button type="submit" name="signin" class="btn btn-primary">Login</button>
            <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>

        </form>
    </div>   

    </div>

@endsection

this is my usercontroller:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class UserController extends Controller
{

  //Register
  public function postUserSignUp(Request $request){

     //validate
     $this->validate($request, [
        'email' => 'email|unique:users|required',
        'name' => 'max:120|required',
        'password' => 'min:4|required'
    ]);

    //get fields from signup form using $request
    $email = $request['email'];
    $name = $request['name'];
    $password = bcrypt($request['password']);

    //create new user - use App\User;
    $user = new User();
    $user->email = $email;
    $user->name = $name;
    $user->password = $password;
    $user->save(); //save to the db.

    Auth::login($user);//login after register

    return redirect()->route('user_dashboard');
  }

//Sing in
public function postUserSignIn(Request $request){

    $this->validate($request, [
        'email' => 'email|unique:users|required',
        'password' => 'min:4|required'
    ]);

    if (Auth::attempt([ 'email' => $request['email'], 'password' => $request['password'] ])){
        return redirect()->route('user_dashboard');
    }
    return redirect()->back();
}

//After signup or signin redirect to the dashboard
public function getUserDashboard(){
    return view('user_dashboard');
}
}

these are my routes:

Route::group(['middleware' => 'web'], function () {

//Index page
Route::get('/', 'HomeController@index');

//User sign up
Route::post('/user_signup', [
    'uses'=>'UserController@postUserSignUp',
    'as'=>'user_signup'
]);
//User sign up
Route::post('/user_signin', [
    'uses'=>'UserController@postUserSignIn',
    'as'=>'user_signin'
]);
//After signup or signin redirect to the dashboard
Route::get('/user_dashboard', [
    'uses'=>'UserController@getUserDashboard',
    'as'=>'user_dashboard'
]);
});
0 likes
19 replies
colourmill's avatar

You forgot to include the most important part, what is the posted data and what is the validation error?

rickyspires's avatar

hello.

thank you for your reply.

there are to email forms on the same page. 1 for the signin and on for the signup. if i try to sign in both email input fail and i get an error saying that email already exists in the database. so the login is failing because it is also trying to register.

Does that help?

rickyspires's avatar

please note a change in the code above.

<form action="{{ url('/user_signup') }}" and <form action="{{ url('/user_signin') }}"

have been changed to

<form action="{{ route('user_signup') }}"  and    <form action="{{ route('user_signin') }}"

i still have the same errors.

thanks

1 like
colourmill's avatar

So are there 2 requests happening? You should be able to tell what data is being posted by looking at your developer tools.

rickyspires's avatar

do you mean console log ? Sorry not sure what to look for.

Also sometimes i am getting TokenMismatchException in VerifyCsrfToken.php line 67: but not always

Snapey's avatar

@YassineMM Why have you created an account just to spam some very old posts with nonsense content?

bashy's avatar

Make sure to add in the csrf_token() field and use a value/name on the button.

View

<form action="/url" method="post">
    <input name="some_field" value="1">

    <button type="submit" name="form1">Submit 2</button>
</form>

<form action="/url" method="post">
    <input name="some_field" value="1">

    <button type="submit" name="form2">Submit 2</button>
</form>

// or you can put this into one form if your fields are the same but want a delete/vote/queue type button.

Controller

if ($request->has('form1') {
    return 'form1 was submitted';
}

if ($request->has('form2') {
    return 'form2 was submitted';
}
rickyspires's avatar

Hello.

No luck :(

i have :

<button type="submit" name="signup" class="btn btn-primary">Signup</button> 

and

<button type="submit" name="signin" class="btn btn-primary">Signup</button> 

then in the controller i did this:

<?php

namespace App\Http\Controllers;

use App\Http\Requests; use App\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth;

class UserController extends Controller {

//Register
public function postUserSignUp(Request $request){

    if ($request->has($request['signup'])) {

        //validate
        $this->validate($request, [
            'email' => 'email|unique:users|required',
            'name' => 'max:120|required',
            'password' => 'min:4|required'
        ]);

        //get fields from signup form using $request
        $email = $request['email'];
        $name = $request['name'];
        $password = bcrypt($request['password']);
       

        //create new user - use App\User;
        $user = new User();
        $user->email = $email;
        $user->name = $name;
        $user->password = $password;
        $user->save(); //save to the db.

        Auth::login($user);//login after register

        return redirect()->route('user_dashboard');
    }else{
        echo "error1";
    }
}

//Sing in
public function postUserSignIn(Request $request){

    if ($request->has($request['signin'])) {

        $this->validate($request, [
            'email' => 'email|unique:users|required',
            'password' => 'min:4|required'
        ]);

        if (Auth::attempt([ 'email' => $request['email'], 'password' => $request['password'] ])){
            return redirect()->route('user_dashboard');
        }
        return redirect()->back();
    }else{
        echo "error2";
    }
}

//After signup or signin redirect to the dashboard
public function getUserDashboard(){
    return view('user_dashboard');
}
}
  • if i click the sign up button i get "error1" and if i click the sign in button i get "error2"

so it is not finding the button name.

colourmill's avatar

If you press F12 in either chrome or firefox you get your developer tools. When you submit a form, you see that request in the network tab of your developer tools. You should really familiarize yourself with this if you want to improve your debugging.

From the network tab, you should be able to tell how many times your form is posted, which url it used, and which values were posted to your server.

If I understand correctly, if you remove the "save" from your code, everything is working? Are you sure you're not testing against a "dirty" database?

thomaskim's avatar

there are to email forms on the same page. 1 for the signin an d on for the signup. if i try to sign in both email input fail and i get an error saying that email already exists in the database. so the login is failing because it is also trying to register.

One problem with this is that you should not be validating a unique email for sign ins. By its very definition, it should be the complete opposite. It is impossible for a user to sign in with a unique email because that means an email does not exist in the database.

Also, these two lines of code will always fail:

if ($request->has($request['signup'])) {
if ($request->has($request['signin'])) {

You should just remove those two if statements because in this case, they serve no purpose. You are not passing in a "signup" or "signin" input.

Also, for future reference, if you want to check if the request has an input, you should do this:

$request->has('input')
rickyspires's avatar

Thanks.

Good point. well spotted :)

I will have another bash at it ...

jekinney's avatar

I would use one form. Keep in mind you can use two submit buttons. Check if optional fields are filled in with $request->has();

bashy's avatar

@thomaskim

You are not passing in a "signup" or "signin" input.

You can do this by using name="" on the form button.

thomaskim's avatar

@bashy Yes, but he's not doing that. :) So it serves no purpose in this case. It'll just always fail.

bashy's avatar

@thomaskim I see, which should do it?

<button type="submit" name="signin" class="btn btn-primary">Login</button>
thomaskim's avatar

@bashy Oh whoops. I didn't even see that. Lol. You have a good eye, and I am blind. :)

d3xt3r's avatar

There are more problems to the form than already spotted.

For starter, to address the original problem, do as already suggested. You validation rule unique email on user signin is causing the problem.

Next, make sure you have different fields as login_email, registration email (not necessary) but since you are using

<div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}">
</div>

this will high light the fields in both the form in case of any error in email.

Wakil_Ahmed's avatar

Just add an id to the form you wanna submit.

$(document).on("click","#formPass",function() { $(this).parents("form").submit(); });

Please or to participate in this conversation.