【PHP】Ajax
blade.php
<!-- ajax -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$('#answer-btn').on('click', function () {
$question_id = {{ $question->id }};
$answer_choice = {{ $question->answer_choice }};
$answered_choice = Number($('input[name="answer"]:checked').val());
console.log($answered_choice);
console.log($answer_choice);
console.log($question_id);
$.ajax({
type: "POST",
url: "/question/choice",
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
data: { 'answered_choice': $answered_choice, 'question_id': $question_id },
});
})
</script>
<!-- ajax -->
QuestionController.php
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function choice(Request $request)
{
$answered_choice = $request->answered_choice;
$question_id = $request->question_id;
$question = Question::find($question_id);
if($answered_choice == $question->answer_choice){
$question->update(["status_num" => 2]);
} else {
$question->update(["status_num" => 3]);
}
}
web.php
use App\Http\Controllers\QuestionController;
Route::resource('/question', QuestionController::class)->middleware('auth');
Route::post('/question/choice', [QuestionController::class, 'choice'])->middleware('auth');
Author And Source
この問題について(【PHP】Ajax), 我々は、より多くの情報をここで見つけました https://qiita.com/RealXiaoLin/items/e36fbbdad15aa240e439著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .