php - 如何从使用 jquery 的 foreach 循环生成的表单中的输入字段中获取值

标签 php jquery ajax foreach

我有 5 个由 foreach 循环生成的表单,我想使用 jquery ajax 发送输入数据。如果我想从 2nd 获取值表单,我得到一个空数组。

我的表格:

<form id="<?php echo $gb_id; ?>" action="" method="POST" role="form"> // $gb_id is unique id

    <input class="hide-robot honeyname" name="honeyname" type="text" />
    <input class="form-control email_from" name="email-from" type="email" value=""> 
    <input class="form-control email_to" name="email-to" type="hidden" value="<?php echo $gb_email; ?>">
    <input class="form-control email_subject" name="email-subject" type="text" value="">
    <textarea rows="6" class="form-control custom-control email_message" name="email-message">
</textarea> 

    <button class="btn btn-primary float-right" type="submit" name="submit_email">Send</button> 
</form>

在每个表单下(也在 foreach 中)都有我的 jquery ajax,如下所示:

$('#<?php echo $gb_id; ?>').on('submit', function(e){
    e.preventDefault();

    var honeyname = $(".honeyname").val();
    var email_from = $(".email_from").val();    
    var email_to = $(".email_to").val();
    var email_subject = $(".email_subject").val();
    var email_message = $(".email_message").val();

    $.ajax({
        url: 'email.php',
        type: 'POST',
        data: {
                honeyname:honeyname,
                email_from:email_from,
                email_to:email_to,
                email_subject:email_subject,                
                email_message:email_message

                },

        success: function(data){
            $('.result').html(data);

        },
        complete:function(){
           $('body, html').animate({scrollTop:$('.result').offset().top}, 'slow');         
        }
    });

});

还有我的 php:

print_r($_POST);    
    $recipient = $_POST['email_to'];
    $subject = $_POST['email_subject'];
    $body = $_POST['email_message'];
    $headers  = 'MIME-Version: 1.0';
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers .= 'From: '.$_POST['email_from'];

    echo $_POST['email_from'].'<br />';
    echo $recipient.'<br />';
    echo $subject.'<br />';
    echo $body.'<br />';

如果我填写第一个表单,我会得到一个包含所有值的完整数组。

但是如果我填写第二个或第三个表格,则只有 [email_to]有一个值和 print_r给我这个输出:

Array ( [honeyname] => [email_from] => [email_to] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8bbbcadbeada688a4a1beade6a6a4" rel="noreferrer noopener nofollow">[email protected]</a> [email_subject] => [email_message] => )

所以我想:从我的 jquery 中的输入字段中获取最接近的值:

var honeyname = $(this).closest(".honeyname").val();
var email_from = $(this).closest(".email_from").val();  
var email_to = $(this).closest(".email_to").val();
var email_subject = $(this).closest(".email_subject").val();
var email_message = $(this).closest(".email_message").val();

但是现在,即使使用第一种形式,我也得到了一个完整的空数组!

我如何才能使每个表单都适用?

$gb_id独特且相互对应

最佳答案

根据最近的文档:( https://api.jquery.com/closest/ )

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

您想要使用的不是最接近,而是查找 ( https://api.jquery.com/find/ )

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

所以改变:$(this).closest$(this).find

它将在 DOM 上“向下”而不是“向上”。

关于php - 如何从使用 jquery 的 foreach 循环生成的表单中的输入字段中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60554371/

相关文章:

javascript - ListView 上的 OnClick 事件有问题吗?

javascript - 选中两个绑定(bind)复选框时显示重复值

php - 无法在 Jquery/Ajax 中调用 Laravel 路由

php - 图片未正确存储在 mysql 数据库中

php - 如何每 10.6 秒从时间戳增加 +1

php - Magento,将增值税号码添加到账单地址

javascript - Jquery slider 中的响应式 Img

php - codeigniter如何从 Controller 传递base_url()

javascript - 我可以使用我的 jQuery AJAX post 方法数据键连接到我的 php 文件吗?

javascript - Codeigniter:在ajax中成功获取php数组