How to process tell-a-friend data?

2019-08-10 04:28发布

I'm working on a script that essentially has tell-a-friend functionality, but I'm trying to figure out the best way to capture and process the data.

For example:

  1. Do I display 5 pairs of fields asking for name and email, each pair being $friend_name1/$friend_email1, $friend_name2/$friend_email2, etc, which allows you to email 5 friends at a time?

  2. Do I display one pair and use JavaScript to allow the user to keep adding more friends using the variable naming convention as #1?

  3. Do I display them as suggested in #1 or #2, but then submit an array e.g. $friend_email[]/$friend_name[], etc.

What's the best way to capture the data?

And then what's the best way to process the data?

If you get an array, such as in #3, do you then loop through each $friend_name $_POST? Do you store it in another array? How do you ensure that the correct name/email combination stays together, if for example, the user didn't add a "name" for the third friend?

How do most people do it so they can remain flexible during the capturing and precise during the processing? I'm really looking for logic here, although sample code is much appreciated.

One of the things I'm tracking is who is referring who. For example, if A refers B and B buys something, A will benefit. So accuracy and security are very important to me.

Thanks, Ryan

2条回答
Lonely孤独者°
2楼-- · 2019-08-10 04:46

I would recommend the array. You could do javascript or otherwise, but make sure that the processing end can see how many referrals there are. To ensure the correct e-mail and name stay together, try something like

<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]" />

and so on. From there, your PHP script will simply match the array indices in the $_POST var. For instance, if this form was submitted (as above) with some arbitrary data, it would format like this (this is the print_r($_POST); data)

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

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

)

An idea on looping through these (if you implemented javascript with a potential for an "infinite" number of referrals) would be to use the count(); function. Implementation would look something like this:

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

Or, if those names where entered out of order, PHP's foreach() feature is very nice and can accomplish the same thing (probably more efficiently for this type of error checking)

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]
}

Hope this helps! Good luck!

Dennis M.

查看更多
贪生不怕死
3楼-- · 2019-08-10 04:54

These are UI questions, and the answer is whatever UI you think works best for your site. You might have a design where a fixed number of fields fits better, or you might have a demographic where the "add another field" button is too confusing. Either way the code that processes the form doesn't have to be related to how you present the UI.

Regarding making sure your fields stay associated if you name them as an array, specify the indexes yourself rather than using open brackets, and you won't have that problem.

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

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

Then in your code,

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

Within the loop, $name and $email will be from the same set of fields in the form, though they can still be blank.

查看更多
登录 后发表回答