0

I have a form which will have dynamic value and it will check that property id and then save it to the database.

For example in my database there is a table with title having the id=1, type having the id=2, description having the id=3, and after the form is submitted it will check if the field is title or type or description and it will save it in database that is if it is title it will save value of field title with propertyid value 1.

     <form method="post" action="something.php">
     <input type="text" name="field[][title]" value="edison">
     <input type="text" name="field[][type]" value="book">
     <input type="text" name="field[][description]" value="some description">
     </form>

it is not inputting normal array in php with using foreach, I am not understanding how to get the value inside the index of the array to check with the sql database, that is to check if the field[][title]" then title has id 1 and if the field is field[][description]" it will check the sql for the property id of description that is 3

  • 1
    Possible duplicate of [How to get form input array into PHP array](https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array) – Alon Eitan Aug 22 '19 at 14:27
  • `field[1][title]`? – u_mulder Aug 22 '19 at 14:27
  • it is not inputting normal array in php with using foreach, I am not understanding how to get the value inside the index of the array to check with the sql database, that is to check if the field[][title]" then title has id 1 and if the field is field[][description]" it will check the sql for the property id of description that is 3 – P.Srivastava Aug 22 '19 at 14:32

2 Answers2

0

You need to look at it in reverse.

You first need to query your database so that you have a mapping of which field associates with which ID.

Then, when your information is posted, you can iterate over that mapping, detect if they have been posted, and use them accordingly:

$mapping = loadFieldNamesToFieldId();

/*
  mapping should look something like: 

  $mapping = [
      'title' => 1,
      'type' => 2
  ];
*/

foreach ($_POST as $field_name => $field_value) { 
  if (isset($mapping[$field_name]]) { 
    $id = $mapping[$field_name];

    // at this point you know that the user submitted a field which
    // had $field_value, and which ID it relates to in your database
  }
}

At which point you can just format your form as so:

<input type="text" name="title" value="edison">
Mark R
  • 136
  • 2
  • 6
  • I agree to the reverse way, but if that is formated that way, in my process.php page, the post method has to be like this $_POST['title'], and if there is 100 property fields then I have to write 100 if cases for it and if there is a new property field is added the process has to be manually updated every time. that is where I am stuck. – P.Srivastava Aug 22 '19 at 15:07
  • Nah. Because you're going to use that very same mapping information to generate the form automatically: ` foreach ($mapping as $field_name => $id) { echo ''; } ` – Mark R Aug 22 '19 at 15:08
  • not understanding can you write a pseudo code as an example – P.Srivastava Aug 22 '19 at 15:09
  • Think of it this way, you're never going to write an "if" statement that deals with a specific property. When you're creating your form, you're just going to query your database for which properties it supports, and create a form field for each. When you receive the data back, you're going to query your database for all of those properties it supports again, and if the form has submitted one, you're going to store that information (because by knowing which field name it is, you also know its ID, because they're stored together in the DB). – Mark R Aug 22 '19 at 15:11
  • I am not getting the part "When I receive the data back", how am i receiving the data back if I dnt know the fields – P.Srivastava Aug 22 '19 at 15:21
  • When the form is submitted, you get all the fields back. PHP takes care of this for you. All you need to do is know what they're called (they're in your database, so you do) to be able to access them. – Mark R Aug 22 '19 at 15:28
0

I found this answer and thought of posting this as this relates to the answer

foreach ($_POST as $param_name => $param_val) 
{
echo "Param: $param_name; Value: $param_val<br />\n";
}