2013-04-28 40 views
-1

我曾多次尝试使值更易于放置。这是我的意思把价值观与阵列,因为我并不需要输入更多的价值的话,他可以把它自动的。例如,我写:

<?php 

$Value1 = array (
      'Somevalue1' => 'SomeValue2', 
      'SomeValue1' => 'SomeValue2', 
      'SomeValue1' => 'SomeValue2', 
      ); 

// This is method what i want to put in, but i dont think that its right 
$Value1[0] = strip_tags($_POST['.Value1[0].']); 

// Its the meaning that he put so out: 

$SomeValue1 = strip_tags($_POST['SomeValue2']); 
$SomeValue1 = strip_tags($_POST['SomeValue2']); 
$SomeValue1 = strip_tags($_POST['SomeValue2']); 

?> 

我没有用太多的经验阵列,我刚学...

回答

0

我猜这是你想做的事:

// create an array with key = field-name in form 
$val = array(
    'name' => null, 
    'lastname' => null, 
    'date_of_birth' => null, 
    'favorite_color' => null); 

if (isset($_POST['submit'])) { // form has been sent 

    // retrieve values from $_POST and store it in $val 
    foreach ($val as $key => $value) 
     $val[$key] = $_POST[$key]; 

    // show the result 
    echo "<pre>"; 
    var_dump($val); 
    echo "</pre>"; 

} // if isset 

看到现场演示:http://codepad.viper-7.com/j03DtO

+0

喜之道,这就是耶士什么我一直在寻找..非常感谢你为共享,但如果你不介意,我只是有一个问题..你怎么能添加strip_tags安全$ _POST在这种编码?谢谢 – 2013-04-28 20:27:06

+0

@DavySays使用'PDO'并让它处理转义数据。搜索PDO和mysql – michi 2013-04-28 20:40:17

+0

好的,我会搜索那个。非常感谢您的帮助和您的代码,如果我是对的,我确实按下了绿色按钮以获得答案。 – 2013-04-28 20:49:13