I am having a problem with getting my PHP script to correctly read and execute my "IF.....ELSEIF" conditions.
In my first file, I have the following code :
if(isset($_POST['submit']) {
$selected_radio = $_POST['selection'];
$_SESSION['my_selection'] = $_POST['selection'];
if (($selected_radio == '25') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
elseif (($selected_radio == '50') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
}
That was the easy part.
If either "radio button" is selected, I have a Javascript function which opens a "new (child) window"
That's also easy.
But, then, comes the hard part : within that new window, the user has to select from another choice of radio buttons :
if(isset($_POST['submit']) {
$selected_radio = $_POST['my_response'];
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
}
Basically, this means : if the user selects "yes" in the current (child) window, then the window closes, and the parent window re-directs to "25.php" or "50.php"..........depending on the value of the $_SESSION['my_selection'] --- which was selected earlier in the parent-window
But, for some reason, it's not working. My code is executing only the FIRST IF-condition :
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
It is completely ignoring the second one.............even if the user had earlier selected "50" in the parent-window.
My first thought was : the SESSION value of the radio-button ---- $_SESSION['my_selection'] --- was not being carried-over into the new (child) window.
But, I used "echo" to verify that this was working properly. The value was indeed being carried-over into the new (child) window.
However, after the child-window closes, and the parent-window is re-directed, I used "echo" again to track any errors........and it showed that : the value of $_SESSION['my_selection'] is always equal to "25" !
In a nutshell : why is the second IF-statement being ignored??
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25')
^ ^
A single equals sign is an assignment and as the value you are assigning is truthy the if will always evaluate to true. Use == for a comparison.
You are using = instead of == in nearly all statements:
if (($_POST['my_response'] = 'yes')
should be
if (($_POST['my_response'] == 'yes')
This way, you don't check if $_POST['my_response'] is equal to "yes", but if it is possible to assign "yes" to $_POST['my_response']. As a result, all your if statements are true.
Related
When a user clicks on "List View" link then I want to show them the "List View" HTML and when they click on "Grid View" I want to show "Grid View" HTML.
I've defined the following links to click-
List View <br>
Grid View
Then I defined the following condition with PHP get method to show user the desired output-
<?php
if( isset( $_GET['view'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view'] ) == 'grid' ){
echo "This is Grid view";
}
This condition is not working. If I change "view" to "view_1" and "view_2" from URL then my condition is working as well.
<?php
if( isset( $_GET['view_1'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view_2'] ) == 'grid' ){
echo "This is Grid view";
}
?>
<br>
List View <br>
Grid View
But I don't want to change the "view" key. I just want to keep the same key and different value for both URL to do the conditional statement.
Is it possible?
isset() only checks if a variable exists and isn't null and returns a boolean (true/false).
To check the value, you first need to check if it exists (isset) and then check the value, like this::
if (isset($_GET['view']) && $_GET['view'] == 'list') {
// your code here.
}
You can read more here: http://php.net/manual/en/function.isset.php
Alternative
If you want to make your code and conditions slightly more readable, you could to this:
// Get the value of $_GET['view'] once, if it exists (pre PHP 7)
$view = isset($_GET['view']) ? $_GET['view'] : null;
// PHP 7.x (new shorter syntax for the above)
$view = $_GET['view'] ?? null;
if ($view == 'list') {
// your code
}
Did you try like this..
if(isset($_GET['view']))
{
if($_GET['view'] == 'list')
{
//code here
}
elseif ($_GET['view'] == 'grid')
{
//code here
}
}
I have a simple form that I'm trying to add a checkbox to, I have everything on the form setup correctly but when I try to handle the check box I'm only able to make echos work. I'm trying set whether the box is checked as a yes or no and store that yes/no in a variable, here is what I have in my handle form for the checkbox:
if(isset($_POST['race']) &&
$_POST['race'] == 'Yes')
{
$race1 == "yes";
}
else
{
$race1 == "No";
}
You need to use the single equal sign when assigning values. Double equals does a comparison.
if(isset($_POST['race']) && $_POST['race'] == 'Yes')
{
$race1 = "yes";
}
else
{
$race1 = "No";
}
== is a comparison operator. You need to use attribution operator =
if (isset($_POST['race']) &&
strtolower($_POST['race']) == 'yes')
{
$race1 = 'yes';
}
else
{
$race1 = 'No';
}
I have simple code which is something like this:
$options = new Options();
$page = new Pages();
if($page->page_limit() <= $options->pageno) {
$page->userid = $user_details->userid;
$page->date_of_pub = $_POST['date_of_pub'];
$resultss=$page->page_create();
}
else {
$resultss=false;
}
Then at bottom I am putting a condition
if(isset($resultss) && isset($resultss) == true) {
echo $alert->SuccessMsg("Page created successfully!");
}
if(isset($resultss) && isset($resultss) == false) {
echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");
}
Instead of printing error value even I have set the value of $result = false is shows success message, means its showing $resultss = true statement.
Suggest something. This is so strange. I got the answer thank you so much :)
One more thing.
can you please tell me how can I get rid of this " echo $alert->ErrorMsg" this is so annoying for all the class and functions. I want to make it a single word.
You check the same twice:
isset($resultss) && isset($resultss)==true
You should do:
isset($ressults) && $ressults == true
You have a problem in your logic with isset(). This:
if(isset($resultss) && isset($resultss)==true){echo $alert->SuccessMsg("Page created successfully!");}
if(isset($resultss) && isset($resultss)==false){echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");}
Should be
if(isset($resultss) && $resultss == true){echo $alert->SuccessMsg("Page created successfully!");}
if(isset($resultss) && $resultss ===false){echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");}
In your existing code, the second isset() in each statement is incorrect. In the first one, it is redundant, and you are asking the same thing as if(isset($resultss) && isset($resultss)), which is always true. In the second one, isset($resultss) && isset($resultss)==false could never be true. It's like true && false.
You really don't need to check if the variables are set, since you are setting it in both branches of the if/else. Just do:
if ($resultss) {
echo $alert->SuccessMsg("Page created successfully!");
} else {
echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");
}
Change your IF conditions to:
if(isset($resultss) && $resultss == true)
isset(x) and isset(x) == true bacially mean the same. What you want is to match two conditions:
the variable IS SET and IT IS EQUAL TO TRUE.
Since I'm not sure what the value might be I'd suggest using these confitions:
if(isset($resultss) && $resultss !== false) //the value is set and it is NOT set to false
AND
if(isset($resultss) && $resultss === false) //the value is set to false
Have you tried setting it to false as default?
$options = new Options();
$page = new Pages();
$resultss = false;
if($page->page_limit() <= $options->pageno){
$page->userid = $user_details->userid;
$page->date_of_pub = $_POST['date_of_pub'];
$resultss = $page->page_create();
}
I want to show an error (in phpBB it's trigger_error) right after the user clicks submit.
I already have a function which checks if a link has been posted. Now I want to call it after the submit post button has been clicked.
Where do I find that? Probably in posting.php?
Open posting.php.
find around line 631
if ($submit || $preview || $refresh)
{
after add
$post_data['your_url'] = "http://www.damienkeitel.com/index.php"; //remove the equals and url value if using in real post
$your_url = $post_data['your_url'];
$your_url_exists = (isset($your_url)) ? true : false;
$your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);
if ($your_url_exists && http_file_exists($your_url) == true)
{
trigger_error('yeah, its reachable');
}
else if ($your_url_exists && http_file_exists($your_url) == false)
{
trigger_error('what da hell..');
}
open functions_posting
find around line 19
/**
* Fill smiley templates (or just the variables) with smilies, either in a window or inline
*/
after add
function http_file_exists($url)
{
$f = #fopen($url,"r");
if($f)
{
fclose($f);
return true;
}
return false;
}
i'm trying to make an image appearance and disappearance based on 3 condition,
condition A = when user is logged in and it's username fits the displayname(by using the GET function) then it should echo "yes"
condition B = When user is logged in and it's username does not fits the displayname then it should echo "no"
condition C = when user is not logged in then it should echo "no" too
(i swapped the image with yes and no for easier referencing)
By logging in, the user has a cookie which is set like below
setcookie("user", $user, $expire);
setcookie("loggedin", 1, $expire);
First i get the cookie which i set when user logins.
$user1 = $_COOKIE["user"];
$loggedin = $_COOKIE['loggedin'];
$user = strtoupper($user1);
then i get my player's name
$playername = $_GET['player'];
Now i do the conditions
$uplayername = strtoupper($playername);
function showplusicon(){
global $uplayername;
if(($loggedin = "1") and ($user == $uplayername)){
echo "yes";
}
else if (($loggedin = "1") and ($user != $uplayername)){
echo "no";
}
else{
echo "no";
}
}
I don't see what's the problem but it keeps being registered as condition B.
Single equal signs assign, not compare.
if(($loggedin == "1") and ($user == $uplayername)){
...
And since you really only have two output states, you shouldn't need 3 conditions; remove condition B.
The variable $loggedin isn't known inside your function showplusicon(). You will need to add it as a global along with global $uplayername.
function showplusicon(){
global $loggedin, $uplayername;
// etc
}
Since this was accepted but not totally complete, I'll just add that as others indicated, the == equality operator needs to be used instead of the = assignment operator.
if(($loggedin == "1")
^^^^
$loggedin = "1"
Surely this should be:
$loggedin == "1"
Otherwise I would echo $user and $uplayername to see if these differ.
First thing's first:
$loggedin = "1" is a bad idea, as you're actually giving $loggedin the value "1" instead of comparing. Use == or even === if you're sure about the datatype.
Further on, the $loggedin isn't available in the scope of showplusicon(), as you haven't declared it as a global like you did with $uplayername.
Fix the listed issues above and it should be working a bit better.
If you've got problems to understand your own code's logic, a simple way is to assign the conditions to self speaking variables to get used to it:
$userIsLoggedIn = $loggedin == "1";
$userIsPlayer = $user == $uplayername;
The variables make it easy to debug your code at the very beginning
var_dump($userIsLoggedIn, $userIsPlayer);
so to locate the actual errors:
The variable $loggedin is undefined
The if clauses are setting a value (=), not comparing it (== or ===).
You can then use additionally a more readable code-flow to make your decision more visible:
if ($userIsLoggedIn)
{ // user is logged in
if ($userIsPlayer)
{ // user is player
...
}
else
{ // user is not player
...
}
}
else
{ // user is not logged in
...
}
Depending of what you want to output, this can be simplified even:
if ($userIsLoggedIn && $userIsPlayer)
{
echo 'yes';
} else
{
echo 'no';
}
Hope this is helpful for you.
Your main problem is todo with global scope of your variables:
<?php
//Get cookie info
$cookie['user'] = $_COOKIE["user"];
$cookie['loggedin'] = (isset($_COOKIE['loggedin'])&&$_COOKIE['loggedin']=='1')?TRUE:FALSE;
//Set user array
$user['user'] = strtoupper($cookie['user']);
$user['loggedin'] = $cookie['loggedin'];
$user['player'] = $_GET['player'];
$user['uplayername']=strtoupper($user['player']);
function showplusicon(){
//Made $user array available within function
global $user;
if($user['loggedin'] === TRUE && $user['user'] == $user['uplayername']){
echo "yes";
}else{
echo "no";
}
}
?>