PHP - Loop over form fields and display blank ones as well - php

I've noticed that this code in PHP 5.5 only displays the fields that have a value. It entirely skips the blank ones and won't display their name or value:
$msg = '';
foreach($_POST as $key => $value) {
$msg .= $key . ': ';
if (is_array($value)) {
foreach($value as $arr_value){
$msg .= $arr_value . "; ";
}
} else {
$msg .= $value;
}
$msg .= "\n";
}
echo $msg;
Is there a way to loop and also see the names of the fields that had no values entered when the form was submitted? Thanks.
Update: The form has this enctype. Could this make a difference? enctype="multipart/form-data"

Related

Data in SQL table is being read as HTML

I have a function that fetches one or multiple rows from a database and in php, goes through a series of foreach loops to make a table body from it, like so:
function buildTableRowsByPCS($connection, $numbers) {
$body = NULL;
foreach($numbers as $number) {
$rows = getRowFromPCS($connection, $number);
$body .= '<tr>';
foreach($rows as $row) {
if(is_array($row)) {
$body .= '<td>';
foreach($row as $r) {
$body .= $r.'<br>';
}
$body .= '</td>';
} else {
$body .= '<td>'.$row.'</td>';
}
}
$body .= '</tr>';
}
return $body;
}
It is working fine, but, sometimes one of the columns has a cell of data that seems to be disturbing the html, whether it be a "/" or "<" or ">" etc. Which results in me getting a mismatch error in my datatable. Is there any way whether in php or sql to treat the data gotten as purely text and treat the characters in it as nothing to do with HTML etc?
You can apply htmlspecialchars() or htmlentities() to the value before you output it, that will convert all applicable characters to HTML entities, for instance > will become >.

PHP Json values with a specified label

I am using json_decode and echoing the values using a nested foreach loop.
Here's a truncated json that I am working on:
[{"product_name":"Product 1","product_quantity":"1","product_price":"2.99"},....
and the loop
foreach($list_array as $p){
foreach($p as $key=>$value) {
$result_html .= $key.": ".$value."<br />";
}
}
This was I am able to echo all key/value pairs.
I have tried using this to echo individual items something like:
foreach($list_array as $p){
foreach($p as $key=>$value) {
echo "Product: ".$p[$key]['product_name'];
echo "Quantity: ".$p[$key]['product_quantity'];
}
}
However I am unable to because it doesn't echo anything.
I would like to be able to show something like:
Product Name: Apple
Quantity: 7
Currently it is showing:
product_name: Apple
product_quantity: 7
How can I remove the key and replace it with a predefined label.
It can be done with:
foreach ($list_array as $p){
$result_html .= 'Product: ' . $p->product_name
. 'Quantity: ' . $p->product_quantity . '<br />';
}
If you are decoding your json into an object you can do it like that.
$list_array = json_decode('[{"product_name":"Product 1","product_quantity":"1","product_price":"2.99"}]');
$result_html = '';
foreach($list_array as $p){
$result_html .= '<div>Product: '.$p->product_name.'</div>';
$result_html .= '<div>Quantity: '.$p->product_quantity.'</div>';
}
echo $result_html;

eliminate a specified post data on a post request array

I use this code to retrieve all the post request (refer below)
<?php
foreach ($_POST as $key => $value) {
$body .= $key . ": " . $value . '<br>';
}
echo $body;
?>
and there is a post data named "adminemail" and "cat", now what i want is to eliminate those two and print all the post data except those two. How to do that? any suggestions, recommendations and ideas, would love to hear. Thank you in advance.
option 1
unset($_POST['adminemail'],$_POST['cat']);
option 2
<?php
foreach ($_POST as $key => $value) {
if(!in_array($key,array('adminemail','cat'))){
$body .= $key . ": " . $value . '<br>';
}
}
echo $body;
?>
The following should work:
<?php
$arr = array_diff_key($_POST, array("adminemail" => 0, "cat" => 0));
$body = ""; // You must have this line, or PHP will throw an "Undefined variable" notice
foreach($arr as $key => $value){
$body .= $key . ": " . $value . "<br/>";
}
echo $body;
?>

Strange with function to list countries from txt

i have a problem and i can't explain it ,,
first this is my function
function list_countries($id,$name=null,$result=null){
$countries = 'countries.txt';
$selected = '';
echo '<select name="'.$name.'" id="'.$id.'">';
echo '<option disabled>طالب الغد</option>';
if(file_exists($countries)){
if(is_readable($countries)){
$files = file_get_contents($countries);
$files = explode('|',$files);
foreach($files AS $file){
$value = sql_safe($file);
if(strlen($value) < 6){
echo '<option disabled>'.$value.'</option>';
}else{
if($value == $result){
$selected = ' selected="selected" ';
}
echo '<option value="'.$value.'".$selected.'>'.$value.'</option>';
}
}
}else{
echo 'The file is nor readable !';
}
}else{
echo "The file is not exist !";
}
echo '</select>';
}
Now the explain
i have a text file includes a countries names separated with "|"
In this file there is a heading before the countries ,, i mean Like this
U|United Kingdom|United State|UAE etc ..
L|Liberia|Libya etc ..
Now what the function Do is Disabled the Heading , and it's always one character ..
but the strlen function the minimum number that it's give to me is 5 not one .. " This is the first problem
The second one in the $result never equaled the $value and ether i don't know why ??
You need to split twice the file, one for the lines, one for the countries.
Also, since your "country header" is always the first item of each row, you do not need to check using strlen. Just shift out the first item of each row set: that one is the header, the following ones are the countries.
Something like this.
Note that in your code there is a syntax error in the echo that outputs the value, the > symbol is actually outside the quotes.
function list_countries($id,$name=null,$result=null){
$countries = 'countries.txt';
$selected = '';
$text = '<select name="'.$name.'" id="'.$id.'">';
$text .= '<option disabled>ﻁﺎﻠﺑ ﺎﻠﻏﺩ</option>';
if(file_exists($countries)){
if(is_readable($countries)){
$list = file($countries);
foreach($list as $item){
$item = trim($item);
$opts = explode('|', $item);
// The first item is the header.
$text .= "<option disabled>$opts[0]</option>";
array_shift($opts);
foreach($opts as $opt)
{
$value = sql_safe($opt);
$text .= '<option';
if($value == $result)
$text .= ' selected="selected"';
$text .= ' value="'.$value.'"';
$text .= '>'.$value."</option>\n";
}
}
}else{
$text .= "The file is not readable!";
}
}else{
$text .= "The file does not exist!";
}
$text .= '</select>';
return $text;
}
I have slightly modified your code so that the function actually returns the text to be output instead of echoing it; this makes for more reusability. To make the above function behave as yours did, just replace the return with
echo $text;
}
and you're good.

PHP post checkboxes help

I"m having trouble receiving a list of items that are checked in a field of checkboxes which are part of a form.
I have an HTML table with a few checkboxes:
HTML
<input type="checkbox" name="carry[]" value="1" />
<input type="checkbox" name="carry[]" value="2" />
<input type="checkbox" name="carry[]" value="3" />
PHP - this is what I'm using to post the form to an email address
foreach($_POST as $key => $val) {
$body .= $key . " : " . $val . "\r\n";
I get the value in my email as: "carry: Array" -- not the actual values that are selected. How do I handle an array of checkboxes selected in a form and post it?
Ideally, I would want: "carry: 1; 2; 3" (without the quotes)
You can check if the value is an array and handle it differently:
foreach($_POST as $key => $val) {
if (is_array($val)) {
$body .= $key . " : " . implode(",",$val) . "\r\n";
} else {
$body .= $key . " : " . $val . "\r\n";
}
}
If you want the string '1; 2; 3', you should join together the items in the array:
$carry= implode('; ', $_POST['carry']);
However doing so will naturally cause ambiguous results if any of the items in the array themselves have a semicolon in.
To iterate over the post array allowing any of its members to be arrays:
foreach($_POST as $key=>$val) {
if (is_array($val))
$val= implode('; ', $val);
$body.= "$key: $val\r\n";
}
Or, if you don't need so much control over the exact formatting and a debugging dump is fine (but you need to see more than just the useless string 'Array':
$body= var_export($_POST, TRUE);
It's printing carry: Array because that's exactly what it is. You need to loop over it (another loop inside the first) to access the values inside:
foreach($_POST as $key => $val) {
if($key == 'carry') {
foreach($val as $carry) {
$body .= $carry;
}
}
else {
$body .= $key . " : " . $val . "\r\n";
}
}
That's completely untested but hopefully the logic is sound :)

Categories