PHP checking $_POST - php

I have some form fields that when a form is submitted creates an array within the $_POST, I needing to check the this array has atleast 4 keys, how can I check that? I have no idea

try:
<?php
if(count($_POST) >= 4):
//Do your stuff
else:
//Do your error stuff
endif;
If you want to check an array within $_POST as apose to $_POST itself use
count($_POST['name_of_key_to_array_you_want_to_count'])

First, to make your work easier, you should change input name into array version. Something like this should work:
<input type='text' name='data[]' value='' />
Then, PHP will do it's magic and all you have to do is:
echo count($_POST['data']);
This is because your data[] form field is changed into array.

Use array_keys and count:
echo count(array_keys($_POST));
Or simply:
echo count($_POST);
because keys are same in number as items.

The count() function returns the length of an array.

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";
}

Creating and Storing an array / Dynamic array

I have issues with creating an array and storing the info, I have a table with data that could be infinite in its number, a user will then select some options which will determine which of these values they can select (which again is an infinite number), these choices are then presented into a checkbox where i use this code
<?php foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details = $detailsline->details_line;
echo $invoice_details;
echo '<input type="checkbox" name="invoice_details" value="'.$invoice_details.'"/>';
}
?>
So this should search through the options they previous choose, and sorts them into an array and then into checkboxs, but when i store the information is just saves the last box checked, I cant change the value of each input EG
echo '<input type="checkbox" name="invoice_details[value1]"
echo '<input type="checkbox" name="invoice_details[value2]"
Because I don't know how many values/checkboxes there will be.
I have also tried this
<?php foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details[] = $detailsline->details_line;
echo $invoice_details[];
echo '<input type="checkbox" name="invoice_details[]" value="'.$invoice_details.'"/>';
}
?>
Changing the
$invoice_details
to
$invoice_details[]
but this will just store a value "Array" in my database and not the actual values.
Please can anyone help me?
Ian
Okey I think You should Try this:
<?php
$i=0;
foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details = $detailsline->details_line;
echo $invoice_details;
echo '<input type="checkbox" name="invoice_details[$i]" value="'.$invoice_details.'"/>';
$i++;
}
?>
Try this
<?php foreach ( $results['detailsline'] as $detailsline )
{
$invoice_details = $detailsline->details_line;
echo $invoice_details;
echo '<input type="checkbox" name="invoice_details[]" value="'.$invoice_details.'"/>';
}
?>
Use <input type="checkbox" name="invoice_details[]">
In your code you can use serialize($invoice_details) for saving into DB and when using - unserialize($field_from_db).
First function returns string, so you can save it as string field, second function get the string and returns the whole array, so you can work with it.
but this will just store a value "Array" in my database and not the actual values.
think you tried to store Array as string so got that value
So may be needed to use implode/serialize/json_encode (Array) to store data?
Literally saying code $invoice_details[] = $detailsline->details_line; means take property details_line of an object $detailsline and insert its value as new element in array $invoice_details. Are you sure it is what you want?) Also I suppose that $detailsline is an array, but not the object (you trying to operate it as an object)

php form result loop

I'm trying to make a BASIC roulette script.
Is there anyway to get the submitted results of a form using PHP? In fact I know theres a way, but i can't find out how to do it.
So say if my form had several fields I want the result to loop through and show me which fields were filled and the numbers in each.
UPDATE: And say the form has about 40 fields, would I have to name each one in the loop? Any easier way?
$_GET or $_POST depending on the form method.
if(isset($_REQUEST['formInputName'])){
echo $_REQUEST['formInputName'];
}
$_REQUEST looks for GET, POST, and COOKIE.
You can also use $_GET to get a variable from the url (asdf.php?var=2).
If your form looks like this:
<form method="post" action="result.php">
<input type="text" name="foo">
</form>
In result.php you can use the global variable $_POST and loop through it if you want:
foreach($_POST as $name => $value) {
echo $name . ' = ' . $value;
}
If your form has 40 fields, you still need to name them all, but you can automate the process of naming and retrieving them with a loop. For example, if you wanted to create a sum with the value of all the fields, you could name them number1, number2, etc and do:
$sum = 0;
for($i = 1; $i <= 40; $i++)
$sum += $_POST['number' . $i];
You need to define the name of each field using name="something" in the input element, and than in the PHP you're getting it using $_POST['something'] in case you sent the form as method="post" or $_GET['something'] in case of get method
You can see what's sent using var_dump() or print_r(), just write something like that:
echo '<pre>';
print_r($_POST);
Or you can go all over the array using foreach statement:
<?php
foreach($_POST AS $key=>$val)
{
echo $key.': '.$val."<br />\n";
}
?>

is there a way to pass two values in an input checkbox to the $_POST

I have a bunch of checkboxes on the page and i want to pass two values for each checkbox like this....
<input name="class[]" value="first_value" data="second_value" type="checkbox" class="auto"/>
any idea how to get first_value and second_value past to the $_POST in php
any suggestions on how to do this
You can do this
<input name="class[]" value="CIS 2910C DL:3" type="checkbox" class="auto"/>
where you separate the two values by a : or whatever other separator you want.
Then on the $_POST you can use explode in a loop like this
$pieces = explode(":", $class);
echo $pieces[0]; // CIS 2910C DL
echo $pieces[1]; // 3
so you can pull out both values
You can't do it directly. I'd loop through with jquery and create new hidden inputs with those values and delete the data attr.
How about:
<input name="class[second_value][]" value="first_value" type="checkbox" class="auto"/>
Then in PHP
foreach($_POST['class'] as $first_value=>$tmpArray) {
foreach($tmpArray as $second_value) {
echo $first_value.": ".$second_value;
}
}
Odd way of doing it, but its seems like an odd situation you are in anyways.
I think you can only do this if you use ajax, or if you add a hidden field with an index sequential '[]' buying the checkbox with the value of X in X location hidden

Check if any variables are passed in a GET

I've done a few searches and not come up with anything, I'm sure it's obvious.
Basically I'm trying to work out if anything has been passed via GET from a form.
I know how to check for individual elements, but I just want to do a quick check if anything at all is passed
Cheers
Be careful when using count($_GET). If you submit the form with empty values it will still create keys for the fields, and your count() will be greater than 0 and empty($_GET) will be false.
<?php
print_r($_GET);
?>
<form action="" method="get">
<input type="text" name="name">
<textarea name="mytext"></textarea>
<input type="submit">
</form>
Make sure the fields are actually not empty:
function ne($v) {
return $v != '';
}
echo count($_GET); // prints 2
echo count(array_filter($_GET, 'ne')); // prints 0
This should do the job:
if (!empty($_GET))
{
}
if ( count($_GET) > 0 ) echo("I hear you!");
if(empty($_GET)) { /* no parameters passed*/}
just check the length of the $_GET array via count($_GET).
if none ha passed it should be 0
Simply 'just': if($_GET){ /* parameters passed*/} (for the current request) works to check if any query-string was passed in the GET or POST request.
This is because an empty array is false in a boolean if($x) context.
See: http://php.net/manual/en/types.comparisons.php
So indeed no need for count() or empty().
Actually, I think it is better to check using isset which checks if it is defined and if it is different than null
if(isset($_GET['value_you_looking_for']) {//code logic here}
Check for documentation

Categories