tags:

views:

47

answers:

1

I have this form:

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

And the following PHP to capture contents:

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

How can I add a new field to the Form and then add that to the array to be saved?

+3  A: 

You can add new fields to the form if you alter the HTML code. There are many different form elements, see if there is something you need here: http://www.w3schools.com/html/html_forms.asp

You can access the value of each element with $_POST['key'], so to add it to the array you would have to write $thisarray[] = $_POST['key']. Note: Replace key with the actual name of the form element.

Whole Example:

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));
}

I named the field *the_new_element*, change this to whatever you want. Of course you should also sanitzie the contents.

Larry_Croft