1

I have a form comment section whereby a user can reply to a comment. Each comment has a form with a reply box. Am getting the comments from the database and running the following loop.

@foreach($responses as $response)

<li>{{$response->answer}}</li>

<form action="{{action('DiscussionController@postAnswerQuestion', [$entry_year, $grade_id, $subject_id, $question_id])}}" method="post" id="form1">
<input type="text" name="reply" value="" placeholder="Reply">
<input type="hidden" name="response_id" value="{{$response->id}}">
<input type="submit" name="" value="reply">
 </form>
  @endforeach

When I submit the php form using normal PHP it submits just fine but when using jquery ajax and the following code.

  var formData = {
        'answer'            : $('input[name=answer]').val(),
        'reply'             : $('input[name=reply]').val(),
        'response_id'       : $('input[name=response_id]').val(),
    };

It returns the value of the first input only after some googling I found that I should use serialize and so instead of the above jquery code I used the following

        var formData = $('#form1').serialize();

But now the values arent being captured and the values are coming to php as null. How should I solve the above problem?

Thanks a lot guys!

3
  • Are you passing formData obj to your php page ? If so , do you see formData obj as null or undefined ? Commented Sep 9, 2015 at 16:43
  • 3
    You have id="form1" in a loop -> @foreach($responses as $response), so instead of unique ids, you have multiple same ids Commented Sep 9, 2015 at 16:51
  • One method, I'd can think of is, keep your jquery code inside an $('#form1').submit() event and then reference all your variables using $(this).serialize() . But I'd recommend you to add a class to the form rather than id and then using $('.form1') Commented Sep 9, 2015 at 16:58

2 Answers 2

2

First, replace the id on the form with a class.

<form action="..." method="post" class="commentForm">
<input type="text" name="reply" value="" placeholder="Reply">
<input type="hidden" name="response_id" value="{{$response->id}}">
<input type="submit" name="" value="reply">
</form>

Then register a submit-event handler on the forms to make the ajax call. Inside the event handler callback, this refers to the form being submitted. You can use that to get the values of the inputs for that form.

$('.commentForm').submit(function(event) {
    event.preventDefault();

    var $form = $(this);
    var formData = {
        answer     : $form.find('input[name=answer]').val(),
        reply      : $form.find('input[name=reply]').val(),
        response_id: $form.find('input[name=response_id]').val()
    };

    // Make the ajax call here.
});

However, you should be able to skip putting together the formData object, and use the following for your ajax call:

data: $(form).serialize(),
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is looping and creating multiple forms with same name of textboxes, so jquery cannot recognize type='name' are you specifying when your write $('input[name=answer]').val();

So I would suggest you add id to all 3 input tags. For each loop all 3 tags should have the same id. Then you can recognize them individually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.