How to Access the Form's 'Name' Variable from PHP

How to access the form's 'name' variable from PHP

To identify the submitted form, you can use:

  • A hidden input field.
  • The name or value of the submit button.

The name of the form is not sent to the server as part of the POST data.

You can use code as follows:

<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>

PHP: How to get name value in form name=form1 /

Is there a way to get the value of the name attribute in the form tag? I'm using PHP and don't see it in $_POST.

No, the form's name attribute is never set to sent to the server as part of the POST data.

The easiest way around this would be adding a hidden form element <input type="hidden"> containing the name.

<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>

how to get a html form element with name is php variable

try this,
use $_POST array in foreach:

action.php

foreach ($_POST as $key => $value)
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

i hope it will be helpful.

how to get a input field's name via PHP GET

To get all input field name using post method.

you can use $GET or $REQUEST instead of $POST.

$REQUEST use with both GET and POST.
In php $POST is a one type of associative array.

<?php
$temp= array();
if (isset($_POST['submit']) && !empty($_POST['submit'])) {
foreach ($_POST as $key => $val) {
//$key is the name you wanted, and $val is the value of that input
$temp[] = $key;
}
}
?>

PHP Get input name

Solution using single loop (Update):

If you're just using question/answer single time you can do it in single loop like this,

<?php

foreach($_GET as $key => $value){
$question = $key;
$answer = $value;

// Save question and answer accordingly.
}

If you will be using question answer to perform other things, use the following method.


You can get all the keys using array_keys(), where $_GET is an array.

Use it like this,

<?php
$keys=array_keys($_GET);
print_r($keys); // this will print all the keys
foreach($keys as $key) {
// access each key here with $key
}

Update:

You can make a pair of question,answer array and put it in the main array to insert it in the database like this,

<?php
$mainArray=array();
$keys=array_keys($_GET);
foreach($keys as $key) {
// access each key here with $key
$questionAnswerArray=array();
$questionAnswerArray["question"]=$key;
$questionAnswerArray["answer"]=$_GET[$key];
$mainArray[]=$questionAnswerArray;
}

// Now traverse this array to insert the data in database.
foreach($mainArray as $questionanswer) {
echo $questionanswer["question"]; //prints the question
echo $questionanswer["answer"]; // prints the answer.
}

How to access variables created dynamically inside functions and methods in PHP?

I finally found a smart, clear way to accomplish this! The solution uses extract built-in function.

Although I do not need it anymore because I'm using Laravel for validation, I still want to share the answer so it can help others.

SOLUTION

Class Form {
#code
...
public function getFieldsCompacted() {
$compactedFields = [];
foreach(array_keys($this->fields) as $key) {
$compactedFiels["$key"] = $this->fields["$key"];
}
return $compactedFields;
}
}

Then, on the code:

require_once '/path/to/Form.php';

$inputs = ['olympiad', 'test_type', 'year', 'level', 'test', 'answer_sheet'];
$Form = new Form;
$Form->addFields($inputs);
extract($Form->getFieldsCompacted());

This way I can do:

$olympiad->required(true)->type('select')->inValues($olyimpiadsArray)->label('Olimpíada')->errorMessage('some error message here');
$test_type->required(true)->type('select')->inValues($testTypeArray)->errorMessage('bla bla');
$level->required(true)->type('select')->inValues(['Nacional', 'Regional'])->label('Nível')->errorMessage('sample error message');
$year->required(true)->type('int')->range(1998, 2019)->label('Ano')->errorMessage('another error message');
$test->type('file')->label('Prova')->allowedExtensions(['pdf'])->errorMessage('bla bla');
$answersheet->type('file')->label('Gabarito')->allowedExtensions(['pdf'])->errorMessage('bla bla bla');

Instead of doing:

$Form->fields['olympiad']->required(true)->type('select')->inValues($olyimpiadsArray)->label('Olimpíada')->errorMessage('some error message here');
$Form->fields['test_type']->required(true)->type('select')->inValues($testTypeArray)->errorMessage('bla bla');
$Form->fields['level']->required(true)->type('select')->inValues(['National', 'State'])->label('Nível')->errorMessage('sample error message');
$Form->fields['year']->required(true)->type('int')->range(1998, 2019)->label('Ano')->errorMessage('another error message');
$Form->fields['test']->type('file')->label('Prova')->allowedExtensions(['pdf'])->errorMessage('bla bla');
$Form->fields['answersheet']->type('file')->label('Gabarito')->allowedExtensions(['pdf'])->errorMessage('bla bla bla');

Edit:

RiggsFolly pointed out the insecurity of using extract. I totally agree that using extract in $_POST and $_GET. However, this is not the case here because the variables to extract are defined as you can see in
$inputs = ['olympiad', 'test_type', 'year', 'level', 'test', 'answer_sheet'];
. So, the extract will only extract these variables and it won't override other variables. Therefore, no risk in the code above.

Access parameter name with brackets in $_POST in PHP

I found that using bracket on input name, give us a Two-dimensional array (2D array), in other words, we can access <input type="text" name="fields[name]"> with this command on PHP back-end:

echo $_POST['fields']['name'];  //access value of name="fields[name]" 
echo $_POST['fields']['email']; //access value of name="fields[email]"

html form name= php variable

I assume you want to get all questions and display them on the one page and then submit all answers to answer.php? In that case you could:

$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND() LIMIT 5";
$result1=mysql_query($sql1);

echo "<form method='post' action='answer.php'>";

while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];

echo "<P><strong>$q_nr $question</strong><BR>";
echo "<input type='radio' name='question[$q_nr]' value='$option1'>$option1<BR>";
echo "<input type='radio' name='question[$q_nr]' value='$option2'>$option2<BR>";
echo "<BR>";
echo "<BR>";
echo "</p>";
}
echo "<input type='submit' value='Send Form'>";
echo "</form>";

And on answer.php:

//Key is $q_nr and $answer is selected $option
foreach($_POST['question'] as $key => $answer) {
echo $key;
}


Related Topics



Leave a reply



Submit