doulan9287 2012-06-07 09: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 10: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}

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部