无法发送具有相同名称的多个输入的值

时间:2014-08-17 00:47:51

标签: php html input

我在方法POST的表单中输入了相同名称(institution[])。我试图通过$_POST["institution"]使用这些输入,但返回一个字符串而不是数组。有什么问题?

HTML代码:

<table class='p1'>
<tr>
    <td>Üniversite/Kurum</td>
    <td><input type='text' name='institution[]' maxlength='150' onkeypress='return isGoodKey(event)' value=''></td>
</tr><tr>
    <td>Program</td>
    <td><input type='text' name='program[]' maxlength='150' onkeypress='return isGoodKey(event)' value=''></td>
</tr><tr>
    <td>Ülke</td>
    <td><input type='text' name='country[]' maxlength='40' onkeypress='return isGoodKey(event)' value=''></td>
</tr><tr>
    <td>Şehir</td>
    <td><input type='text' name='city[]' maxlength='100' onkeypress='return isGoodKey(event)' value=''></td>
</tr>
</table>
<table class='p1'>
    <tr>
    <td>Üniversite/Kurum</td>
    <td><input type='text' name='institution[]' maxlength='150' onkeypress='return isGoodKey(event)' value=''></td>
</tr><tr>
    <td>Program</td>
    <td><input type='text' name='program[]' maxlength='150' onkeypress='return isGoodKey(event)' value=''></td>
</tr><tr>
    <td>Ülke</td>
    <td><input type='text' name='country[]' maxlength='40' onkeypress='return isGoodKey(event)' value=''></td>
</tr><tr>
    <td>Şehir</td>
    <td><input type='text' name='city[]' maxlength='100' onkeypress='return isGoodKey(event)' value=''></td>
</tr>
</table>

PHP代码:

$institution[] = $_POST['institution'];
$program[] = $_POST['program'];
$country[] = $_POST['country'];
$city[] = $_POST['city'];

这就是我var_dump($_POST)时得到的:

array(6) { ["camp_count"]=> string(1) "2" ["institution"]=> string(3) "mom" ["program"]=> string(3) "dad" ["country"]=> string(0) "" ["city"]=> string(0) "" ["submit"]=> string(5) "Devam" } 

更新 这是isGoodKey(event)函数

function isGoodKey(evt){
  var charCode = (evt.which) ? evt.which : event.keyCode
  var charCode = evt.which || evt.keyCode;
  var charTyped = String.fromCharCode(charCode);
  var myChars = new Array("A","B","C","Ç","D","E","F","G","Ğ","H","I","İ","J","K","L","M","N","O","Ö","P","R","S","Ş","T","U","Ü","V","Y","Z","1","2","3","4","4","5","6","7","8","9","0",",",":",".","/","a","b","c","ç","d","e","f","g","ğ","h","ı","i","j","k","l","m","n","o","ö","p","r","s","ş","t","u","ü","v","y","z","Q","q","W","w","x","X"," ","@"); 
  if((myChars.indexOf(charTyped) != -1)||charCode==8){
      return true;
  }else{
      alert("Bu alana kullandığınız karakterlerle giriş yapılamaz!");
      return false;
  }
}

更新 在这里您可以找到整个文档: http://codepad.org/WgJzApco

更新 好吧,那只是因为我的愚蠢。我在第一个用js之后添加了表格,js中的名字没有数组括号([])。现在没问题。

4 个答案:

答案 0 :(得分:0)

在你的PHP中,你已经将它们作为数组接收了,所以不要再将它们放入数组中。试试这个:

$institution = $_POST['institution'];
$program = $_POST['program'];
$country = $_POST['country'];
$city = $_POST['city'];

答案 1 :(得分:0)

这里你不需要方括号。

$ _ POST [&#39;机构&#39;]拥有一个数组。因此,您不要尝试将该数组放入另一个数组中。

如果你有:

$array = array(1, 2, 3);

你想将$ array的内容复制到另一个变量$ array2,你可以使用:

$array2 = $array;

因此,从$ _POST [&#39; index&#39;]复制数组是完全相同的。

将您的php更改为:

$institution = $_POST['institution'];
$program = $_POST['program'];
$country = $_POST['country'];
$city = $_POST['city'];

答案 2 :(得分:0)

您的问题是尝试将[]用于文本输入。尝试在变量名中强制使用索引,如下所示:

<input type="text" name="institution[0]" />
...
<input type="text" name="institution[1]" />

<强>更新 我刚刚运行了这段代码,它没有问题。

<?php

    echo '<pre>'; print_r($_POST);

?>
<html>
    <form method="post">
        <input type="text" name="institution[]" value="1" />
        <input type="text" name="institution[]" value="2" />
        <input type="text" name="institution[]" value="3" />
        <input type="submit" />
    </form>
</html>

Array
(
    [institution] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
)

您的PHP和Apache版本是什么?

答案 3 :(得分:0)

我发现了问题。我实际代码中的名字并不相同。当我把所有的名字都和[]相同时,一切正常。