How to process data "each other"?

I am working on a script that essentially has a “each other” function, but I'm trying to find a better way to capture and process data.

For instance:

  • I will display 5 pairs of fields requesting a name and email address, each pair will be $ friend_name1 / $ friend_email1, $ friend_name2 / $ friend_email2, etc., which allows you to send 5 friends at an email at a time?

  • Am I showing one pair and using JavaScript to allow the user to continue adding more friends using the variable naming convention as # 1?

  • I will display them as suggested in # 1 or # 2, but then send an array, for example. $ friend_email [] / $ friend_name [], etc.

What is the best way to capture data?

And what's the best way to process data?

If you get an array, for example, in # 3, will you skip every $ friend_name $ _POST? Do you save it in another array? How do you guarantee that the correct name / email combination stays together if, for example, the user does not add a "name" for a third friend?

How do most people do this so that they can remain flexible during grip and accuracy during processing? I'm really looking for logic here, although the sample code is very much appreciated.

One of the things I'm tracking is who means who. For example, if A means that B and B are buying something, A will win. Therefore, accuracy and safety are very important to me.

Thanks Ryan

+1
source share
2 answers

. javascript , , , . , , -

<input type="text" name="friend_name[0]" />
<input type="text" name="friend_email[0]" />
<input type="text" name="friend_name[1]" />
<input type="text" name="friend_email[1]" />

.. PHP script $_POST. , ​​( ) , ( print_r ($ _ POST))

Array
(
    [friend_name] => Array
        (
            [0] => test_name
            [1] => test_name2
        )

    [friend_email] => Array
        (
            [0] => email@something.com
            [1] => email@two.com
        )

)

( javascript "" ) count(); . :

for($i=0;$i<count($_POST['friend_name']);++$i)
{
    if($_POST['friend_email'][$i] == NULL) continue; // Ignore NULL e-mails
    // Process data blah
}

, , , PHP foreach() (, )

foreach($_POST['friend_name'] as $key => $val)
{
    if($_POST['friend_email'][$key] == NULL) continue; // Ignore NULL e-mails
    // Process $val and $_POST['friend_email'][$key]
}

, ! !

.

+1

, - , , , . , , , " " . , , , .

, , , , , .

<input type="text" name="name[0]" value="First Friend Name" />
<input type="text" name="email[0]" value="First Friend E-mail" />

<input type="text" name="name[1]" value="Second Friend Name" />
<input type="text" name="email[1]" value="Second Friend E-mail" />

foreach ($_POST['email'] as $index => $value) {
    $name = $POST['name'][$index];
    $email = $_POST['email'][$index];
}

$name $email , .

+1

Source: https://habr.com/ru/post/1781363/


All Articles