The variable name $ _POST is a variable, how to get it?

My site users can create a custom form. All fields are stored in a database with a unique identifier. When someone visits the form, the field name attribute is a * ID * field, for example

<p>Your favorite band? <input type="text" name="field28"></p> <p>Your Favorite color? <input type="text" name="field30"></p> 

After submitting the form, I use php to validate the form, but I don’t know how to get the value of $ _POST [field28] (or any number that has this field).

 <? while($field = $query_formfields->fetch(PDO::FETCH_ASSOC)) { $id = $field[id]; //this doesn't work!! $user_input = $_POST[field$id]; //validation comes here } ?> 

If anyone can help me, this is really appreciated!

+4
source share
4 answers

Add some quotation marks:

 $user_input = $_POST["field$id"]; 
+11
source

I would suggest using PHP array syntax for forms:

 <input type="text' name="field[28]" /> 

You can access this in php with $_GET['field'][28]

+5
source
 $user_input = $_POST['field'.$id]; 
+3
source

Remember that you are using string for the first part of the input name, so try something like: $user_input=$_POST['field'.$id]; . In addition, I would suggest calling them in an array to retrieve all the data:

 <?php $user_inputs=array(); while($field=$query_formfields->fetch(PDO::FETCH_ASSOC)) { $id=$field['id']; $user_inputs[]=$_POST['field'.$id]; } ?> 
+2
source

Source: https://habr.com/ru/post/1399608/


All Articles