سلام
من فرم ارسال کامنت رو با ajax انجام داده ام وقتی button رو میزنم 2 بار ارسال انجام میشه و تو پایگاه داده 2 بار ذخیره میشه
این مشکل رو چطور برطرف کنم
@ziba.varmaziari
بجای create تو کنترلر از firstOrCreate نمیشه استفاده کنی ؟
شاید پاسخ شما همینجا باشد
$('.send-comment').click(function
event.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
var comment = $('.comment').val();
var id = $('.send-comment').attr('data');
$.post('new-comment', {comment:comment , id:id,email:email , name:name}, function ($result, $status) {
if($result ==1)
{
$(".send-comment").after("دیدگاه شما ثبت شد و پس از تایید مدیران نمایش داده می شود.");
$('.comment').val("");
}
else
{
$("#posted").text("خطا در اتصال به دیتا بیس,صفحه را لود کنید و دوباره امتحان کنید");
}
});
});
Route::post('/product/new-comment','ProductController@postNewComment');
public function postNewComment()
{
$user = Auth::user();
if ($user == null) {
return redirect(url('/register'));
} elseif ($user != null) {
$comment = new Comment();
$comment->content = Request::input('comment');
$comment->name = Request::input('name');
$comment->email = Request::input('email');
$comment->user_id = Auth::User()->id;
$comment->product_id = Request::input('id');
$comment->status = 0;
$comment->save();
return 1;
}
}
@arash.taghavi69
دقیقا 2 تا درخواست میره
@if (Auth::User())
<div>
<div class="form-group clearfix">
<label class="col-xs-12 col-sm-12 col-md-12 col-lg-2" for="name">نام</label>
<input readonly class="col-xs-12 col-sm-12 col-md-12 col-lg-4" name="name" id="name" value="{{$User->name}}">
</div>
<div class="form-group clearfix">
<label class="col-xs-12 col-sm-12 col-md-12 col-lg-2" for="email">ایمیل</label>
<input readonly class="col-xs-12 col-sm-12 col-md-12 col-lg-4" name="email" id="email" value="{{$User->email}}">
</div>
<div class="form-group clearfix">
<label class="col-xs-11 col-sm-11 col-md-11 col-lg-2" for="comment">دیدگاه</label>
<textarea class="col-xs-11 col-sm-11 col-md-11 col-lg-9 comment" name="comment" id="comment" rows="6"></textarea>
<span class="required">*</span>
</div>
<button data="{{$Product->id}}" class="btn btn-wide btn-success send-comment">ارسال دیدگاه</button>
<p id="posted"></p>
</div>
@else
<div style="align-items: center;
display: flex;
flex-direction: column;
margin: auto;">
<p style="margin-bottom: 13px;
font-weight: 900;
font-size: 17px;">برای ثبت دیدگاه لطفا به فروشگاه وارد شوید</p>
<a style="width: 10px;margin: auto;" class="btn btn-wide btn-warning center" href="https://roocket.ir/{{url('login')}}" class="btn btn-primary alone" >
{{ __('ورود به حساب کاربری') }}
</a>
</div>
@endif
</div>
@arash.taghavi69
من نمونه کد خودمو میذارم ببینید میتونید با این مشکل رو حل کنید
@auth
<div class="box">
<div class="box-body">
<div class="widget">
<h4 class="mt-0 pb-15 mb-25 bb-1">ارسال دیدگاه</h4>
<input type="hidden" name="parent_id" >
<div class="col-lg-12 col-md-12">
<div class="form-group">
<textarea class="form-control btn--comments-reply" id="mytextarea" name="body" rows="7"
placeholder="دیدگاه مد نظر شما "></textarea>
</div>
</div>
<div class="col-lg-12 col-md-12">
<button id="ajax-sub" class="btn btn-primary">
<span>ارسال دیدگاه</span>
</button>
</div>
</div>
</div>
</div>
@endauth
<script>
$(document).ready(function(){
$('.replyComment').on('click' , function () {
let id = $(this).attr('data-commentID');
$('input[name="parent_id"]').val(id);
});
$('#ajax-sub').click(function(e){
// e.preventDefault();
if($("#mytextarea").val().length == 0){
Swal.fire({
title: 'خطا',
text: 'متن دیدگاه کوتاه است',
icon: 'error',
confirmButtonText: 'بسیار خوب',
animation : true,
timer: 5000
})
}
$.ajax({
url: "{{route('comment.article' , $article->id)}}",
method: 'post',
data: {
body: $('#mytextarea').val(),
parent_id: $('input[name="parent_id"]').val(),
_token: "{{ CSRF_TOKEN() }}"
},
success: function(result){
document.getElementById("mytextarea").value = "";
$('input[name="parent_id"]').val('');
Swal.fire({
title: 'با تشکر',
text: 'دیدگاه شما با موفقیت ارسال شد و پس از بررسی و تایید توسط مدیر وبسایت منتشر خواهد شد ',
icon: 'success',
confirmButtonText: 'بسیار خوب',
animation : true,
timer: 5000
})
}});
});
});
</script>
public function articleComment(Article $article , Request $request)
{
$article->comments()->create([
'body' => $request->body,
'user_id' => auth()->id(),
'parent_id' => $request->parent_id,
]);
return response()->json(['success' => 'عملیات با موفقیت انجام شد']);
}public function postNewComment()
{
$user = Auth::user();
if ($user == null) {
return redirect(url('/register'));
} elseif ($user != null) {
$comment = new Comment();
$comment->content = Request::input('comment');
$comment->name = Request::input('name');
$comment->email = Request::input('email');
$comment->user_id = Auth::User()->id;
$comment->product_id = Request::input('id');
$comment->status = 0;
$comment->save();
return 1;
}
}
این ها داخل فایل web هستند یا api?
@arash.taghavi69
2 بار چاپ کرد
@Rp76
داخل controller هستن
@ziba.varmaziari
برام جالب شد بدونم از کجا بهش دسترسی دارید از فایل api یا web
به این کنترلر درخواست ارسال شده؟
@Rp76
بله این کنترولر درخواست post انجام میده
@arash.taghavi69
من هرچی تابع در این صفحه مینویسم 2 تا میاره و مشکل از چیه نمیدونم
@arash.taghavi69
حل شد مسیرشو عوض کردم