Jump to content

Why setted cookie variable needed twice click to hyperlink?


eaglehopes
Go to solution Solved by ginerjm,

Recommended Posts

I used below code in my website :

<!DOCTYPE html>

<?php

	//$page="home";
	error_reporting(E_ALL);
	ini_set('display_errors', '1');
	session_start();
	$_SESSION['page']   = 'home';
	$_SESSION['subblog'] = 'CP';
	$_SESSION['finalPage']  = '';
	$_SESSION['time'] = time();
?>
<html>
	
<head>  
<link rel="stylesheet" type="text/css" href="./css/styles.css"/>

  <table title="" class="headPart" >
    <tbody  >
      <tr >
        <td class="headImage" >
        	<b class="headupperpart">MY WEB SITE</b> 
		</td> 
      </tr>
    </tbody>
  </table>

</head>

<body class="background" >

  <table class="bodyLayout">
    <tbody>
      <tr  >
		  <!--  MENU ROW -->
        <td class="MenuTag" >
	
			<a class="button" href="./index.php?page=home" target="_self">Home</a>
			<a class="button" href="./index.php?page=about" target="_self">About</a>
			<a class="button" href="./index.php?page=blog"  target="_self">Blog</a>
        </td>

      </tr> <!-- end  of the MENU row -->
      <?php 
			if(isset($_GET["page"]) ) {
					setcookie("page",$_GET["page"]);
				}
			if (isset($_GET["subblog"]) ) {
					setcookie("subblog",$_GET["subblog"] );
				}
			if( isset($_GET["fin"] ) )  {
					setcookie("finalPage",$_GET["fin"]);
				}

			if ( strcmp($_COOKIE['page'],"blog")==0 ) {	 // create new menu 
				if( isset($_COOKIE['subblog']) ) {   // if subblog is selected
					// create subblog menu and put it in new row

					echo '<tr><td class="MenuTag">';
					loadSecondLevelMenu() ;
					echo "</td></tr>";

					echo '<tr><td class="MenuTag">';
					loadThirdLevelMenu($_COOKIE['subblog']) ;
					echo "</td></tr>";
					
				} else {    // subblog is empty
					echo '<tr><td class="MenuTag">';
					loadSecondLevelMenu();
					echo "</td></tr>";
				}
			} 
			
			function loadThirdLevelMenu($id) {
					if( (include './pages/blog/subCat.php') == TRUE ) {
						// do nothing since it loads page inside above "if statement" automatically
					} else {
						echo "categories page Load error...";
					}
			} 
			
			function loadSecondLevelMenu() {
					if( (include './pages/blog/categories.php') == TRUE ) {
						// do nothing since it loads page inside above "if statement" automatically
					} else {
						echo "categories page Load error...";
					}
			}
		?>
            
    </tbody>
  </table>
 </body>
</html>

I tried to use $_COOKIE[] array to pass the menu link data between all pages in the server.  I changed cookies with hyperlinks so I sent new variables to page with GET method. It works but not as I expected ! I need to click twice for buttons to set the cookies(i.e that are setted in $_GET[<cookiename>]). Why ? How can I convert it to : "once I clicked the button it changes getted variable and set it to coookie variable" ? Any help or guide is appreciated...

 

Edited : I deleted unused javascript from code...

Edited by eaglehopes
I deleted unused javascript from code...
Link to comment
Share on other sites

  • Solution

Not sure what "setted" means but when you do set a cookie in your current script, it is not available until you refresh the page you are on.  Cookies are loaded when the server sends a page to the client.   Does that help?

Link to comment
Share on other sites

2 hours ago, ginerjm said:

Not sure what "setted" means but when you do set a cookie in your current script, it is not available until you refresh the page you are on.  Cookies are loaded when the server sends a page to the client.   Does that help?

Thanks ginerjm it helped much.  After using setcookie I need to refresh page, I understood that. So, after your answer, I could not any method of PHP to refresh page after setting cookie for instance other than header("Refresh:<time>"). Howevere it is not the solution I am looking for I think, it is continously refresh the main page ... 

I think I should return to the use GET method to do what I try to do : change a page .. :)

Link to comment
Share on other sites

3 hours ago, ginerjm said:

Maybe if you just tell us what you want to do we could tell you how to do it.  Forget what you have done.

