как отличить guish обработку данных от 2 форм с 1 ajax до php - PullRequest
1 голос
/ 29 апреля 2020

У меня есть 2 разные формы, и я хочу обработать данные этих форм с 1 ajax в файл php. Чтобы отличить guish, если это main-comment-form или nested-comment-form, я добавил дополнительный скрытый ввод со значением main_form в первое поле формы:

Основная форма (для обычного комментария):

<form class="commentform" action="" method="POST" role="form">
    <!-- MAIN_COMMENT; need to distinguish main comment or nested comment -->
    <input type="hidden" class="main_comment" value="true" />
    <!-- NAME -->
    <input class="form-control comment_name" name="comment_name" type="text" placeholder="NAME">    
    <button type="submit" class="btn btn-primary" name="submit_comment">Submit</button>
</form>

2-я форма (для размещения вложенного комментария)

<form class="commentform" action="" method="POST" role="form">
    <!-- NESTED NAME -->
    <input class="form-control nested_comment_name" name="nested_comment_name" type="text" placeholder="NAME">  
    <button type="submit" class="btn btn-primary" name="submit_nested_comment">Submit</button>
</form> 

My ajax js выглядит this:

$('.commentform').on('submit', function(e){
    e.preventDefault();

    var main_comment = $(".main_comment").val();    
    var comment_name = $(".comment_name").val();        
    var nested_comment_name = $(".nested_comment_name").val();


    $.ajax({
        url: 'comment.php',
        type: 'POST',
        data: {
                main_comment:main_comment,          
                comment_name:comment_name,                              
                nested_comment_name:nested_comment_name,

                },

Файл PHP, комментарий. php:


// variables
$main_comment = $_POST['main_comment'];
echo $main_comment;

Когда я публикую 2-ю форму (вложенный комментарий), он также отображает значение true. Я не понимаю этого, потому что во 2-м классе я не отправляю $_POST['main_comment']

1 Ответ

1 голос
/ 29 апреля 2020

Ваш способ мышления, чтобы решить эту проблему со скрытым полем, хорош. Но вы должны также поместить скрытое поле во вторую форму со значением false

1-я форма:

<form class="commentform" action="" method="POST" role="form">
    <!-- CHECK_MAIN_COMMENT; need to distinguish main comment or nested comment -->
    <input type="hidden" class="main_comment" value="true" />
    <!-- NAME -->
    <input class="form-control comment_name" name="comment_name" type="text" placeholder="NAME">    
    <button type="submit" class="btn btn-primary" name="submit_comment">Submit</button>
</form>

2-я форма:

<form class="commentform" action="" method="POST" role="form">
    <!-- CHECK_MAIN_COMMENT; need to distinguish main comment or nested comment -->
    <input type="hidden" class="main_comment" value="false" />
    <!-- NESTED NAME -->
    <input class="form-control nested_comment_name" name="nested_comment_name" type="text" placeholder="NAME">  
    <button type="submit" class="btn btn-primary" name="submit_nested_comment">Submit</button>
</form>

Ваш ajax:

$('.comment_form').on('submit', function(e){
    e.preventDefault();

    // below;this will find the value of class main_comment from the CLOSEST form!
    var main_comment = $(this).closest('.comment_form').find('.main_comment').val(); 

    var comment_name = $(".comment_name").val();        
    var nested_comment_name = $(".nested_comment_name").val();

    $.ajax({
        url: 'comment.php',
        type: 'POST',
        data: {
                main_comment:main_comment,          
                comment_name:comment_name,                              
                nested_comment_name:nested_comment_name,

                },

комментарий. php

// variables
$main_comment = $_POST['main_comment'];

if($main_comment == 'true') {
    // handle data that comes from 1st form
}
else {
    // handle data that comes from 2nd form
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...