0

我使用此循环显示一个包含用户列表的页面:

foreach ($mytaxonomies as $mytaxonomy) :  setup_postdata($mytaxonomy);
  echo $mytaxonomy->name; // print the name of the user
  echo '<a>Send email</a>';
endforeach;

该对象$mytaxonomy包含许多值,例如当前用户的电子邮件$mytaxonomy->email

单击链接(发送电子邮件)会显示一个模式叠加层,其中包含用于向该用户发送电子邮件的表单。表单将邮件发送到变量中指定的电子邮件地址,$to但我无法将其分配$mytaxonomy->email给该变量(取决于单击了哪个链接)。

我需要类似的东西

<?php $to = $mytaxonomies[...]->email; ?>

每次我点击不同的用户时都会发生$mytaxonomies[...]->email变化(因为显然每个用户都有不同的电子邮件)。

编辑: $mytaxonomies 是包含所有用户及其信息的数组

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] => ...
        )

    ...
)
4

2 回答 2

1

使用 ajax 发送电子邮件。或者在另一个页面中。页面加载后,您无法在页面中设置 php 变量。

阿贾克斯示例:

$(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;
?>

在 sendmail.php 中,您可以使用 POST 示例获取变量 mail:$to = $_POST["mail"];

HTML 表单:

<form id="myform" style="display:none" action="sendmail.php">
 ...
 <input name="to">
 <input name="from">
 ...
</form>

jQuery:

$(document).on("click", "a", function(){
  var mail = $(this).prop("href");
  $("#myform").show();
  $('#myform input[name="to"]').val(mail);
});

您将不再需要 ajax。该表格会将您发送到 sendmail.php。注意:“...”是您表单的其余部分 :)

于 2013-04-24T08:54:05.790 回答
-1

!!!!!不要相信来自客户端的任何价值

<?
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;
?>

发送邮件.php

<?
$mail = $_GET['mail'];
mail($mail, 'My Subject', 'message');
?>

更新>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<?
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;?>

发送邮件.php

<?
//just for demo send mail function, dont copy and use
$mail = $_POST['mail'];
mail($mail, 'My Subject', 'message');
?>
于 2013-04-24T09:14:57.310 回答