1

I have a form that allows the user to add more fields if they need them. When the user does this, two more fields appear (one for link text, the other for the link address). I'm use javascript to append a number onto the input names (e.g. newLinkTitle1 + newLinkAddress1, newLinkTitle2 + newLinkAddress2, etc.).

  <input type="text" value="" name="newLinkTitle" size="50" />
  <input type="text" value="http://" name="newLinkAddress" size="50" />  

I want to check if there are variables in my POST array containing the prefix newLinkTitle and then get the newLinkAdress associated with it then add them to my database.

Currently I have this:

foreach ((strpos($_POST, 'newLinkTitle')) as $currentNewLinkTitle) { // check if newLinkTitle exists
    $num = substr($currentNewLinkTitle, 12);  // get the number following "newLinkTitle"
    $currentLinkAddress = $_POST['newLinkAddress'.$num];
    //Update query 

}
shadyyx
  • 15,825
  • 6
  • 60
  • 95
Sg1456
  • 954
  • 2
  • 10
  • 17

5 Answers5

1

The right approach would be to name the fields as arrays: newLinkTitle[] and newLinkAddress[]:

<input type="text" value="" name="newLinkTitle[]" size="50" />
<input type="text" value="http://" name="newLinkAddress[]" size="50" />

Then using Your JS code just add another couple of the fields with the very same names (newLinkTitle[] and newLinkAddress[]). And in PHP, just do something like:

foreach($_POST['newLinkTitle'] as $key => $val) {
    $currentNewLinkTitle = $val;
    $currentNewLinkAddress = $_POST['newLinkAddress'][$key];
    // save to DB
}

Do not forget to escape the values properly prior to saving to DB!

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • Your approach has some risks because you don't define array keys in html. There is a possibility that the keys will be out of sync. – Paul Annekov Jun 21 '13 at 15:04
  • @SteelRat Really? If You define the keys like that, that means, that every new value will be added as a new index. There is no need to specify the keys, never heard of that (that means that if that would be a risk, I would definitely had to hear of it ;-) ). – shadyyx Jun 21 '13 at 15:23
  • @shadyyx I would not rely on the order in which the browser sends the form elements. It's something that is not standardized. – Paul Annekov Jun 23 '13 at 14:12
0

try something like:

foreach ($_POST as $key => $value)
{
  if (strpos($key, 'newLinkTitle') !== false)  // note triple = is needed because strpos could return 0 which would be false
  {
    // do stuff with $value 
  }
}
gries
  • 1,135
  • 6
  • 29
0
// Loop thru all POST fields.
foreach($_POST as $fieldName => $fieldValue)
{ 
  // Check if field is a link title.
  if(substr($fieldName, 0, 12) == 'newLinkTitle')
  {
    // If so, get index and link address.
    $num = substr($fieldName,12);
    $currentLinkAddress = $_POST['newLinkAddress'.$num];
    // Update query 
  }
}

Remember to use mb_substr() function instead of substr(), if using UTF-8.

Marcovecchio
  • 1,322
  • 9
  • 19
0

You'll only have to adjust your loop to look like so:

foreach($_POST as $field_name=>$field_value){
    #it's the field name you want to match for similarity
    if(strpos($field_name, "newLinkTitle") !== false){
        #we have a matching field
        .....
    }
}

NOTE: $_POST is an associative array of fieldnames and the values they carry (just a reminder)

Emmanuel Okeke
  • 1,452
  • 15
  • 18
0

You need to use arrays in form names. Example:

<input type="text" value="" name="newLinkTitle[0]" size="50" />
<input type="text" value="http://" name="newLinkAddress[0]" size="50" />
<input type="text" value="" name="newLinkTitle[1]" size="50" />
<input type="text" value="http://" name="newLinkAddress[1]" size="50" />

PHP will convert form fields to array. You can use them in such way:

foreach ($_POST['newLinkTitle'] as $key => $value) {
  echo 'Link title: ' . $value . ', link address: ' . $_POST['newLinkAddress'][$key];
}

Don't forget to validate $_POST array.

Paul Annekov
  • 3,193
  • 3
  • 19
  • 31