2

I have the following code in my file:

<table width="100%" class="friends">
<tr>
    <th width="50%">Friends Name</th>
    <th>Friends Email</th>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>
<tr>
    <td><input type="text" name="friendname[]" id="friendname[]" class="validate[minSize[4]]" min="4" placeholder="John &amp; Jane Doe" style="width:80%;" /></td>
    <td><input type="text" name="friendemail[]" id="friendemail[]" class="validate[custom[email]]" min="4" placeholder="[email protected]" style="width:80%;" /></td>
</tr>

And the following as my PHP Code:

if(!empty($_POST['friendname']) && !empty($_POST['friendemail']) && is_array($_POST['friendname']) && is_array($_POST['friendemail']) && count($_POST['friendname']) === count($_POST['friendemail'])) {
    $err[] = "Please enter in your friend's details.";
}

$name_array = $_POST['friendname'];
$email_array = $_POST['friendemail'];
for ($i = 0; $i < count($name_array); $i++) {
    $name = $name_array[$i];
    $email = $name_array[$i];
}

However my code isn't echoing anything.

I got the code for the above from insert multiple fields using foreach loop

I have other fields inside my form and they work fine, but for some reason these don't. Does anyone know of a way to do what I'm looking for based on the code above? :)

0

1 Answer 1

2
foreach (array_combine($_POST['friendname'], $_POST['friendemail']) as $name => $email) {
 echo $name . " - " . $email . "<br />";
}
Sign up to request clarification or add additional context in comments.

8 Comments

thanks @BeatAlex that's really helpful! But unfortunately my code shows the arrays as being empty. I put echo $_POST['friendname'];echo $_POST['friendemail'];exit(); at the top of my submit code and it just came back ArrayArray
You can't "echo" an array in PHP. Put var_dump($_POST['friendname']);var_dump($_POST['friendemail']);exit(); on top of your page
@nahime sorry you are correct. I changed it to echo '<pre>';var_dump($_POST['friendname']);echo '</pre>';echo '<pre>';var_dump($_POST['friendemail']);echo '</pre>';exit(); and that gave me results, but I use if(!empty($_POST['friendname']) && !empty($_POST['friendemail']) && is_array($_POST['friendname']) && is_array($_POST['friendemail']) && count($_POST['friendname']) === count($_POST['friendemail'])) { for validation and that isn't giving me anything. Any ideas?
What does your var_dump return?
You're checking that both have same length. Look at the var_dump to see if that's true. BTW, have you tried @BeatAlex solution without validating before?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.