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.
Related
i have made some random input names, because its a drag and drop page builder, so i can't guess, how much elements user will use, so i have created a random input names,
for that am using php foreach loop for $_POST requests. i have tried to make it encoded into json and then later save it into database. but it looks like something is wrong in my json.
Here are my html demo codes :
<input style="display:none;" name="DATA-BLOCK-A(some random string)">
<input style="display:none;" name="DATA-BLOCK-B(some random string)">
PS: A is for selecting element A, and B is for element B.
here is my PHP code :
if (isset($_POST)) {
//$arr = array();
foreach($_POST as $key => $value)
{
$arr = array($key => $value);
$encode = json_encode($arr);
echo $encode;
}
}
and Here is the result :
{"sortlist":"block[]=D5e3385b75a75d&block[]=K5e3385b85a75e&block[]=C5e3385b95a75f&block[]=F5e3385ba5a760"}{"save_cont_flag":"0"}{"DATA-block-D5e3385b75a75d":"0#TRANSP
<\/p>"}{"DATA-block-K5e3385b85a75e":"0#TRANSP20"}{"DATA-block-C5e3385b95a75f":"01#TRANSP0images\/250place.jpg\u00b8"}{"text-1573532276681":""}{"textarea-1573532278320":""}{"DATA-block-F5e3385ba5a760":"121212unundefined"}{"page_name":"123"}{"aff_link":""}{"pause_link":""}{"seo_title":""}{"fbook":""}{"seo_desc":""}{"seo_keywords":""}{"back_color":"#EEEEEE"}{"body_color":"#FFFFFF"}{"back_image":""}{"ty_font_color":"#000000"}{"ty_override":""}{"ty_name":"12314"}{"ty_stm":""}{"modal_para_width":"0"}{"catcha_url":""}{"catcha_un":"Yes"}{"catcha_message":""}{"code_head":""}{"code_body":""}{"modal_share_width":"0"}{"modal_cta_width":"0"}{"modal_video_width":"0"}{"modal_mp_width":"0"}{"modal_stm_width":"0"}{"modal_image_width":"0"}{"modal_bonus_width":"1"}{"ty_headline":""}{"modal_spacer_width":"0"}{"att_bar_status":"0"}{"att_delay_in":"0"}{"att_bar_color":"#bbbbbb"}{"att_gradient":"0"}{"att_text_color":"#000000"}{"att_text_font":"Open Sans:400"}{"att_text_size":"14"}{"att_bar_message":"Add Your Attention Bar Text Here"}{"att_link_color":"#000000"}{"att_link_label":"Add Link Text Here"}{"att_link_url":"http:\/\/commissiongorilla.com"}{"count_font":"Open Sans:800"}{"count_size":"55"}{"count_status":"0"}{"count_type":"0"}{"count_end":"01\/31\/2020 6:41 AM"}{"count_zone":"0.0"}{"count_eg_days":"0"}{"count_eg_hours":"0"}{"count_eg_mins":"0"}{"count_digit_color":"#bbbbbb"}{"count_label_color":"#bbbbbb"}{"count_background":"0"}{"count_language":"1"}{"count_exp":"0"}{"count_url":"http:\/\/commissiongorilla.com"}{"count_add_days":"0"}{"count_add_hours":"0"}{"count_add_mins":"0"}{"modal_countdown_width":"0"}{"modal_review_width":"0"}
and also how seperate all A BLOCKS and B BLOCKS?
Thanks.!
You don't need to use a loop.
Just used json_encode :
$json = json_encode($_POST);
If you need to get key contain DATA-block-, you can write :
foreach ($_POST as $key => $value) {
if (strpos($key, 'DATA-block-') !== false) {
// Here `DATA-block-{}`
}
}
If you change the input names to DATA-BLOCK-A[] and DATA-BLOCK-B[], $_POST['DATA-BLOCK-A'] will contain an array of all a blocks and $_POST['DATA-BLOCK-B'] will contain an array of all b blocks.
This also eliminates the need for generating random strings.
I have 2 pages in php. The 1st page includes a form which transimts data to the 2nd page. The form uses method=post. Data transmitted successfully in the 2nd page. I have the following code, which gets data and printing them using the code:
php?
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $value;
}
?>
All I want is to extract data from array and place them into variables, because I want to use these varaibles later in some if startments and mysql queries. Any idea how can i do this?
First, these really are basic PHP skills (or programming skills for that matter). Try to follow some tutorials or courses before attempting to write code in the "real world".
As long as you know the key for the value you want to store, this is how you do it:
$yourVariableName = $yourArray['yourKey']; // or just a number if the key is an int
You don't need for loops to do this.
EDIT
$kentroName = $_POST['kentro_name'];
$kentroSurName = $POST['kentro_surname'];
// And then the following six.
<?php
var_dump($_POST);
$array = array();
foreach ($_POST as $key => $value) {
echo $value;
$array[$key] = $value;
}
print_r($array);
?>
All,
I have a form that has some text inputs, checkboxes, select box and a textarea. I want to submit the form with PHP using a submit button to a page for processing.
That part is fine but what I would like to do is basically get the results into an array so I can do a foreach loop to process the results.
Basically I'm trying to create a dynamic form the submits to my backend processing script and don't want to hard code in the post values like the following:
$var1 = $_POST['var1'];
echo $var1;
$var2 = $_POST['var2'];
echo $var2;
Does anyone know how to go about doing something like this or give any recommendations?
If there're no other data in your POST but these generated elements, just do
foreach( $_POST as $key => $val ) {
// do your job
}
and process what you have. If you want to mix your generated entries with predefined you may want to put these generated in nested array:
<input ... name="generated[fieldname]" />
and then you iterate
foreach( $_POST['generated'] as $key => $val ) {
// do your job
}
Just use array notation:
<input name="vars[]" value="" />
Then you will have something like this as $_POST:
Array ('vars' => Array(
0 => 'val1'
1 => 'val2'
)
)
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val";
}
Actually, the $_POST variable is an array. you just need to extract the array values by using a simple foreach loop. that's it.
I hope this example help you.
foreach($_POST as $field_name => $val)
{
$asig = "\$".$field_name."='".addslashes($val)."';";
eval($asig);
}
After running this script all the POST values will be put in a variable with the same name than the field's name.
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.
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.