dongweiben5229 2018-05-31 19:40
浏览 94
已采纳

laravel ajax仅在第一列插入数据表单注释成功

Sory my english is not good, I have a problem with comment input form in my program. the comment field in the process will only succeed if the filled column is the top column. if the comment field other than the above will fail. please enlighten him

Urgent

this is a successfull process in first column comment first column comment

but if I write in the comment field other than the above will fail comment field

token and field with_id same as comment column above, whereas value from barengan_id if in inspect element differ its contents. and also comment field so empty value token and field with_id same as comment column above, whereas value from barengan_id if in inspect element differ its contents. and also comment field so empty value

enter image description here

and this is my code

my controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Barengan;
use App\BarenganComment;
use App\User;
class CariBarenganCommentController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store(Request $request,Barengan $id)
    {
        $data = [
            'user_id' => auth()->id(),               
            'barengan_id' => $id->id,
            'comment' => $request['comment'],
        ];
        return BarenganComment::create($data);
    }

    public function destroy(Barengan $barengan_id,$id)
    {
        BarenganComment::destroy($id);
    }
}

And this my form in view

<div id="form">
    <form method="post" data-toogle="validator" class="form-horzontal">
        {{ csrf_field() }}
        {{method_field ('POST')}} 
        <input type="hidden" name="id" id="id">              
        <input type="hidden" name="barengan_id" value="{{$d->id}}" id="barengan_id">
        <div class="styled-input">
          <input class="input inputkoment" type="text" placeholder="Tulis Komentar ..." name="comment" id="comment">
          <span></span> 
          <button type="submit" class="btn btn-default pull-right btn-custom-komen"><i class="fa fa-chevron-circle-right"></i></button>
        </div>        
    </form>
</div>


<script src="{{asset('js/jquery-1-11-0.js')}}"></script>

<script>
function deleteComment(id) {
    var popup = confirm("apakah anda yakin akan menghapus data?");
    var csrf_token = $('meta[name="csrf-token"]').attr('content');
    if(popup == true){                
      $.ajax({

        url: "{{ url('caribarengancomment')}}/"+id,         
        type: "POST",
        data: {'_method': 'DELETE','_token': csrf_token
      },
      success: function(data) {
        $("#contact-table").load(" #contact-table");       
        $('#alert-success').html('show');
      },
      error: function () {
        alert("Opppps gagal");
      }
    })
    }
  }


 $(function () {
    $(document).on('submit','#form form',function (e) {
      if (!e.isDefaultPrevented()) {
        var barenganId = $('#barengan_id').val();
        console.log(barenganId);
        url = "{{ url('caribarengan')}}/" + barenganId + "/comment";  
        // url= '{{route('caribarengancomment.store',$d)}}';          

        $.ajax({
          url: url,
          type: "POST",
          data: $('#form form').serialize(),
            success: function(data) {
              $("#contact-table").load(" #contact-table");              
              $('#alert-success').html('show');
            },
            error: function () {
              alert('Oops! error!');
            }
          });
        return false;
      }
    });
  });
</script>

and my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BarenganComment extends Model
{
    protected $fillable = ['user_id','barengan_id','comment'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function barengan()
    {
        return $this->belongsTo(Barengan::class);
    }
}

I am very tired these days stack here :(

  • 写回答

1条回答 默认 最新

  • dsy19890123 2018-05-31 20:18
    关注

    you use multiple forms on page? look like id`s of inputs conflict. try this way

    <form method="post" data-toogle="validator" class="form-horzontal" data-barengan="{{$d->id}}">
    ...
    if (!e.isDefaultPrevented()) {
            var barenganId = $(this).data('barengan');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题