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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)