using VAR DUMP POST and how to get value into a variable - php

I have a form when I do var dump on post am getting an array like this
array(1) { [1338099133]=> string(9) "hardcover" }
However when I try to set a variable with the name of the radio button it is giving me an error of undefined variable, despite the fact that the name of the radio input matches the post value and that var dump is showing some values in post...
How do I remove those values in post to a variable please help
Here is my code that shows the radio in put
<input type="radio" name='.$arr[$row]['isbn'].' value="hardcover" >Hardcover:
and here is my post variable
var_dump($_POST);
$value = $_POST[$row]['isbn'];
am I maybe refering to it wrong...?

It appears the undefined variable is $row.
I'm assuming that you are looping through $arr, grabbing the row and outputting each line for your input element. When you go to grab the item from the $_POST array, $row isn't set and that's throwing the error.
The generated html would look like:
<input type="radio" name='1338099133' value="hardcover" >Hardcover:
which matches the key & value in your $_POST array on submit.
To fix this, you'll need to come up with a known name instead of a dynamic one. Something like the following should work:
<input type="radio" name="isbn['.$arr[$row]['isbn'].']" value="hardcover" >Hardcover:
Which then allows you to loop through all of your inputs on submission:
foreach ( $_POST['isbn'] as $isbn => $response ) {
// $isbn = 1338099133;
// $response = 'hardcover';
}

the array key is "1338099133" its value is "hardcover"
if $row in your example is "1338099133" then to set $value to "hardcover" its.
$value = $_POST[$row];

Related

Php form isset with multiple brackets

