gpt4 book ai didi

php - 通过 php 函数动态添加表单字段作为 "array"而不是值

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:42 24 4
gpt4 key购买 nike

我有一个表单,允许用户在需要时添加更多表单字段。我需要通过 php 在电子邮件中发布值,但它总是以“数组”而不是值的形式返回。我是新手,只是想不通问题。

这是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++;
}
}

这是 html 表单(我删除了大部分以节省时间):

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

这是 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\r\n"
."Reply-To: $from\r\n"
."Content-Type: text/html; charset=iso-8859-1\n"
."X-Mailer: PHP/" . phpversion());

最佳答案

改变:

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

收件人:

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

您包含在以下代码中的代码没有为您的电子邮件做任何事情:

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

你可以这样做:

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

然后更新电子邮件以将 {$myInputs} 反射(reflect)到 {$message}

关于php - 通过 php 函数动态添加表单字段作为 "array"而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10937001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com