php check if any of 30 form fields is empty [duplicate] - php

This question already has answers here:
Check if multiple strings are empty [duplicate]
(7 answers)
Closed 5 years ago.
I have more than 30 fields in a form, and can't believe that I must write an array to check if any of them is empty (likehere)
I need something like js
$_POST[].each function{
if this is empty...
}
and hope that 'php' as so glorified language has something like this or similar.

Try this:
foreach($_POST as $post)
{
if( !empty($post) )
{
// your code here
}
}

Related

Try if there is any $_GET value [duplicate]

This question already has answers here:
How to check if $_GET is empty?
(8 answers)
Closed 4 years ago.
There is a way to determine if there is any $_GET value (not with a specific key) passed in a GET request ? I'm not sure the syntax
isset($_GET)
is valide
$_GET is array so use count instead of isset.
<?php
if(count($_GET) >0){
//valid
}
?>

What's the best way of checking if an object property in php is undefined? [duplicate]

This question already has answers here:
Check if a variable is undefined in PHP
(8 answers)
Closed 4 years ago.
What's the best way of checking if an object property in php is undefined?
You can use is_null
Or
!isset($object)
Example :
I want to check if the input is undefined. So, I can show errors.
if (!isset($_POST['myInput'])) { echo "error"; } else { // do the code }

PHP - I'm a little confused on $_POST and in_array [duplicate]

This question already has answers here:
Check if $_POST exists
(15 answers)
Closed 6 years ago.
Alright so i receive from $_POST and i need my code do something like this
if ($_POST contains EU-London) {
//do stuff here
}else{
//failed to find EU-London
}
Now i've been told several ways to find a certain phrase in code but the posted data contains:
Array
(
[EU-London] =>
)
How would i check if the EU-London is there? because pregmatch uses strings and im not sure how to grab this using in_array()
You seem to be looking for a key in $_POST:
if (isset($_POST['EU-London'])) {
// Key exists.
}
As correctly commented by Robert, the proper way to check for an existing key would be
if (array_key_exists('EU-London', $_POST)) {
// Key exists.
}
You can check if isset key
if (isset($_POST['EU-London'])) {
//key isset
}

Why my $_POST is empty? [duplicate]

This question already has answers here:
What is the difference between POST and GET? [duplicate]
(7 answers)
Closed 7 years ago.
I have no time and too tired to struggle with this, so I decided to ask here: I've created the file my.php which contains only:
<?php var_dump( $_POST ); ?>
And then I open the file using browser like this:
www.domain.com/my.php?post1=hey&post2=ho&post3=letsgo
And in the browser I have array(0) { } as a response.
Question: What could I possibly done wrong??
Thanks!
In URL are GET parameters, not POST.
echo $_GET['post1']; // hey
echo $_GET['post2']; // ho
echo $_GET['post3']; // letsgo
You cant pass POST variables through URL.
u r using GET method..

PHP adds \ to JSON [duplicate]

This question already has answers here:
"slash before every quote" problem [duplicate]
(6 answers)
Closed 9 years ago.
My shared web hosting adds \ to JSON. I use ExtJS and it normally sends this data
[{"property":"id","direction":"ASC"}]
Howere PHP receives or chages it as [{\"property\":\"id\",\"direction\":\"ASC\"}]
Thus I cannot use json_decode($_REQUEST['sort'])
I think this is because they wanted to prevent SQL injection but now they break my application. What I have to do?
Edit:
$sort = json_decode($_GET['sort']);
print_r($_GET); // [sort] => [{\"property\":\"id\",\"direction\":\"ASC\"}]
print_r($sort); //
Please check, if you vHost has magic quotes enabled.
Someone over here proposed this to get the unchanged values:
if (get_magic_quotes_gpc()) {
function strip_array($var) {
return ( is_array($var)
? array_map("strip_array", $var)
: stripslashes($var)
);
}
$_POST = strip_array($_POST);
$_SESSION = strip_array($_SESSION);
$_GET = strip_array($_GET);
}

Categories