isset($_GET['variable']) not working? - php

This function is not working for me. I think it is isset($_GET['success']) that's not working but I'm really not sure. the problem is it doesn't print anything ever. And without if(isset($_GET['success'])) it only prints "username taken" Please help?
<?php
if(isset($_GET['success'])) {
$success=$_GET['success'];
if($success=='yes') {
echo "<center><font color='red'>Comment Posted!</font></center>";
}
else {
echo "<center><font color='red'>Username taken!</font></center>";
}
}
?>

What kind of output are you getting from this? Are you passing the GET method correctly? the URL should have page_name.php?success=yes in it. If you're not getting anything and you want success to be only set if it is true perhaps this would be better.
<?php
if(isset($_GET['success']) && $_GET['success']=='yes')
{
echo "<center><font color='red'>Comment Posted!</font></center>";
}
else
{
echo "<center><font color='red'>Username taken!</font></center>";
}
?>

Related

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

GET request variable error in PHP

I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.

If MySQL value empty echo, else echo?

I am trying to echo "No explanation entered." if the health_info value is empty in the MySQL database with the below code. However, no matter if the row is empty or not, it always echos "No explanation entered." What am I missing or doing wrong? Thanks!!
<?php
if (empty($health_info)) {
echo "No explanation entered.";
} else {
echo $health_info;
}
?>
<?php
if (empty($health_info)) {
echo "No explanation entered.";
} else {
echo $health_info;
}
?>
In your code you are evaluating $health_info that is undefined (if the code is what you posted).
Do a var_dump( $health_info ); and you will know what the value is.

trouble with the index PHP

for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'][$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
on the line where it says "NOT WORKING",
the index [$c] does not get the value from the loop.
but when i specify it and change it to $_POST['answer1'], it is working.
what is the correct syntax for this?
Try this.
for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'.$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
You're treating 'answer' as an array here, looking for an index within.
You want to concatenate the value.
if($row['answer']==$_POST["answer{$c}'])
Based on your note, it looks like you want:
$_POST["answer$c"]
Correct is:
if ($row['answer'] == $_POST['answer' . $c]) {
....
}

if else does not work

i have problem with if else statement in zend framework
<?php if(count($this->result) > 0) {
echo "not found";
}
else{
echo "advertise here";
}
?>
i would like to hide ads if there is no result, somehow, it does not work, please help
I'm not sure if I've understood the question correctly. But try
<?php
if(count($this->result) > 0) {
echo "advertise here";
}else{
// do nothing..effectively hiding.
}
?>
You can also get rid of the else part completely.
Your if else statement seems to be correct.
Did you try to check the content of your variable with a
var_dump($this->result)

Categories