doushanlv5184 2013-06-22 15:41
浏览 43
已采纳

如何使用PHP处理动态生成的javascript代码并将结果作为电子邮件发送?

this is the javascript code

$(function () {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function () {
        $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
        i++;
        return false;
    });

    $('#remScnt').live('click', function () {
        if (i > 2) {
            $(this).parents('p').remove();
            i--;
        }
        return false;
    });
});

this is the html code

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts">
            <input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
        </label>
    </p>
</div>

i'm trying to collect all the inputs generated and process them with php

  • 写回答

1条回答 默认 最新

  • douzha5990 2013-06-22 15:58
    关注

    1- Add a <form> tag before #p_scents so all the new <input type="text" ... are inside the form. Close the tag after #p_scents with ` and add a submit button or javascript function. In the form action you can add the same name of the file you are in (assuming it can handle php), otherwise create a php file.

    2- Then you need to create a foreach loop in the php file to search for all the inputs. Colect them with $_POST.

    You can do this way:
    When you add new fields you can just keep them identicall and serialize them. Use name="p_scnt[]" in all input fields. The one already in the html and identical for all the generated by javascript. This will serialize them.

    In the php file use: foreach($_POST["p_scnt"] as $value) { … }, inside the { } you can writhe your php code.

    3- About making email from the variables you will have it's recomended to use some email library to avoid header injection, but anyway here is an example:

    $to      = 'recepient@somemail.com';
    $subject = 'Subject here';
    $message = "Content";
    $message .= "more Content";
    $message .= "even more Content or a variable".$variable;
    $headers = 'From: sender@yourdomain.com' . "
    " .
    'Reply-To: sender@yourdomain.com' . "
    " .
    'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作