I display a page with a list of users using this loop:
foreach ($mytaxonomies as $mytaxonomy) : setup_postdata($mytaxonomy);
echo $mytaxonomy->name; // print the name of the user
echo '<a>Send email</a>';
endforeach;
the object $mytaxonomy
contains many values like the email of the current user in $mytaxonomy->email
Clicking on the link (Send email) shows a modal overlay with a form to send an email to that user. The form sends the mail to the email address specified in the variable $to
but I'm not able to assign the $mytaxonomy->email
to that variable (depending on which link was clicked).
I need a thing like
<?php $to = $mytaxonomies[...]->email; ?>
where $mytaxonomies[...]->email
changes everytime I click on a different user (because obviously each user have a different email).
EDIT: $mytaxonomies is the array that containes all the users with their infos
print_r($mytaxonomies);
Array
(
[0] => stdClass Object
(
[term_id] => 4
[name] => John Doe
[slug] => john-doe
[email] => johndoe@email.com
[age] => ...
[phone] => ...
)
[1] => stdClass Object
(
[term_id] => 5
[name] => Jane Doe
[slug] => jane-doe
[email] => jdoe77@converge.con
[age] => ...
[phone] => ...
)
...
)
Send the e-mail using ajax. Or in another page. You can't set a php variable in a page after the page is loaded.
Ajax example:
$(document).on('click', 'a', function(){
var data = 'mail=' + $(this).prop('href');
$.ajax({
type: 'POST',
data: data,
url: 'sendmail.php',
success: function(){
alert('mail sent');
}
)};
)};
PHP:
<?
foreach ($mytaxonomies as $mytaxonomy) : setup_postdata($mytaxonomy);
echo $mytaxonomy->name; // print the name of the user
echo "<a href='{$mytaxonomy->email}'>Send email</a>";
endforeach;
?>
In sendmail.php you can get the variable mail with POST example: $to = $_POST["mail"];
The HTML form:
<form id="myform" style="display:none" action="sendmail.php">
...
<input name="to">
<input name="from">
...
</form>
The jquery:
$(document).on("click", "a", function(){
var mail = $(this).prop("href");
$("#myform").show();
$('#myform input[name="to"]').val(mail);
});
You will no longer need ajax. the form will send you to sendmail.php. Note: "..." are the rest of your form :)
!!!!!dont trust any value from client side
<?
foreach ($mytaxonomies as $mytaxonomy) : setup_postdata($mytaxonomy);
echo $mytaxonomy->name; // print the name of the user
echo "<a href='send_mail.php?mail={$mytaxonomy->email}'>Send email</a>";
endforeach;
?>
send_mail.php
<?
$mail = $_GET['mail'];
mail($mail, 'My Subject', 'message');
?>
UPDATE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<?
foreach ($mytaxonomies as $mytaxonomy) : setup_postdata($mytaxonomy);?>
<form action="send_mail.php" method="post">
<? echo $mytaxonomy->name;?>
<input type="hidden" name="mail" value="<?echo $mytaxonomy->email?>">
<input type="submit" value="Send">
</form>
<? endforeach;?>
send_mail.php
<?
//just for demo send mail function, dont copy and use
$mail = $_POST['mail'];
mail($mail, 'My Subject', 'message');
?>