I have a check box with a name of:
<input type='checkbox' name ='schedule[".$row['id']."][1]' />
I want to check if the check box was checked with the PHP isset(...) And I tried it as
isset($_POST['schedule[".$row['id']."][1]]);
But this didn't work. Any ideas that how It can works?
Try it like:
isset($_POST['schedule'][$row['id']][1])
Simply treat it as multi-D array in this case and edit particular index of schedule key.
After the submission of form the GLOBAL VARIABLES $_POST passes the data as string or int instead of original variables.
To check if checkbox was checked try this one:
$id = $row['id'];
if(isset($_POST['schedule'][$id][1])) {
echo "hello world";
}

how NOT to store submit button value as post data php

ok i have dynamically created form elements with unique names and when validated i want to store all the form data into SESSION.
This is the code to do it:
if(isset($_POST["address_submit"])){
insertSessionVars();
header("Location: http://localhost/project%20gallery/notes.php");
exit;
}
function insertSessionVars(){
foreach($_POST as $fieldname => $fieldvalue){
$_SESSION['formAddresses'][$fieldname] = $fieldvalue;
}
}
This works almost fine but stores also submit button value. I output it like this:
foreach($_SESSION['formAddresses'] as $value){
echo $value;
}
Any help will be greatful :)
Don't assign a name attribute to your submit button. If you assign a name then it is passed as part of the $_POST array.
<input type="submit" value="My Button" />
Since you no longer have the submit button in post, instead of checking if the submit button is set check to see if the post array contains data using count().
if(count($_POST) > 0)
The $_POST values are just array so after the for each why don't you use unset to drop the submit key from the array
That way you can check the form has been submitted and the either choose to not set the submit key or drop it rather then not giving it a name at all
Either way you can in your foreach do something like this (see *) and pass "$except" as parameter.
Where $except has to be the name of your submit button in insertSessionVars();
if(!in_array($key, $except)){...}
And ofc above your foreach something like this :
if(!is_array($except)) $except=array($except);

Dynamic forms and PHP

Ive started working on a dynamic form script that allows a user to add form elements via Jquery, which is then in turn submitted to a PHP script.
I'm just after some feedback on ways to achieve this. At the moment I have the following:
When a user adds a form element the element is added with the following name array:
<textarea name="element[text][123]">
<input type="text" name="element[input][456]" />
As I need to know the type of form element that was submitted I am using a multidimensional array called 'element[][]' where the first level of the array is the type of element and the second element of the array is a unique ID and the value.
When I var_dump() This after submission PHP outputs:
array
text => array
123 => string 'The textarea value'
input => array
456 => string 'The input field value'
Im working on the PHP side of the script now and just wondering if there is a better way to do this.
Any thoughts?
UPDATE
I have to change the way that Im doing this as the array keynames are not unique.
If the user adds two textareas
<textarea name="element[text][123]">
<textarea name="element[text][456]">
When the user adds a form element, the element can be dragged so the positioning can be changed after the element was created. This allows a user to add an element but then move it to where they want it to appear.
PHP handles this ordering fine and accepts the array in the order that the form is submitted, however as mentioned above if the key names are the same then the order will be broken.
On the PHP side I need to know
the type of form field
the value of the form field
the unique ID, which is just a timestamp, of the form field
I think I might need to do what Cole mentioned, assigning the names as:
element[text_123]
I can then explode the keyname on '_' to determine the type and the identifier.
UPDATE
I took the script Jack posted and slightly modified it
$vars = $_POST['element'];
foreach ($vars as $id => $vals)
{
// $vars[id] outputs the ID number
// $vars[vals] is the array containing the type and value
echo "This fields ID is $id. ";
foreach($vals as $key => $value)
{
echo "Type was: $key and the value was: $value <br />";
}
}
A quick test of this outputted
This fields ID is 1338261825063. Type was: heading and the value was: xzczxczxczxczxczxc
This fields ID is 1338261822312. Type was: heading and the value was: asdasdasdasdad
From this I know the identifier and the array that it belongs to, the type and the value, but I also know the order that the data was submitted.
From that I can wrap my data in markup, perform any additional operations and then insert the data into the database.
Looks okay; you could also consider something like this (it introduces more fields though, so you must really think the benefit is worth it):
<input type="hidden" name="element[123][type]" value="text" />
<input type="hidden" name="element[456][type]" value="input" />
<textarea name="element[123][value]">
<input type="text" name="element[456][value]" />
Then you can do this:
foreach ($_POST['element'] as $name => $info) {
// $info['type'] is 'text' or 'input'
// $info['value'] is the user input
}

Get all variables sent with POST?

I need to insert all variables sent with post, they were checkboxes each representing a user.
If I use GET I get something like this:
?19=on&25=on&30=on
I need to insert the variables in the database.
How do I get all variables sent with POST? As an array or values separated with comas or something?
The variable $_POST is automatically populated.
Try var_dump($_POST); to see the contents.
You can access individual values like this: echo $_POST["name"];
This, of course, assumes your form is using the typical form encoding (i.e. enctype=”multipart/form-data”
If your post data is in another format (e.g. JSON or XML, you can do something like this:
$post = file_get_contents('php://input');
and $post will contain the raw data.
Assuming you're using the standard $_POST variable, you can test if a checkbox is checked like this:
if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
...
}
If you have an array of checkboxes (e.g.
<form action="myscript.php" method="post">
<input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
<input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
<input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
<input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
<input type="checkbox" name="myCheckbox[]" value="E" />val5
<input type="submit" name="Submit" value="Submit" />
</form>
Using [ ] in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox'] won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.
For instance, if I checked all the boxes, $_POST['myCheckbox'] would be an array consisting of: {A, B, C, D, E}. Here's an example of how to retrieve the array of values and display them:
$myboxes = $_POST['myCheckbox'];
if(empty($myboxes))
{
echo("You didn't select any boxes.");
}
else
{
$i = count($myboxes);
echo("You selected $i box(es): <br>");
for($j = 0; $j < $i; $j++)
{
echo $myboxes[$j] . "<br>";
}
}
you should be able to access them from $_POST variable:
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val<br />\n";
}
It is deprecated and not wished to access superglobals directly (since php 5.5 i think?)
Every modern IDE will tell you:
Do not Access Superglobals directly. Use some filter functions (e.g. filter_input)
For our solution, to get all request parameter, we have to use the method filter_input_array
To get all params from a input method use this:
$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...
Now you can use it in var_dump or your foreach-Loops
What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULL and that is correct.
If you need to get all Input params, comming over different methods, just merge them like in the following method:
function askForPostAndGetParams(){
return array_merge (
filter_input_array(INPUT_POST),
filter_input_array(INPUT_GET)
);
}
Edit: extended Version of this method (works also when one of the request methods are not set):
function askForRequestedArguments(){
$getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
$postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
$allRequests = array_merge($getArray, $postArray);
return $allRequests;
}
So, something like the $_POST array?
You can use http_build_query($_POST) to get them in a var=xxx&var2=yyy string again. Or just print_r($_POST) to see what's there.
Why not this, it's easy:
extract($_POST);
Using this u can get all post variable
print_r($_POST)

PHP: Parsing of a given form input variable

How would I go about parsing incoming form data where the name changes based on section of site like:
<input type="radio" name="Motorola section" value="Ask a question">
where 'Motorola section may be that, or Verizon section, or Blackberry section, etc.
I do not have any control over changing the existing forms unfortunately, so must find a way to work with what is there.
Basically, I need to be able to grab both the name="" data as well as its coresponding value="" data to be able to populate the email that gets sent properly.
Well, you don't receive a HTML form, but just field names and values in $_POST. So you have to look what to make out of that.
Get the known and fixed fields from $_POST and unset() those you've got [to simplify]. Then iterate over the rest. If " section" is the only constant, then watch out for that:
foreach ($_POST as $key=>$value) {
if (stristr($key, "section")) {
$section = $value;
$section_name = $key;
}
}
If there are multiple sections (you didn't say), then build an section=>value array instead.
<form action="formpage.php" method="post">
<input type="radio" name="Motorola_section" value="Ask a question">
</form>
$motorola = $_POST['Motorola_section'];
if ($motorola =='Ask a question')
{
form submit code if motorola is selected
}
Well, first off, you shouldn't have spaces in the name field (even though it should work with them).
Assuming it's a form, you can get the value through the $_POST (for the POST method) and $_GET (for the GET method) variables.
<?php
if ( isset( $_POST['Motorola section'] ) ) // checks if it's set
{
$motoSec = $_POST['Motorola section']; // grab the variable
echo $motoSec; // display it
}
?>
You can also check the variables using print_r( $_GET ) or print_r( $_POST ).

Categories