Exclude form fields from POST request - php

I have a simple foreach statement grabbing all the fields from a submitted form. The fields are then put into a table that is sent via email. There are 3 fields that I need submitted and validated, but not sent with the email. Here is my script generating the table:
foreach($_POST as $name => $value) {
if($name !== "num1" || $name !== "num2" || $name !== "captcha") {
$text .= "<tr><td>$name</td><td>";
//if form field is array
if(is_array($value)) {
foreach($value as $symptom) {
$text .= $symptom . ", ";
}
//else no array
} else {
$text .= $value;
}
$text .= "</td></tr>";
}
}
Basically all form field names are set as $name and all values are set as $value. I am trying to include all fields except the ones named num1, num2, & captcha.
I am not redoing the way this form is handled, I just need to exclude these 3 fields from the table but my conditional statement isn't doing what I had expected. How can I ensure these three fields won't be included in the email?

change if($name !== "num1" || $name !== "num2" || $name !== "captcha")
to if($name !== "num1" && $name !== "num2" && $name !== "captcha")

Change the || in your if() to &&. Your if() as written will ALWAYS succeed, because whatever value you're testing will always "not be" any of those values at the same time.

PHP's continue will skip an interation of a loop, then you change to using == comparison operators... Try this:
if($name == "num1" || $name == "num2" || $name == "captcha")
continue;
else {
// rest of your code
}

You should use && instead of || in if statement.

Related

How to exclude a parameter within an if statement?

I'm trying to create somewhat of a fuzzy search with PHP. Consider the following pseudo code/scenario below.
Take these $_POSTed values:
name
price
location
age
And I have an array of items (lets assume cars), of which each item contains the following properties:
name
price
location
age
So in my function, I use an if() within a foreach() to check if any of the posted data matches with an item and if it does, echo the item.
function search() {
$items = array(...);
$name = $_POST['name'];
$price = $_POST['price'];
$location = $_POST['location'];
$age = $_POST['age'];
foreach($items as $item) {
if(
$item['name'] == $name &&
$item['price'] == $price &&
$item['location'] == $location &&
$item['age'] == $age
){
echo json_encode($item);
}
}
}
The issue is that not every posted value is filled in, so for example $name could be empty i.e. $name = ''.
Question: How can I exclude any of the items in the if() statement if the initial $_posted key is empty without creating an if() inception type scenario? I considered using || but I'm pretty sure that wouldn't be the same as excluding a comparison all together. A
You can achieve that by taking advantage of PHP's lazy interpreter:
if(
(!$name || ($name && $item['name'] == $name)) &&
(!$price || ($price && $item['price'] == $price)) &&
(!$location || ($location && $item['location'] == $location)) &&
(!$age || ($age && $item['age'] == $age))
){
echo json_encode($item);
}
This will first check that each variable is not null, nor empty. (How exactly does if($variable) work?)
Also look at Does PHP have short-circuit evaluation?

I'm trying to check a form's input to make sure the fields aren't blank or contain specific words. Anyone know why this code isn't working?

This is the "contactprocess.php" file that the form posts to:
$result = "";
foreach ($_POST as $key => $val) {
if(($val != "") || (strpos($val,'http') == false) || (strpos($val,'seo') == false)){
$result = "clear" ;
}
}
if ($result == '') {
header("location: contact.php");
}
The code ignores the "header("location: contact.php");" line and continues on with the rest of the script. How else can this be written?
In your if-branch where you attempt to redirect, you need exit; to prevent the script from continuing execution:
if ($result == '')
{
header("location: contact.php");
exit;
}

How do I check required fields using 'empty' in a field whose value is an integer?

I'm trying to run a check of my required fields where i use the empty function. Unfortunately empty considers 0 to be empty and my values could be 0 and one in the radio button. I've tried to use !is_int(myvalue) and yet it didn't work.
Here is my code:
<?php $required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname) {
if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_int($_POST[$fieldname]))) {
$errors[] = $fieldname;
}
}
?>
It appears you want to test a string to see if it is an integer.
Try using is_numeric
http://www.php.net/manual/en/function.is-numeric.php
if(!isset($_POST[$fieldname]) // not in the post
|| (empty($_POST[$fieldname]) && 0 != $_POST[$fieldname]) // empty but not zero
|| (int) $_POST[$fieldname] == '')) // not an integer
) {
$errors[] = $fieldname;
}

Dynamic if-statement with variables?

