简体   繁体   English

如何将文本框在php中保存为数组?

[英]How to save a textbox in php as an array?

Is there anyway to save a textboxs as an array similar to how multiple checkboxs can be saved as an array? 无论如何,是否有将文本框另存为数组的方式,类似于如何将多个复选框另存为数组? When you save multiple checkboxs to an array the value only comes up in the array when the checkbox has a check in it. 当您将多个复选框保存到一个数组时,只有复选框中有一个复选框时,该值才会出现在数组中。 Is this the same with textboxs? 文本框也一样吗? Do they only have value when they're not empty? 他们只有在不为空时才有价值吗?

I'm trying to save these textboxs as an array (I only took the part of the form that you all need to see) 我正在尝试将这些文本框保存为数组(我只使用了大家都需要看到的表单的一部分)

<form action="formemail.php" method="POST" name="rcr">
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="submit" name="sendwork" id="sendwork" value="Send Work Order"></td>

</form>

In the next page I'm trying to take these values and just output them in a simple way (later I'm going to be adding them to an email using foreach if I can)..... below is formemail.php 在下一页中,我尝试采用这些值并以简单的方式输出它们(如果可以的话,以后我将使用foreach将它们添加到电子邮件中).....下面是formemail.php

<?php
$description = $_POST["desc"];
echo $description[0];
echo $description[1];
?>

I can't get anything to echo for the life of me. 我一生都无法回应。 And I'm not sure what I'm doing wrong. 而且我不确定我在做什么错。

tried ? 试过了吗?

   if(  isset( $_POST["desc[]"] )  ) echo "is set ; )";

btw. 顺便说一句。 same multiple id's can cause you problems if you work with JS/JQuery. 如果您使用JS / JQuery,则相同的多个ID可能会给您带来麻烦。

Try to use foreach and test submitted values: 尝试使用foreach并测试提交的值:

foreach($_POST['desc'] as $post)
{
    echo !empty($post) ? $post.'<br />' : 'An empty value.<br />';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM