Remove submit button value from $_POST form data PHP - php

I'm using this cycle to print all form $_POST data:
foreach($_POST as $name => $value) {
print "$name : $value<br>";
}
And at the end of result is submit button value (submit : Edit) and this cause error for me, because with this foreac cycle I'm adding data to excel document $name is cell, $value is cell value so how to remove button value from the list?

you can just unset it before you do your foreach:
unset($_POST['submit']);

Your submit button does not need to have a name attribute in your HTML. If it has no name, it will not be present in the POST data.

Just skip it with continue;
foreach($_POST as $name => $value) {
if($name == "submit") continue;
print "$name : $value<br>";
}

What about
foreach($_POST as $name => $value) {
if($name != "submit"){
print "$name : $value<br>";
}
}

What you are doing is not good practice. In this case, passing the data into an Excel spreadsheet, you are unlikely to have problems, nevertheless, this is a dangerous habit to get into.
You have designed your forms and given names to each of your inputs, so you know what indexes your $_POST array will contain ahead of time. You should explicitly reference only those values in your $_POST array and validate each one as required.
Do not forget that $_POST comes from the user and is, therefore, untrustworthy. Extra fields can be added to the $_POST array and, as it stands, your code will happily process them.
This may or may not be an issue in your code, but you should at least think about it.

Related

form fields count and names to PHP

I have form with changable content of textareas, from 1 to 5, each time with different names. I cannot modify the form itself.
how can i get number of textareas in form and names of them (it would be the best if i could do it clean in php without javascript).
the form is using method="POST" and PHP version is 5.2+
EDIT: i forgot to tell you that i have only textareas in form.
You could do something along the lines of :
$count=0;
$formElements=array();
foreach ($_POST as $key => $val)
{
$count++;
$formElements[]=$key;
}
echo "The form as $count elements.";
var_dump($formElements);
If you want the values of the post you could make a two dimensional array like this:
foreach ($_POST as $key => $val)
{
$count++;
$formElements[]=array($key => $val);
}
if you post form to php script, $_POST variable is array. then you can use something like this:
foreach($_POST as $v){} to get every field.

PHP: translate POST into simple variables?

I know this is totally wrong, I didn't write the app, I just have to make it work while I work on the new one. Seems like GoDaddy made some updates to our account so now there are some things that don't work. I already turned register globals on and a lot of things went back to normal. Now, this doesn't work...
There is a hidden field <input type="hidden" name="SUBMIT1" value="1" />
There is a check for that field
if($SUBMIT1) {
// this part never gets executed.
}
I know I can easily fix it by doing this instead...
if($_POST['SUBMIT1']) {
// this part never gets executed.
}
The problem is, this is a huge app and it's going to take a loooong time to do that everywhere. Is there a way or a setting I can turn on so that when a form is submitted $_POST['WHATEVER'] also works as $WHATEVER?
You can use extract to get the exact functionality you described:
extract($_POST);
Please note the possible safety issue here: a user could send extra data in $_POST and "overwrite" the values of existing variables in your code. You can pass a second parameter to extract to prevent these problems:
extract($_POST, EXTR_SKIP);
This will skip extracting any fields in the $_POST array that already have matching variables in the current scope.
you can try something like this:
$_POST = array('SUBMIT1' => 'SUBMIT ONE', 'SUBMIT2' => 'SUBMIT TWO');
foreach($_POST as $k => $v) {
define(''. $k. '', $v);
}
echo SUBMIT1; // and so on
foreach ($_POST as $key => $value ) {
if(is_array($value)){
foreach ($value as $k => $v ) {
$$k = $v ;
}
}else{
$$key=$value;
}
echo $key ." : " .$value."<br>";
}

How to use a foreach loop with var_export?

I have a form with some checkboxes in Drupal and I need to get the checked boxes to add them to a database. To get the values in the checkboxes I use var_export which returns an array indicating if the checkbox has been checked. After I have this array store in a variable I do this:
$checked = array();
if(is_array($data) {
foreach($data as &$value) {
if($value != 0) { //the checkbox was checked
$checked[] = $value;
}
}
However, when I print out the variable $checked there is nothing stored in it. What am I doing wrong?
The normal way to do this in Drupal would be:
$checked = array_filter($form_state['values']['name_of_checkboxes_element']);
That will give you an array of all the values selected in your checkbox element, assuming you're running this code in the submit/validate handler for the form.
Also I should mention the Devel module, it has a wonderful function called dpm() which prints the value of any variable to the messages area in a hierarchical format that you can navigate through easily.

Foreach value from POST from form

I post some data over to another page from a form. It's a shopping cart, and the form that's being submitted is being generated on the page before depending on how many items are in the cart. For example, if there's only 1 items then we only have the field name 'item_name_1' which should store a value like "Sticker" and 'item_price_1' which stores the price of that item. But if someone has 5 items, we would need 'item_name_2', 'item_name_3', etc. to get the values for each item up to the fifth one.
What would be the best way to loop through those items to get the values?
Here's what I have, which obviously isn't working.
extract($_POST);
$x = 1; // Assuming there's always one item we're on this page, we set the variable to get into the loop
while(${'item_name_' .$x} != '') {
echo ${'item_name' .$x};
$x++;
}
I'm still relatively new to this kind of usage, so I'm not entirely how the best way to deal with it.
Thanks.
First, please do not use extract(), it can be a security problem because it is easy to manipulate POST parameters
In addition, you don't have to use variable variable names (that sounds odd), instead:
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value'";
}
To ensure that you have only parameters beginning with 'item_name' you can check it like so:
$param_name = 'item_name';
if(substr($key, 0, strlen($param_name)) == $param_name) {
// do something
}
Use array-like fields:
<input name="name_for_the_items[]"/>
You can loop through the fields:
foreach($_POST['name_for_the_items'] as $item)
{
//do something with $item
}
If your post keys have to be parsed and the keys are sequences with data, you can try this:
Post data example: Storeitem|14=data14
foreach($_POST as $key => $value){
$key=Filterdata($key); $value=Filterdata($value);
echo($key."=".$value."<br>");
}
then you can use strpos to isolate the end of the key separating the number from the key.
i wouldn't do it this way
I'd use name arrays in the form elements
so i'd get the layout
$_POST['field'][0]['name'] = 'value';
$_POST['field'][0]['price'] = 'value';
$_POST['field'][1]['name'] = 'value';
$_POST['field'][1]['price'] = 'value';
then you could do an array slice to get the amount you need

Making POST values dynamic within a loop to store as an array?

I've been working on trying to write a function that will grab the POST values of any given form submission, pop them into an array, loop through the array using trim, addslashes etcetera pass that value back to a variable where it can then be passed to a database.
Now the hurdle I have atm is getting all the input,textarea,select element data into an array upon form submission. code I have follows
$fields = array($_POST['1'], $_POST['2']);
$i = 0;
foreach ($fields as $field) {
$i++;
${'field'.$i } = trim(addslashes(strip_tags($field)));
echo "POST field info #". $i ." - ". ${'field'.$i }."<br />";
}
As you can see everything is fine here baring that the POST value names are still being in-putted statically, what I need is a way to get that POST data fed into a loop which dynamically calls the POST name using an increment variable and then pop all that data into the same array. Code I have tried follows.
for ($ii=0;$ii++;) {
foreach($_POST['$ii'] as $field) {
$fields = array($field);
}
}
$i = 0;
foreach ($fields as $field) {
$i++;
${'field'.$i } = trim(addslashes(strip_tags($field)));
echo "POST field info #". $i ." - ". ${'field'.$i }."<br />";
}
Now I know this wont work but I can sense I am relatively close, so I am wondering if any clever person can help me sort the last part out? I sadly am now going to sleep and wont be viewing this post for at least 9 hours, apologies.
Thanks in advance.
Dan.
$arrayOfPostValues = $_POST; // it already is an array
$arrayOfPostValues = array_map('strip_tags', $arrayOfPostValues);
$arrayOfPostValues = array_map('trim', $arrayOfPostValues);
Or, if you really, really want to use a loop:
foreach ($arrayOfPostValues as &$value) {
$value = trim(striptags($value));
}
I'd absolutely advise against the use of addslashes, it serves very little purpose. Use mysql_real_escape_string or prepared statements instead.
I'd also advise against breaking the vales out of the array into separate variables, it can only cause problems. If you really want to do it, there's the extract function, which does exactly that. But, again, don't do it. Arrays are the perfect way to handle this kind of data.
You need to assign values to $_POST[1] and $_POST[2] to begin with, I've done this for you but normally they would be populated from a form I assume?
I'm not sure why you want to do this sort of thing: ${'field'.$key}, but I've left that part as is as I assume you must have a reason.
Anyway I've modified your code a bit, see below.
$_POST['1'] = '<h1>variable 1</h1>';
$_POST['2'] = '<h2>variable 2</h2>';
foreach($_POST as $key => $value){
${'field'.$key} = trim(addslashes(strip_tags($value)));
echo "POST field info #". $key ." = ". ${'field'.$key}."<br />";
}
The above code outputs:
POST field info #1 = variable 1
POST field info #2 = variable 2
On a side note, using field names such as '1' and '2' is not very good. Try using something more descriptive but as I said above I assume you have a reason for doing this.
UPDATE:
You can still get this to work for any form even if you are using specific names for the form elements. I have added a few lines below as an example for you.
$_POST['email'] = 'example#example.com';
$_POST['password'] = 'hgbks78db';
$_POST['name'] = '';
foreach($_POST as $key => $value){
if($value==''){
echo 'POST field "'.$key . '" is empty<br />';
/* I added the name of the field that is empty to an error array
so you have a way of storing all blank fields */
$a_error[] = $key;
}
else{
echo 'POST field "'.$key . '" is not empty<br />';
}
}

Categories