I'm trying to create a dynamic if-statement. The reason I want to do this, is because I need to check server-sided whether inputfields match my regex and are not empty. However, some of my inputfields can be removed in my CMS, meaning there would be more/less inputfields accordingly.
Ideally I would add variables in my if-statement but I'm not 100% sure if that's allowed, so perhaps I would need an other way to solve this problem. Here's what I tried:
if ($f_naw['streetname'] == 1)
{
$streetname= $_POST['streetname']; //Used in INSERT query
$cstreetname = " || $_POST['streetname'] == ''"; //Used to check if field is empty
$pstreetname = " || !preg_match($streetnameReg,$_POST['streetname'])"; //Used to check if it matches my regex
}
else
{
//These variables define variables if inputfields are not shown
$streetname= ''; //No streetname means it's excluded in INSERT query
$cstreetname = ''; //Not needed in check
$pstreetname = ''; //Also not needed in check
}
// more of these if/else statements
if ($_POST['firstname'] == '' || $_POST['lastname'] == '' || $_POST['email'] == '' $cstreetname $cpostalcode $chometown $ctelnr $csex $cdateofbirth)
{
echo 'One of the fields is empty.';
header('refresh:3;url=index.php');
}
else
{
//Regex check, after that more code
}
My idea was to check if a specific field is shown on the front-end and in that case I'm creating some variables that I want to paste in my if-statements.
I'm getting an error saying Server error meaning my php-code would be invalid.
Is it even possible at all to make a dynamic if-statement? If yes, at what part am I failing?
Help is much appreciated! Thanks in advance.
First of all, since it looks like you need to combine all of the conditionals with ||, you can correct your program by writing it like this:
if ($f_naw['streetname'] == 1)
{
$streetname= $_POST['streetname']; //Used in INSERT query
$cstreetname = $_POST['streetname'] == ''; //Used to check if field is empty
$pstreetname = !preg_match($streetnameReg,$_POST['streetname']); //Used to check if it matches my regex
}
else
{
//These variables define variables if inputfields are not shown
$streetname= ''; //No streetname means it's excluded in INSERT query
$cstreetname = false; //Not needed in check
$pstreetname = false; //Also not needed in check
}
if ($_POST['firstname'] == '' || $_POST['lastname'] == '' || $_POST['email'] == '' || $cstreetname || $cpostalcode || $chometown || $ctelnr || $csex || $cdateofbirth)
{
echo 'One of the fields is empty.';
header('refresh:3;url=index.php');
}
This would work, but it's unwieldy. A much better solution would be to use an array (let's name it $errors that gets dynamically populated with errors resulting from validating your fields. Like this:
$errors = array();
if ($f_naw['streetname'] == 1)
{
$streetname= $_POST['streetname']; //Used in INSERT query
if ($streetname == '') {
$errors[] = 'Streetname cannot be empty.'; // message is optional
}
if (!preg_match($streetnameReg,$streetname)) {
$errors[] = 'Streetname is invalid.'; // message is optional
}
}
And then:
if ($errors) {
echo 'There are errors with the data you submitted.';
header('refresh:3;url=index.php');
}
If you provided human-readable error messages you can also arrange for them to be displayed so that the user knows what they need to fix. And of course there are lots of variations of this technique you can use -- e.g. group the error messages by field so that you only show one error for each field.
If you want to check for empty $_POST fields you can do something like this
$error = False;
foreach($_POST as $k => $v)
{
if(empty($v))
{
$error .= "Field " . $k . " is empty\n";
}
}
if(!$error)
{
echo "We don't have any errrors, proceed with code";
}
else
{
echo "Ops we have empty fields.\n";
echo $error;
}
And after you are sure that all the fields are not empty you can do other stuff.

Check if both strings are empty not working

I have a conditional if check on a webform on which ajax (and php) is doing the submit work.
if(empty($name) or empty($message))
{
do something
}
The above is working if one of the 2 strings is empty.I want to add to the above
or (empty($name) and empty($message))
for checking if both strings are empty but in someway its not working!I want to make sure all 3 senarios are covered,Name empty or Message empty or both empty.
Any ideas?
This will check that both $name and $message is not blank, than true
if($name != '' && $message != '') {
//Do something
}
If you use this, this will check if any 1 field is empty than the condition is true
if($name == '' || $message == '') {
//Do something
}
If you use this, this will be true if both are empty
if($name == '' && $message == '') {
//Do something
}
Replace "or" with "||". Example:
if (empty($name) || empty($message))
{
// Do something
}
or means "If either condition is true", not "If exactly one condition is true".
The code you have already covers your requirement.

Categories