简体   繁体   English

循环 DIV 元素并获取动态添加的文本输入 - PHP

[英]Loop through DIV elements and get dynamically added text inputs - PHP

I have a form on a web page that allows for dynamically added divs with text inputs inside them (via JavaScript) into a div called "actorsDivHTML".我在网页上有一个表单,它允许将其中包含文本输入的 div(通过 JavaScript)动态添加到名为“actorsDivHTML”的 div 中。 So the structure is as follows:所以结构如下:

<div name="actorsDivHTML">

    <div name="actor[]">
        <input type="text" name="actorName[]">
        <div name="actorInfoDiv[]">
            <input type="text" name="actorInfo[]">
            <input type="text" name="actorInfo[]">
        </div>
    </div>

    <div name="actor[]">
        <input type="text" name="actorName[]">
        <div name="actorInfoDiv[]">
            <input type="text" name="actorInfo[]">
        </div>
    </div>

</div> 

Note: I am unsure if a div is allowed to be used the same way an input is -> ie.注意:我不确定是否允许以与输入相同的方式使用 div -> 即。 "div[]" “div[]”

My aim is to allow the user to dynamically add as many "actors" as possible with as many "actorInfo" inputs for each "actor" as they require.我的目标是允许用户根据需要为每个“演员”动态添加尽可能多的“演员”,并为每个“演员”输入尽可能多的“演员信息”。 When the user submits the form, I have a PHP file that will need to get each "actor" and each "actorInfo" from the "actorsDivHTML" div.当用户提交表单时,我有一个 PHP 文件,需要从“actorsDivHTML”div 中获取每个“actor”和每个“actorInfo”。

My questions are;我的问题是; what is the best way to name each div so the info can be easily extracted by the PHP script?为每个 div 命名以便 PHP 脚本可以轻松提取信息的最佳方法是什么? What is the best way to iterate through the div and extract the information that I need (ie. each actor and the info about them), and place them into an array of the following structure:遍历 div 并提取我需要的信息(即每个演员和有关他们的信息)并将它们放入以下结构的数组中的最佳方法是什么:

Array(
    [actor] => Array([0] => "name", [1] => Array([0] => "info1", [1] => "info2", etc.))
    [actor] => Array([0] => "second name", [1] => Array([0] => "actor 2 info1", etc.))
)

Happy to clarify and share more code!很高兴澄清和分享更多代码!

Cheers!干杯!

[EDIT: 1] [编辑:1]

In response to Pupil:回复小学生:

I have updated my Div's to look like this:我已经更新了我的 Div 看起来像这样:

<div class="actorTest">

   <div name="actorDivPlace">
       <input type="text" name="actorNameTest[name]">
       <div name="actorInfoDiv">
           <input type="text" name="actorNameTest[name][]">
           <input type="text" name="actorNameTest[name][]">
        </div>
   </div>

    <div name="actorDivPlace">
        <input type="text" name="actorNameTest[name]">
        <div name="actorInfoDiv">
            <input type="text" name="actorNameTest[name][]">
            <input type="text" name="actorNameTest[name][]">
        </div>
    </div>

</div>

and when I run print_r($_POST["actorNameTest"]) I only get the last actor:当我运行print_r($_POST["actorNameTest"])我只得到最后一个演员:

Array ( [name] => Array ( [0] => second info [1] => second second info) ) 

1) PHP cannot access anything from your <div> . 1) PHP 无法从您的<div>访问任何内容。 Only html can do that and PHP will only pick values from your <input> which is looking very bad.只有 html 可以做到这一点,PHP 只会从你的<input>选择值,这看起来很糟糕。

2) actorInfoDiv[] and actor[] are both invalid HTML names (They can neither be resolved correctly by CSS or JavaScript). 2) actorInfoDiv[] 和 actor[] 都是无效的 HTML 名称(CSS 或 JavaScript 都无法正确解析它们)。

3) You shouldn't be using name to identify <div> 's. 3) 您不应该使用name来标识<div> You should be using one of id or class .您应该使用idclass

4) Try using the following structure: 4)尝试使用以下结构:

