发布表单启动获取请求

时间:2018-07-20 22:19:35

标签: php html post get

我创建了一个表单,该表单在单击“提交”按钮后启动POST请求。但是,最终,我意识到处理请求的PHP代码无法正常运行。当我尝试调试代码时,我打开了“网络”选项卡,并通过提交表单进行了测试。 GET请求没有启动POST请求,而是在我的网络上显示,这说明了PHP代码无法正常运行的原因,因为它实际上从未收到POST请求。但是,我不明白为什么可能会有GET请求而不是POST。有人有什么想法吗?

更新:这与我的.htaccess文件的redirect属性有关吗?

.htaccess文件

<IfModule mod_rewrite.c>

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


    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

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

</IfModule>

提交表单后控制台选项卡的图片。即使我的网络注册了POST请求,控制台也始终打印出“ GET REQUEST”,这意味着我的PHP方法注册仅接收到GET请求,尽管我的网络说了什么。

Picture of Network Tab after submitting form. Why is there a GET request instead of a POST upon submitting the form?

HTML代码(我的表单):

<form role="form" action="" method="post" class="registration-form">

                        <fieldset>
                            {{ csrf_field() }}

                            <div class="form-top">

                                <div class="form-top-left">
                                    <h3>Insight Contributor Account Info</h3>

                                </div>
                                <div class="form-top-right">

                                </div>
                            </div>

                            <div class="form-bottom" style="height: 400px">
                                <!--Name-->
                                <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">

                                    <div class="col-md-8 col-md-offset-2">

                                        <input id="name" type="text" class="form-control" name="name"
                                               placeholder="Full Name (e.g. John Doe)" value="{{ old('name') }}">
                                        <br>

                                        @if ($errors->has('name'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('name') }}</strong>
                                            <h3> name is required</h3>
                                        </span>
                                        @endif
                                    </div>
                                </div>
                                <!--Email-->
                                <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">

                                    <div class="col-md-8 col-md-offset-2">
                                        <input id="email" type="email" class="form-control" name="email"
                                               placeholder="Primary Email Address (e.g.Jdoe@gmail.com)"
                                               value="{{ old('email') }}"><br>
                                        @if ($errors->has('email'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('email') }}</strong>
                                            </span>
                                        @endif
                                    </div>
                                </div>
                                <!--Password-->
                                <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                    <!-- <label for="password" class="col-md-4 control-label">Password</label>-->
                                    <div class="col-md-8 col-md-offset-2">
                                        <input id="password" type="password" class="form-control"
                                               placeholder="Password (at least 6 character)" name="password"><br>
                                        @if ($errors->has('password'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('password') }}</strong>
                                            </span>
                                        @endif
                                    </div>
                                </div>
                                <!--PasswordConfirm-->
                                <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                                    <div class="col-md-8 col-md-offset-2">
                                        <input id="password-confirm" type="password" class="form-control"
                                               placeholder="Confirm Password" name="password_confirmation"><br>

                                        @if ($errors->has('password_confirmation'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('password_confirmation') }}</strong>
                                                <h3> password mismatch</h3>
                                            </span>
                                        @endif
                                    </div>
                                </div>
                            </div>

                            <div class="form-top" style="margin-top: 10px">

                                <div class="form-top-left">
                                    <h3>Professional Information</h3>
                                </div>

                            </div>

                            <div class="form-bottom" style="height: 460px">
                                <!--Primary Industry(single value)-->
                                <div class="form-group{{ $errors->has('industry') ? ' has-error' : '' }}">

                                    <div class="col-md-8 col-md-offset-2"><br>
                                        <select class="form-control selectpicker" name="industry" id="industry">
                                            <option selected disabled>Primary Industry</option>
                                            <option>Art</option>
                                            <option>Business</option>
                                            <option>Law</option>
                                            <option>Media</option>
                                            <option>Medicine</option>
                                            <option>Education</option>
                                            <option>Technology</option>
                                            <option> Science</option>
                                            <option>Service</option>
                                            <option>Other</option>
                                        </select>


                                        @if ($errors->has('industry'))
                                            <span class="help-block">
                                            <strong>{{ $errors->first('industry') }}</strong>
                                            </span>
                                        @endif
                                    </div>
                                </div>


                                <!--Primary Job Function (single value)-->
                                <div class="form-group{{ $errors->has('job_function') ? ' has-error' : '' }}">

                                    <div class="col-md-8 col-md-offset-2"><br>
                                        <select class="form-control selectpicker" name="job_function"
                                                id="job_function">
                                            <option selected disabled>Primary Job Function</option>
                                            @foreach($professions as $profession)
                                                <option @if ($profession->id == old('job_function_id')) selected
                                                        @endif value="{{ $profession->id }}">{{ $profession->name }}</option>
                                            @endforeach
                                        </select>

                                        @if ($errors->has('job_function'))
                                            <span class="help-block">
                                            <strong>{{ $errors->first('job_function') }}</strong>
                                            </span>@endif



                                    </div>
                                </div>

                                <!--Add relative experience (multi tag)-->
                                <div id="tags" class="form-group" style="margin-top: 30px">

                                    <div class="col-md-8 col-md-offset-2"><br>


                                        <select id="test" style="width: 100%;margin-left: 10%;" name="tags[]"
                                                multiple>

                                            <option value="root" disabled="disabled">Tags</option>
                                            <option value="level11" parent="root" disabled="disabled">Subjects
                                            </option>
                                            <option value="level12" parent="root" disabled="disabled">Grades
                                            </option>
                                            <option value="level13" parent="root" disabled="disabled">Relationship
                                                Management
                                            </option>
                                            <option value="level14" parent="root" disabled="disabled">Classroom
                                                Management & Design
                                            </option>
                                            <option value="level15" parent="root" disabled="disabled">Curricula &
                                                Resources
                                            </option>
                                            <option value="level16" parent="root" disabled="disabled">Professional
                                                Growth & Career Management
                                            </option>
                                            <option value="level17" parent="root" disabled="disabled">More</option>
                                            @foreach($tags as $tag)

                                                @if($tag->category =='Subjects')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level11"> {{$tag->name}}</option>
                                                @endif
                                                @if($tag->category =='Grades')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level12"> {{$tag->name}}</option>
                                                @endif
                                                @if($tag->category =='Relationship Management')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level13"> {{$tag->name}}</option>
                                                @endif
                                                @if($tag->category =='Classroom Management & Design')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level14"> {{$tag->name}}</option>
                                                @endif
                                                @if($tag->category =='Curricula & Resources')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level15"> {{$tag->name}}</option>
                                                @endif
                                                @if($tag->category =='Professional Growth & Career Management')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level16"> {{$tag->name}}</option>
                                                @endif
                                                @if($tag->category =='More')
                                                    <option value='{{ $tag->id }}'
                                                            parent="level17"> {{$tag->name}}</option>
                                                @endif
                                            @endforeach

                                        </select>
                                        @if ($errors->has('tags'))
                                            <span class="help-block">
                                    <strong>{{ $errors->first('tags') }}</strong>
                                    </span>
                                        @endif
                                    </div>
                                </div>


                                <!--Bio-->
                                <div class="form-group{{ $errors->has('bio') ? ' has-error' : '' }}">
                                    <!--  <label for="bio" class="col-md-4 control-label">Short Bio</label> -->

                                    <div class="col-md-8 col-md-offset-2"><br>
                                    <textarea id="bio" class="form-control" placeholder="Brief profile bio"
                                              name="bio">{{ old(nl2br('bio')) }}</textarea><br>


                                        @if ($errors->has('bio'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('bio') }}</strong>

                                            </span>
                                        @endif
                                    </div>
                                </div>
                            </div>

                            <div class="form-top" style="margin-top: 10px">

                                <div class="form-top-left">
                                    <h3> Agreements </h3>
                                </div>

                                <div class="form-top-right">
                                </div>
                            </div>

                            <div class="form-bottom">

                                <!--Terms-->
                                <h2 class="section-heading">Cypress Community Principles</h2>
                                <p class="lead">
                                    <br>
                                    Teachers value each other for their expertise.<br><br>

                                    Teachers believe in the power of collaboration and will work together to engage
                                    in
                                    open and honest dialogue, provide guidance and mentorship, and create content
                                    that
                                    supports growth and success for fellow teachers.<br><br>

                                    Teachers will respect each other and be mindful of what they post. We encourage,
                                    open and honest communication, a diversity of perspectives, and thoughtful
                                    disagreement. Harassment, disrespect, and inappropriate content are not
                                    tolerated.<br><br>

                                    Teachers will actively engage in fostering a positive community of learning and
                                    growth.<br><br>

                                    Teachers are the most significant influence on a student’s academic achievement
                                    and
                                    will support fellow teachers as agents of change and innovators of
                                    education.<br><br>
                                </p>

                                <form action="#"
                                      onsubmit="
                                  if(document.getElementById('agree').checked) {
                                  return true;

                                  } else
                                  { alert('Please indicate that you have read and agree to the Terms and Conditions and Privacy Policy');
                                  return false;
                                  }">
                                    <input type="checkbox" name="checkbox" value=0 id="agree"/> I have read and
                                    agree to
                                    the Community Principle,
                                    <a href="/terms" style="color: #5dc19f">Terms
                                        and Conditions
                                    </a> and
                                    <a href="/privacypolicy" style="color: #5dc19f">Privacy
                                        Policy</a><br><br>

                                </form>

                                <!--Signup botton-->
                                <button type="submit" id="submit" class="btn btn-default"
                                        style="background-color: #a5d5a7">
                                    <i class="fa fa-btn fa-user"></i> Sign me up!
                                </button>

                                <button type="button" class="btn btn-default" style="background-color: #a5d5a7">
                                    <a class="btn btn-link" href="{{ url('/') }}" style="color: whitesmoke">
                                        Cancel </a>
                                </button>


                            </div>
                        </fieldset>

                    </form>

