Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

laravel - Append data to form using through DOM?

Form which uses POST method:

<div>
<form action="{{ route('post.store') }}" method="POST">
   <input type="text" name="name">
   <input type="text" name="text">
   <button type="submit">Submit</button>
</form>
@foreach ($comments as $comment)
   {{ $comment->text }}
   <a href="#">Reply</a>
@endforeach
</div>

Array of $comments is being returned when the view itself is returned.

Basically, I am intrested if there is a way to append $comment->id to the data that will be sent to server on Submit button click using Laravel Blade or AlpineJS, but without creating any additional functions between <script> tags? I mean, is it possible to do something like this: <a href="#" @click.prevent="formData.append('parent_comment_id', {{ $comment->id }})">Reply</a>?

EDIT:

I expressed myself wrongly. I don't want to append new id every time Reply is clicked, but to overwrite corresponding property in data which is going to be sent to server when Submit button is clicked with the $comment->id for which Reply was clicked for.

question from:https://stackoverflow.com/questions/65871205/append-data-to-form-using-through-dom

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume you are using Laravel Resource Controller

Not sure I totally understand why you want to send all comment ids when submitting the form but when calling the route post.store you should better send the post id

<form action="{{ route('post.store', ['post' => $post]) }}" method="POST">

If you want to get all comment id and have proper model relationships set up in your models you can get all comments for a post in your controller.

To send a specific id in your form create a new <input name="comment_id" value=""> and let that field be populated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...