<div name="actorsDivHTML"> <!-- I kept name here so I wouldn't mess up with your javascript code. You should really consider using an id for this. -->
<!-- OR -->
<!-- <div id="actorsDivHTML"> -->

    <div class="actor">
        <input type="text" name="actor[name][]">
        <div class="actorInfoDiv">
            <input type="text" name="actor[info1][]">
            <input type="text" name="actor[info1][]">
        </div>
    </div>

    <div class="actor">
        <input type="text" name="actor[name][]">
        <div class="actorInfoDiv">
            <input type="text" name="actor[info2][]">
        </div>
    </div>

</div>

That makes a more logical and accurate HTML code.这使得 HTML 代码更加合乎逻辑和准确。

Php to parse the data php解析数据

<?php

// We need to ensure that the data exist.
if(isset($_POST['actor'])){

    $parsed_data = [];

    // We will be using this number to get the info for 
    // the current name in the list when we iterate 
    // through the names.
    $i = 1;

    // Check the sent data by their names...
    foreach($_POST['actor']['name'] as $key => $value){

        // Add the name to an array
        // Looking at: [0] => "name"
        $array = [$value];

        // Create an array to hold the info's
        $array_infos = [];

        // Get all info posts that associates with the 
        // name in the current position
        // Looking at: [1] => Array([0] => "info1", [1] => "info2", etc.)
        foreach($_POST['actor']["info{$i}"] as $value_info){

            // Append the infos into the value_info array
            // NB: This makes sure that our data is exactly 
            // in the order the user enters it.
            array_push($array_infos, $value_info);
        }

        // Add the infos unto the end of the main array.
        array_push($array, $array_infos);

        // Add the main array to the array of parsed data results.
        // Looking at: [actor] => Array([0] => "name", [1] => Array([0] => "info1", [1] => "info2", etc.))
        array_push($parsed_data, ['actor' => $array]);
        $i++;
    }

    // Do anything you want with the result.
    // I choose to print it out here.
    print_r($parsed_data);
}

?>

Here is a ready to run example:这是一个准备运行的示例:

<?php

// We need to ensure that the data exist.
if(isset($_POST['actor'])){

    $parsed_data = [];

    // We will be using this number to get the info for 
    // the current name in the list when we iterate 
    // through the names.
    $i = 1;

    // Check the sent data by their names...
    foreach($_POST['actor']['name'] as $key => $value){

        // Add the name to an array
        // Looking at: [0] => "name"
        $array = [$value];

        // Create an array to hold the info's
        $array_infos = [];

        // Get all info posts that associates with the 
        // name in the current position
        // Looking at: [1] => Array([0] => "info1", [1] => "info2", etc.)
        foreach($_POST['actor']["info{$i}"] as $value_info){

            // Append the infos into the value_info array
            // NB: This makes sure that our data is exactly 
            // in the order the user enters it.
            array_push($array_infos, $value_info);
        }

        // Add the infos unto the end of the main array.
        array_push($array, $array_infos);

        // Add the main array to the array of parsed data results.
        // Looking at: [actor] => Array([0] => "name", [1] => Array([0] => "info1", [1] => "info2", etc.))
        array_push($parsed_data, ['actor' => $array]);
        $i++;
    }

    // Do anything you want with the result.
    // I choose to print it out here.
    print_r($parsed_data);
}

?>
<form method="post">

    <div name="actorsDivHTML"> <!-- I kept name here so I wouldn't mess up with your javascript code. You should really consider using an id for this. -->
    <!-- OR -->
    <!-- <div id="actorsDivHTML"> -->

        <div class="actor">
            <input type="text" name="actor[name][]">
            <div class="actorInfoDiv">
                <input type="text" name="actor[info1][]">
                <input type="text" name="actor[info1][]">
            </div>
        </div>

        <div class="actor">
            <input type="text" name="actor[name][]">
            <div class="actorInfoDiv">
                <input type="text" name="actor[info2][]">
            </div>
        </div>

    </div>

    <button>Submit</button>
</form>

Following are guidelines:以下是指导方针:

1) Only HTML <form> elements are posted. 1) 仅发布 HTML <form>元素。 No <div>s will be posted.不会发布<div>s

2) You need to use multiple values form elements for getting vector data. 2)您需要使用多个值表单元素来获取矢量数据。

   <div name="actor">
        <input type="text" name="actorName[name]">
        <div name="actorInfoDiv[]">
            <input type="text" name="actorName[name][]">
            <input type="text" name="actorName[name][]">
        </div>
    </div>

Post the form, and print the $_POST , you will get required array.发布表单,并打印$_POST ,您将获得所需的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM