doulan9287 2012-06-07 17:42
浏览 39
已采纳

动态添加表单字段通过php函数发布为“数组”而不是值

I have a form that allows the user to add more form fields if needed. I need to post the values through php in an email, but it keeps coming back as "array" instead of the values. I am a novice and just can't figure out the problem.

Here is the javascript:

    var counter = 1;
    var limit = 50;
    function addInput(divName){
        if (counter == limit)  {
          alert("You have renter code hereeached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}

Here is the html form (I deleted most of it to save time):

    <form id="teamreg" class="appnitro"  method="post" action="teamreg.php" >
        <input name="recipient" type="hidden" id="recipient" value="kim@ka-kingdesigns.com" />
        <ul>
            <li>
                <h3>Contact Information</h3>
            </li>   
            <li id="li_1" >
                <label class="description" for="company">Company/Organization </label>
                <div>
                    <input id="company" name="company" class="element text large" type="text" maxlength="255" value=""/> 
                </div> 
            </li>

            <li id="li_3" >
                <label class="description" for="contactname">Team Cooridinator</label>
                <span>
                    <input id="element_2_1" name= "contactname_1" class="element text" maxlength="255" size="8" value=""/>
                    <label>First</label>
                </span>
                <span>
                    <input id="element_2_2" name= "contactname_2" class="element text" maxlength="255" size="14" value=""/>
                    <label>Last</label>
                </span> 
            </li>       

<li class="section_break">
            <h3>Team Members</h3>
            <p>Please list everyone planning on volunteering (including spouses, children, etc.)<br/>
</p>
        </li>

<li>
<div id="dynamicInput">
          Entry 1<br><input type="text" name="myInputs[]">
     </div>
     <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">


</li>
            <li class="buttons">
                <input type="hidden" name="form_id" value="421421" />

                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
        </li>
            </ul>
        </form> 

Here is the php:

header("location: registrationthankyou.html");
$company = $_POST['company'];
$coordinator = "{$_POST['contactname_1']} {$_POST['contactname_2']}";
$myInputs = $_POST['myInputs'];
foreach ($myInputs as $eachInput) {
     echo $eachInput . "<br>";
}
$to = $_POST['recipient']; //"user@example.com";
$subject = "Day of Caring Team Registration".$_POST['subject'];
$comments = "
<strong>Company:</strong> {$company}<br>
<strong>Team Coordinator:</strong> {$coordinator} <br>
<strong>Team Members</strong> {$myInputs}<br><br>
mail($to,$subject,$comments,"From: $from
"
    ."Reply-To: $from
"
    ."Content-Type: text/html; charset=iso-8859-1
" 
    ."X-Mailer: PHP/" . phpversion());
  • 写回答

1条回答 默认 最新

  • dqv2743 2012-06-07 18:00
    关注

    Change:

    <strong>Team Members</strong> {$myInputs}<br><br>
    

    To:

    <strong>Team Members</strong> ".implode(",", $_POST['myInputs'])."<br /><br />
    

    The code you have included in the following is not doing anything for your email:

    foreach ($myInputs as $eachInput) {
         echo $eachInput . "<br>";
    }
    

    You can do:

    foreach ($myInputs as $eachInput) {
         $message .= $eachInput . "<br>";
    }
    

    And then update the email message to reflect {$myInputs} to {$message}

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

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?