Thanks ginerjm. You offered gentle help. After your answer, I looked for new solution. First of all I noticed that I am completely wrong  in my code since I mixed $_SESSION variables with $_COOKIE variables ... any way as you sad, my previous code is wrong and I forgot it, but write from the start.

I wanted to get and embed "ready php or html pages" into main page's related tags(i.e divs etc) after clicking hyperlinks. Like using a javascript and "onclick" event of button, but with php script.

Finally, I did it by using $_SESSION variables. So, my result code was in index.php file :

<!DOCTYPE html>

<?php
	error_reporting(E_ALL);
	ini_set('display_errors', '1');
	
	session_start();
	$_SESSION['page']   = '';
	$_SESSION['subblog'] = '';
	$_SESSION['finalPage']  = '';
	$_SESSION['time'] = time();
?>
<html>
	
<head>  
<link rel="stylesheet" type="text/css" href="./css/styles.css"/>

  <table title="" class="headPart" >
    <tbody  >
      <tr >
        <td class="headImage" >
        	<b class="headupperpart">MY WEB SITE</b> 
		</td> 
      </tr>
    </tbody>
  </table>

</head>

<body class="background" >

  <table class="bodyLayout">
    <tbody>
      <tr  >
		  <!--  MENU ROW -->
        <td class="MenuTag" >
	
			<a class="button" href="./index.php?page=home" target="_self">Home</a>
			<a class="button" href="./index.php?page=about" target="_self">About</a>
			<a class="button" href="./index.php?page=blog"  target="_self">Blog</a>
        </td>

      </tr> <!-- end  of the MENU row -->
      <?php 
			if(isset($_GET["page"]) ) {
				$_SESSION["page"]=$_GET["page"];
			}
			if (isset($_GET["subblog"]) ) {
				$_SESSION["subblog"]=$_GET["subblog"] ;
			}
			if( isset($_GET["fin"] ) )  {
				$_SESSION["finalPage"]=$_GET["fin"];
			}
			
			// if user request blogs- then show blog menu here

			if ( strcmp($_SESSION["page"],"blog")==0 ) {	 // create new menu 
			//if ( strcmp($_COOKIE['page'],"blog")==0 ) {	 // create new menu 
				if( isset($_SESSION["subblog"]) ) {   // if subblog is selected
					// create subblog menu and put it in new row
					// while the blog menu bar remains open, get the selected subblogId (short id) & read "sub blog's sub menu file:subcats file"
					// create new final menu - three depth menu system
					echo '<tr><td class="MenuTag">';
					loadSecondLevelMenu() ;
					echo "</td></tr>";

					echo '<tr><td class="MenuTag">';
					// loadThirdLevelMenu($subblog) ;
					loadThirdLevelMenu($_SESSION["subblog"]) ;
					echo "</td></tr>";
					
				} else {    // subblog is empty
					echo '<tr><td class="MenuTag">';
					loadSecondLevelMenu();
					echo "</td></tr>";
				}
			} 
			
			function loadThirdLevelMenu($id) {
				if( (include './pages/blog/subCat.php') == TRUE ) {
					// do nothing since it loads page inside above "if statement" automatically
				} else {
					echo "subCat page Load error...";
				}
			} 
			
			function loadSecondLevelMenu() {
					if( (include './pages/blog/categories.php') == TRUE ) {
						// do nothing since it loads page inside above "if statement" automatically
					} else {
						echo "categories page Load error...";
					}
			}
		?>
            
    </tbody>
  </table>
 </body>
</html>

and in subCat.php (abbreviation of subCategory) 

<?php 

	$subCat=$_SESSION['subblog'];
	// do something with sended variable changed into local variable of $subCat
	
?>

And I have managed to work with this combination.  Thanks again for your kindness. If you do not mind, I do not waste nobody's time about my problem since I have managed to solve it. In the future, I want to reserve "help token" you offered me, and spend it for other questions I will ask.  :) if you accept of course !

Link to comment
Share on other sites

What you wrote means absolutely nothing to me.  Now that may be my ignorance.  What I tried to ask you for was a plain English description of the task you have made for yourself.  Not in terms of programming but in a way that describes the problem you are trying to fix using a computer.  All the stuff above just goes around and around something that I have no picture of.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.