PHP form keeps giving me back same response - php

very short simple quiz app. browser keeps returning Correct as a response, even though it is not correct.
<?php
$answer = (isset($_POST['answer']));`
if ($answer == "Dagny Taggart")`
{
echo "Correct";
} else {
echo "wrong";
}
?>

Try this. You're currently setting $answer to isset($_POST['answer'], meaning as long as $_POST['answer'] is set, you're $answer is going to be TRUE. I would check if it's set and then set it to the $_POST value.
<?php
if(isset($_POST['answer'])) {
$answer = $_POST['answer'];
if ($answer == "Dagny Taggart")
{
echo "Correct";
} else {
echo "wrong";
}
} else {
// Do something?
}
?>

isset returns a boolean (true or false) value. So your test will always fail because true != 'Dagny Taggart'
Add this ternary and it will work
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';

Related

php If statement inside html table wrong output

i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table

How to compare value in bidimensional array Laravel

I receive the information with this form correctly.
$findNIF = User::where('nif','=',$nif)->get();
$findEmail = User::where('email','=',$email)->get();
I need to compare if it is the same value the "nif" and "email" of the $findNIF and $findEmail.
$findNIF["0"]["nif"];
$findNIF["0"]["email"];
$findEmail["0"]["nif"];
$findEmail["0"]["email"];
I use the following sentence but I always receive "OK"
if ($findNIF["0"]["nif"] && $findNIF["0"]["email"] && $findEmail["0"]["nif"] && $findEmail["0"]["email"]){
echo "OK";
} else {
echo "NO";
}
Here
if ($findNIF["0"]["nif"] && $findNIF["0"]["email"] && $findEmail["0"]["nif"] && $findEmail["0"]["email"]){
echo "OK";
} else {
echo "NO";
}
in your condition if you get values in both of your $findNIF and $findEmail variables then it always return true.
If you want to compare the nif of $findNIF & $findEmail and email of $findNIF & $findEmail both then this would be like this
if (($findNIF["0"]["nif"] == $findEmail["0"]["nif"]) && ($findNIF["0"]["email"] == $findEmail["0"]["email"])){
echo "OK";
} else {
echo "NO";
}
Please elaborate more about what you need.

Wrong output in php program

Following is my code here actually o/p should be hi..but it is giving no
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='xyz,ccc';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
output:hi
actual result should be 'no'.
Side note, this is bad code:
in_array(isset($weekendArr) && $weekendArr,$arr)
do it like
isset($weekendArr) && in_array($weekendArr,$arr)
and in_array is not strict so this
in_array(true,array('w','s'))
will be allways TRUE
do it with:
in_array(true,array('w','s'),true)
and you see.
And you can't check an array with an array the $needle be an STRING here.
The only solution is to do splitt your STRING into two values and then check two times for TRUE
$c='Sunday,Monday';
foreach(explode(',',$c) as $check){
if(in_array($c,$arr,true))
{
echo $check.' is in array';
}
else
{
echo $check.' is NOT in array';
}
}
Hope that helps a little.
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$day='Sunday'; //You cant test both days ! Just one value at a time
if(true === in_array($day, $listDays))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
Or option two if you want to test different days
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$dayToTest='Sunday, Monday'; //Here we have multiple days
$tabTest = preg_split(',', $day); //split into an array
//Then test for each string in tabTest
foreach($tabTest as $string)
{
if(true === in_array($string, $listDays))
{
echo $string.' is OK';
}
else
{
echo 'no';
}
}
?>
Change your code to:
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='Sunday,Monday';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}

$_GET['variable'] == 'text'

I want to compare if a get variable is a specific text. But I cannot get into the if-statemeant. The echo "invalid user" is not written.
code:
<?php
if ($_GET['status'] && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
when I check the variable status before (ando also after) the if-statemeant with:
echo $_GET['status'];
or with
print_r($_GET['status']);
the result is in both
login_fail
So the variable status is there and has "login_fail".
But "invalid user" is not written!
I tried it also with === or with ' instead " and much much more.
Use isset().
`
<?php
if (isset($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid useenter code herer";
}
`
Try this :
<?php
if (!empty($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
I think your code don't have any problem.
if your facing a problem till now you can add this line
before your if condition script.
$_GET['status'] = trim($_GET['status']);
In order to avoid errors when $_GET has nothing, use this:
if (!empty($_GET['status']) && ($_GET['status'] == "login_fail")) {
echo "invalid user";
}
Change to test if 'status' is set in the $_GET variable first and then check its value.
if (isset($_GET['status']) && $_GET['status'] == "login_fail")
{
echo "invalid user";
}
That way you will not run into any problems if it's not set.
You should use isset() function to check whether a variable has any value:
if(isset($_GET['status']) && $_GET['status'] == 'login_fail')
{
echo "invalid user";
}

how can i do first variable like second variable in php

my code is
<?php if($_SERVER['REQUEST_URI'] == "/scripts/script1.php")
{
echo 'yes';
}
else
{
echo 'no';
}
some time my URL come like
www.example.com/scripts/script1.php?var1=value1&var2=value2
How can do that with PHP
URL like function....
Make use of strpos in PHP
<?php
if(strpos($_SERVER['REQUEST_URI'],"/scripts/script1.php")!==false)
{
echo 'yes';
}
else
{
echo 'no';
}
A slight variation on Shankar's answer
$result = "no";
if( strpos( $_SERVER[ "REQUEST_URI" ], "/scripts/script1.php" ) !== false ) {
$result = "yes";
}
By setting the default value for the variable, you can make your code more concise and improve readability.

Categories