laravel ajax仅在第一列

时间:2018-05-31 19:40:54

标签: php ajax laravel

Sory我的英语不好,我的程序中的评论输入表格有问题。只有填充列是顶部列时,流程中的注释字段才会成功。如果除上述之外的注释字段将失败。请赐教他

Urgent

这是第一栏评论中的成功流程 first column comment

但如果我在评论字段中写入除上述内容之外将失败 comment field

标记和字段with_id与上面的注释列相同,而来自barengan_id的值(如果在inspect元素中)与其内容不同。并且注释字段如此空值 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

这是我的代码

我的控制器

<?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);
    }
}

这是我在视野中的表格

<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>

和我的模特

<?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);
    }
}

这些天我累得很累:(

1 个答案:

答案 0 :(得分:1)

您在页面上使用多个表单?看起来像输入冲突的id。 试试这种方式

<form method="post" data-toogle="validator" class="form-horzontal" data-barengan="{{$d->id}}">
...
if (!e.isDefaultPrevented()) {
        var barenganId = $(this).data('barengan');