PHP代码(我利用控制台检查了到达PHP代码的请求类型):

public function register(Request $request)
    {
        echo '<script>console.log("request()");</script>';
        $tags = Tag::all();
        $professions = Profession::all();
        if($request->isMethod('get')){
            echo '<script>console.log("GET REQUEST");</script>';
        }
        if ($request->isMethod('post')) {
            echo '<script>console.log("POST REQUEST");</script>';
            $validator = $this->validateRegister($request->input());
            if ($validator->fails()) {
                return back()->withErrors($validator)->withInput();
            }

            $user = Iuser::create([
                'name' => $request['name'],
                'email' => $request['email'],
                'password' => bcrypt($request['password']),
                'bio' => $request['bio'],
                'industry' => $request['industry'],
                'confirmation_code' => str_random(30),
                'job_function' => $request['job_function'],
            ]);
            $user ->tags()->sync($request['tags']);

            #event(new NewUserWasRegistered($user));


            if($user->save()){
                return redirect('/insight/login')->with('success', 'Welcome to Cypress!');
            }else{
                return back()->with('error', 'Register failed!')->withInput();
            }
        }
        $datas = array('tags' => $tags, 'professions'=>$professions);
        #return $user;
        return view('iauth.register')->with($datas);
    }



    protected function validateRegister(array $data)
    {
        echo '<script>console.log("validateRegister");</script>';
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'password_confirmation' => 'required|min:6',
            'bio' => 'required',
            'industry' => 'required|string',
            'job_function' => 'required|string',
        ], [
            'required' => ':attribute is required',

            'min' => ':attribute is too short',
            'confirmed' => 'different passwords',
            'unique' => 'This email exits',
            'max' => ':attribute is too long'
        ]);
    }

1 个答案:

答案 0 :(得分:0)

我可能会误会,但我认为您可能没有将请求日志保存在浏览器开发人员工具的“网络”标签中。

您的控制器正在使用Post-Redirect-Get方法。处理请求后,它将使用redirect()back()(也是重定向)返回响应。

因此有 个POST请求,但是如果您不将该请求日志保留在浏览器工具中,则后续的GET请求将在“网络”标签中替换该请求。在Firefox中寻找名为“ Persist Logs”的设置,在Chrome中寻找“ Preserve log”的设置。 (对不起,我不确定在IE / Edge中它叫什么,我现在无法理解,但我想它也有类似的东西。)