douweng7233 2013-04-24 08:32
浏览 7

根据单击的链接更新PHP变量

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

    ...
)
  • 写回答

2条回答 默认 最新

  • dongliuxia9495 2013-04-24 08:54
    关注

    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 :)

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本