2010-02-23 60 views
2

我有這樣的形式:幫助陣列和郵政

<form action="" method="post"> 
    <label for="color">Color:</label> 
    <input type="text" style="width: 195px;" name="color" id="color" value="<?php echo $item; ?>"> 
    <input value="Save" type="submit" /> 
</form> 

而下面的PHP捕捉到的內容:

if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0) { 
    $thisarray[] = $_POST['color']; 
    SetProfileField('usercss', $USER->id, implode(',',$thisarray)); 
} 

我如何添加一個新的字段表格,然後添加到要保存的數組?

回答

3

如果更改HTML代碼,則可以將新字段添加到表單中。有許多不同的表單元素,看看是否有你需要的東西在這裏:http://www.w3schools.com/html/html_forms.asp

你可以用$_POST['key']訪問每個元素的值,所以將它添加到你會寫$thisarray[] = $_POST['key']陣列。注意:將替換爲表單元素的實際名稱。

全舉例:

HTML:

<form action="" method="post"> 
    <label for="color">Color:</label> 
    <input type="text" style="width: 195px;" name="color" id="color" value="<?php echo $item; ?>"> 
    <input name="the_new_element" /> 
    <input value="Save" type="submit" /> 
</form> 

PHP:

if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0) { 
    $thisarray[] = $_POST['color']; 
    $thisarray[] = $_POST['the_new_element']; 
    SetProfileField('usercss', $USER->id, implode(',',$thisarray)); 
} 

我命名領域the_new_element,這種變化到任何你想要的。當然,你也應該清理內容。