I build different forms dynamically. I query a database where i get all the fields related to the current form ID. (i have 2 tables, one with the list of fields and one with the list of fields associated to a products).I make a join and i can get all the informations in order to build the form.
It works fine.
My question is related to the treatment of the datas sent with $_POST. Usually, when i know all the fields, i get the data like this :
$nom = htmlentities($_POST['nom']);
$prenom = $_POST['prenom'];
I can’t do that since i have 300 fields all with a different name.
Can i get all the post value with PHP without knowing the name field ?
PHP has a function for that called extract, though it is not exactly recommended to be used with user data, since a user could send any key that you're not expecting.
To achieve the same, you can also use a loop:
foreach($array as $key => $value) {
$$key = $value;
}
The security problems are the same though.
Related
Is there a way to post form input dynamically.
For example instead of
$name = $_POST['name'] e.t.c
you have something like
for each form input POST to database. I need a dynamic way so that if my form changes I don't have to change my POSTing script as well.
You can loop through your $_REQUEST data at server side using foreach() function like:
foreach($_REQUEST as $req)
{
//all your form data will be available here
}
Note: I used $_REQUEST here but you can use $_GET or $_POST as per your requirement
You could have some fixed fields that you will always have (name / username / e-mail address / etc.) and store these in a fixed table.
Then you get the ID of that row and you store the rest of the fields as key - value pairs in another table where the key is the name of the input and the value its value. In that table you also add the post ID so that you know which post the pair belongs to. If needed, you could add some additional fields with properties like what kind of field it is, the order, etc..
Example, I want the drop down value to be 1278|Toy Name
I would like to then separate via the | and spost the id "1278" in an id field
and "Toy 2" in a toy field.
I am actually using FormTools (formtools.org) for all of my forms and this is one thing I just can not get my head around.
A little background: This is for a client and needs to be this way. they need the ID submitted as well as the Toy Name into separate Db tables.
Any help would be greatly appreciated.
You could split the submitted value based on the | and use the retrieved values to insert into the database.
$toyInput = $_POST["toyInput"];
$toySplit = explode("|", $toyInput);
$toyID = $toySplit[0];
$toyName = $toySplit[1];
// Now insert $toyID and $toyName into your DB in separate fields.
Be aware that this has no validation, a user could easily put fake ID's, names, or a value with no "|" and cause your script to error out. Be sure to add extra checks for these cases!
You can also use list that assign values to a list of variables.
$toyInput = $_POST["toyInput"];
list($toyID, $toyName) = explode("|", $toyInput);
I am working on a booking script in PHP and I need to get their values. To be clear the select is not multiple value select where you select many values in one select. It is just plain and simple select fields in a form. The problem is i can't be sure how many of them there are and what their names are. Is there a way to get their names and and values without knowing how many of them there are and what their names are when I click the submit button? I use a while loop to print them. I was suggested use sessions to achieve this but I am not exactly sure how I would implement it.
All data that the html part is tranfering to the php part of the page is sent to a variable ($_GET, $_POST). Which one is defined by the method you are using for the form itself. The variable itself is built as an array with fieldname as the key of the array element and the value (either value of the selected element for select fields, or the input value for input fields, ....) as value of the array element.
If you want to parse through all elements that you have in the form field, then you can use foreach (it gets a bit more complicated if you don't want to parse through all elements......then I would suggest that you make sure that all the fields that you want to process begin with a specific "tag" in their name, which only they have. As an example c_Field1, c_Field2, Field3, Field4,.... with c_ being the tag....you can then either use string functions on the key (which is equal to the fieldname) or you can use arrayfunctions to only get these fields).
As an example with foreach if you want to get all fields beginning with 'c_':
foreach ($_POST as $fieldname => $value)
{
if (str_replace('c_','',$fieldname)!=$fieldname)
{
//field to be processed so use $value
}
}
If you submit them, they are in the POST variable (well, could be GET, but probably not). That thing is an array, so you can use all the fancy array functions to find out what's inside
for instance:
http://php.net/manual/en/function.array-keys.php
or just a plain old foreach I guess.
I am working in the confines of a CMS system, which defines certain fields which can be used to make forms for use within the application in PHP.
I am making use of the inputSmartSearch field, which is basically similar to Google suggest.
It allows me to define an SQL query, and then displays records that match as I type in my search.
For my smartsearch, I have chosen it to search through three fields in a different table, and to display those fields concatenated together.
I use define my field like so:
$theinput = new inputSmartSearch($db, "chooseguests", "Choose Guests");
The last parameter is the name of the SQL query to execute.
This works fine, and a guest can be located by searching his last or first name.
However, I have implemented this smartsearch what is meant to be the page to add a sales order.
In each sales order stored in the sales table, distinct from the guest table, I also want to have the firstname and lastname of the guest making the order.
The design of the sales order table has two separate fields for firstname and lastname.
Using smartsearch, I cannot find any way to tokenize the selected input and insert it back into the field.
If I have a smartsearch and can search by firstname or lastname it shows the result as just one fields, and I want it to save the firstname to the firstname field and the lastname to the lastname field.
Each form defined has an include file which defines the function for inserting a record and such, like so:
function prepareVariables($variables){
// if ($variables["webaddress"]=="http://")
// $variables["webaddress"] = NULL;
return $variables;
}
function updateRecord($variables, $modifiedby = NULL, $useUuid = false){
$variables = $this->prepareVariables($variables);
return parent::updateRecord($variables, $modifiedby, $useUuid);
}
function insertRecord($variables, $createdby = NULL, $overrideID = false, $replace = false, $useUuid = false){
$variables = $this->prepareVariables($variables);
return parent::insertRecord($variables, $createdby, $overrideID, $replace, $useUuid);
}
However, I am unsure of how I could modify the insert or update functions to do what I need them to do, or if that is even the correct approach.
Should I be looking for a complex sql query? hidden fields with the content autopopulated from the result of my inputSmartSearch? Something else?
Not sure what's possible with the setup you've got going, but... my first reaction would be to return from the db the names combined with tokens - like - ',' So Smith, John. Then, when getting ready to send back, to split those up ( based upon the known token ). Now where that goes what you've got there, I do not know.
You just need to set the starting value of the smart search manually using the values from the database. Then when the data is posted to the server you can parse and append to the array that is used to store the data in the database. At least that seems like it should work.
this is probably quite simple but I could do with some help.
I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.
So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.
I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.
Also, is there a JQuery or Javascript alternative?
Just looking for a few pointers to get me on my way.
Thanks in advance,
Micanio
You could either do this on the server-side or through JS.
JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).
PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:
$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];
From there just store the $finalValue however you'd like.
$data = array();
for ($i=0;$i<6;$i++) {
$data[$i] = array();
for ($j=0;$j<6;$j++)
$data[$i][$j] = rand(0,100);
}
This should create an array similar to what you have described.
You can then access it like so...
echo $data[0][3];
Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.
If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.
This kind of functionality is usually done with javascript not server side php.
http://javascript.internet.com/forms/country-state-drop-down.html