Redirect button in php [duplicate] - php

This question already has answers here:
What does "href" stand for? [duplicate]
(5 answers)
Closed 7 years ago.
Hello guys I want to redirect a user to another page when clicking on a button on my webpage, but I dont know how to do it. My buttons are "home", "coffee", "shop" and "about us". When clicking on any of these, I want to get to another page. Can anyone help please?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="Styles/stylesheet.css" />
</head>
<body>
<div id="wrapper">
<div id="banner">
</div>
<nav id="navigation">
<ul id="nav">
<li>Home</li>
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>
</ul>
</nav>
<div id="content_area">
<?php echo $content; ?>
</div>
<div id="sidebar">
</div>
<footer>
<p>All rights reserved</p>
</footer>
</div>
</body>
</html>
and this is my index code:
<?php
$title = "Coffee Webpage";
$content = '<img src="https://encrypted-tbn2.gstatic.com/images? q=tbn:ANd9GcSmRPsVdzF4BYRWbF3yBpbkW-1ZjyURYWX1JQm0e6RAbJPR0H76" class="imgLeft" />
<h3>Title 1</h3>
<p>
</p>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdJv4yTpue170rTnOn9unKMgYWeOSPxBP9LoBwvUvhcDJNFcPG6w" class="imgRight"/>
<h3>Title 2</h3>
<p>
</p>
<img src="http://seattle.eat24hours.com/files/cuisines/v4/coffee.jpg?e24v=225" class="imgLeft" />
<h3>Title 3</h3>
<p>
</p>';
include 'Template.php';
?>

add the url to that page in to your link so for example if the page coffes is coffe.php then put coffe.php in there.
alternately put your full url which I would recommend.
you could also add this before your link to get the site url using php
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

Typically, you'd want to do this by adding a front controller; or, in your case, turning your index.php into a front controller.
<?php
$page = $_GET['page'];
switch($page) {
case 'coffee':
// set variables
break;
case 'another':
// set variables to something else
break;
}
require 'template.php';

Put a link to each page in their corrosponding a href attributes.
Example:
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>

Related

when i click button to other pages it shows my landing page with no css

So when i open my code on localhost/mframe the top of the project it looks like this
landing page
from here if i click the home button which should show the exact same page it goes to the url http://localhost/mframe/pages/index and shows the headers and nav for the landing page but it isnt using the css and looks like this
home page
then if i click the auction button it goes to the url http://localhost/mframe/pages/auction which shows the same page as above the landing page without css when it should just show the nav as thats been called in as required
finally my about page if i click that it opens a blank white page when it should open the page with the nav top right using the css but its just blank like this
about page
my code is set up with mvc php my code is below
config.php class
<?php
//Database params
define('DB_HOST', 'localhost'); //Add your db host
define('DB_USER', 'root'); // Add your DB root
define('DB_PASS', ''); //Add your DB pass
define('DB_NAME', 'auctionsystem'); //Add your DB Name
//APPROOT
define('APPROOT', dirname(dirname(__FILE__)));
//URLROOT (Dynamic links)
define('URLROOT', 'http://localhost/mframe');
//site name
define('SITENAME', 'Auction System');
my head.php
<html lang="en">
<head>
<meta charset="UTF-8">
<meta htttp-equiv="Cache-control" content="no-cache">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo SITENAME; ?></title>
<link rel="stylesheet" href="css/style.css"/>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Lato:wght#300;400;700&display=swap" rel="stylesheet">
</head>
<body>
</body>
</html>
my navigation.php
<nav class="top-nav">
<ul>
<li>
Home
</li>
<li>
About
</li>
<li>
Auction Items
</li>
<li class="btn-login">
Login/Signup
</li>
</ul>
</nav>
auction/about page code identical yet different when i open them up
<?php
require APPROOT . '/views/includes/head.php';
?>
<div class="navbar">
<?php
require APPROOT . '/views/includes/navigation.php';
?>
</div>
and index.php code
<?php
require APPROOT . '/views/includes/head.php';
?>
<div id="section-landing">
<?php
require APPROOT . '/views/includes/navigation.php';
?>
<div class="wrapper-landing">
<div>
<h1>One Man crappy software</h1>
<h2>is another mans job</h2>
</div>
</div>
</div>
login.php code
<?php
require APPROOT . '/views/includes/head.php';
?>
<div class="navbar">
<?php
require APPROOT . '/views/includes/navigation.php';
?>
</div>
<div class="container-login">
<div class="wrapper-login">
<h2>Sign in</h2>
<form action="<?php echo URLROOT; ?>/users/login" method="POST">
<input type="text" placeholder="Username *" name="username">
<span class="invalidFeedback">
<?php echo $data['usernameError']; ?>
</span>
<input type="password" placeholder="Password *" name="password">
<span class="invalidFeedback">
<?php echo $data['passwordError']; ?>
</span>
<button id="submit" type="submit" value="submit">
Submit
</button>
<p class="options">Not registered yet? Create an account! </p>
</form>
</div>
</div>
so i need to know why i have no css on any of the other pages why the about and auction page load up different despite having the same code and also why when i click the home button it doesnt show the same landing page
There are 2 types of HTML Redirect Links.
Absolute and Relative.
Absolute is:
Link
Relative link is:
Link
You are using absolute link without https:// prefix. So it expect a relative link and adding absolute link to your domain link.
You can add http:// or https:// prefix to fix it.

Rendering another layout inside index.php

I am following this Link to understand the basic framework of PHP Web app but I am not able to understand how can I switch back and forth to the different layouts (Articles, Portfolio) through navigation bar?
All different layout must be like index.php.
Image you have articles.php and portfolio.php. There content would be like:
Articles.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your article content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
and portfolio.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your portfolio content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
But everytime you add a new page, you must update your header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple Site</title>
</head>
<body>
<div id="header">
<h1>Simple Site</h1>
<ul class="nav global">
<li>Home</li>
<li>Articles</li>
<li>Portfolio</li>
<li>Any other page</li>
</ul>
</div>
This is what actually I wanted to achieve
<nav>
<ul>
<li>Home</li>
<li>Projects</li>
<li>Calculator</li>
<li>Blog</li>
<li>About</li>
</ul>
</nav>
I am sending the page variable to the index and then it is rendering the layout accordingly through this way.
if (isset($_GET["page"])){
$page=$_GET["page"];
}
else {
$page = "home";
}
renderLayoutWithContentFile($page .".php", $variables);

PHP header conflict [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 5 years ago.
Hello i make a project and i have the fallowing problem.
In my index page i have a dynamic layout:
<html>
<head>
<link rel="stylesheet" type="text/css" href="index2.css">
</head>
<body class="news">
<header>
<div class="nav">
<ul>
<li class="home">Home</li>
<li class="logare"><a class="active" a href="index2.php?page=admin_login">Admin</a></li>
<li class="registration">Registration</li>
<li class="produse">Produse</li>
</ul>
</div>
</header>
<div id="content">
<?php
$p=$_GET['page'];
switch($p){
case "welcome":
include('welcome.php');
break;
case "admin_login":
include('Admin_login.php');
break;
case "registration":
include('Registration.php');
break;
case "produse":
include('produse.php');
break;
default:
include('welcome.php');
break;
}?>
</div>
</body>
</html>
in default and welcome case it;s redirecting me to my welcome.php page:
<?php
session_start();
if(!$_SESSION['email'])
{
header("Location: login.php");//redirect to login page to secure the welcome page without login access.
}
?>
<html>
<head>
<title>
Registration
</title>
</head>
<body>
<h1>Welcome</h1><br>
<?php
echo $_SESSION['email'];
?>
<h1>Logout here </h1>
</body>
</html>
Because i have a redirecting in welcome.php too ,my header is not loading from the dynamic layout.How can i repair this,but still redirecting me to login?
To avoid PHP header conflicts, I prefer to use JS redirection instead. See the below code that I have modified for you.
<?php
session_start();
if(!$_SESSION['email']) {
?>
<script language="javascript" type="text/javascript">
alert("User not logged in.");
window.location = 'login.php';
</script>
<?php
}
?>
Hope this helps.

HTML Templates -- php?

So, I'm making a site about WWI as a school assignment, and I want this to appear in every document:
<!DOCTYPE html>
<html>
<head>
<title>1914</title>
<script src="modernizr-1.5.js"></script>
<link href="styles.css" type="text/css" rel="stylesheet" />
<meta charset="utf-8" />
</head>
<body>
<div id="container">
<header>
<img src="images/banner.png" alt="World War I" style="border: none"/>
<nav>
<ul>
<li><span>Home</span></li>
<li><span>1914</span></li>
<li><span>1915</span></li>
<li><span>1916</span></li>
<li><span>1917</span></li>
<li><span>1918</span></li>
</ul>
</nav>
</header>
<section>
<article>
<br style="clear: both" />
</article>
<aside>
</aside>
</section>
<footer style="font-weight: bold; letter-spacing: .1em">
Citations •
About
</footer>
</div>
</body>
</html>
I think it's kind of stupid to copy-paste all this into each document, and painstakingly go in and change each page separately if I just want to change one word or tag. Is there a way I can put this in template.htm (or something similar) and have php or javascript code take this and insert everything from the requested file inside of the <article> tag? I don't know a lot of php, so this is probably a piece of cake for you gurus, but I would appreciate the help.
Using php includes:
Save this as top.php
<html>
<head>
<title><?php echo $title; ?></title>
<script src="modernizr-1.5.js"></script>
<link href="styles.css" type="text/css" rel="stylesheet" />
<meta charset="utf-8" />
</head>
<body>
<div id="container">
<header>
<img src="images/banner.png" alt="World War I" style="border: none"/>
<nav>
<ul>
<li><span>Home</span></li>
<li><span>1914</span></li>
<li><span>1915</span></li>
<li><span>1916</span></li>
<li><span>1917</span></li>
<li><span>1918</span></li>
</ul>
</nav>
</header>
<section>
Save this as bottom.php
<aside>
</aside>
</section>
<footer style="font-weight: bold; letter-spacing: .1em">
Citations •
About
</footer>
</div>
</body>
</html>
Then your individual pages would be like this:
<?php $title = '1914'; include("top.php");?>
//This would be where you would make the changes that need to be made on each page.
<article>
<br style="clear: both" />
</article>
<?php include("bottom.php");?>
You could consider one of these popular template engines :
Smarty (becoming outdated)
Latte (used mostly by the Nette community)
Twig (used mostly by the Symfony community)
Mustache (official implementations of this templating engine exist in more than two dozen proramming/scripting languages)
I tend to favor Mustache, mostly because it has an official JS version, an official Ruby version, an official Java version, etc. It allows you to use the same templates frontend and backend, which is very useful for widgets that are first rendered in background and rerendered in foreground at updates.
Put the content in a file abc.php
and then add this to each page you want the desired content in :
<?php
include("abc.php");
?>
So if your code is :
<nav>
<ul>
<li><span>Home</span></li>
<li><span>1914</span></li>
<li><span>1915</span></li>
<li><span>1916</span></li>
<li><span>1917</span></li>
<li><span>1918</span></li>
</ul>
</nav>
</header>
<section>
<article>
<br style="clear: both" />
</article>
<aside>
</aside>
</section>
And you want the part inside <nav> to be repeated in each page, you can put the content between <nav> and </nav> (including the tags) inside abc.php and include abc.php in your file like this :
<?php
include("abc.php");
?>
</header>
<section>
<article>
<br style="clear: both" />
</article>
<aside>
</aside>
</section>
Create the template file as a single file, exactly you have it already, but printing PHP variables in the places where you want content.
eg:
....
<article>
<?php print $mainContent; ?>
</article>
....
Then write a PHP function as follows:
<?php
function insertContentIntoTemplate($mainContent) {
require_once('/path/to/template.php');
}
?>
Now you can load your page content into the template simply by calling the function and passing the content into it for each page.
So, for example, 1914.php could look like this:
<?php
require_once('/path/to/insertFunction.php');
//the text could be loaded from a DB, or from another file, or just as a plain string like this:
$text = "The year was 1914, and the war was just starting.";
insertContentIntoTemplate($text);
?>
Congratulations. You now have a working (albeit very simple) template system. Add more variables / placeholders to your template as required.

Can I reference an external JavaScript file in a PHP file?

I cannot seem to get this to work. I have a small JS file that switches banners depending on the time of day, but it seems that doing an external reference in my PHP file does not work. It works fine in an HTML page.
This is the code in the JavaScript file.
function getStylesheet() {
var currentTime = new Date().getHours();
if (7 <= currentTime && currentTime < 19) {
document.write("<img src='images/banner_day.jpg'>");
} else {
document.write("<img src='images/banner_night.jpg'>");
}
}
getStylesheet();
And here is the reference code I used to call the JavaScript file. Its in a PHP file.
<script src="http://beta.website.com/wp-content/themes/theme/scripts/banner.js"></script>
Everything on the PHP page shows up in the browser, except for the banner that I tried to call with the script.
Here is the entire PHP file code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div id="main">
<div class="container">
<div id="header">
<div id="logo">
<img src="http://beta.dfdfdf.com/wp-content/themes/asdafd/images/logo.png" />
</div>
<div id="shadow2"></div>
<div id="shadow1"></div>
<ul id="menu">
<li>df</li>
<li>df</li>
<li>fd df df</li>
<li>The asfdssd asdfds</li>
<li>sf</li>
<li>df</li>
<li>dfd</li>
</ul>
<div id="slogan"><big>"fasfdsads2005."</big></div>
<div id="loginDiv">Login Panel Here</div>
</div>
<div id="banner">
<script src="http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js"></script></div>
<div class="sidebar">
Sidebar Content<br />
<br /><br />
blah
blah
blah
</div>
<div class="content">
Main Content
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<div id="footer">
<div class="container">
<div class="footer_column long">
<h3>Cadsfadsfafdfsd.com All Rights Reserved</h3>
<p>dsafasdfdffadffadsdafsdfsadafsdfsadfas</p>
</div>
<div class="footer_column2">
<h3>More Links</h3>
<ul>
<li><a href="http://aadfsdfsdfa.
com">asfsdfa</a></li>
<li><a href="http://ItsNotch.
com">ItsNotch</a></li>
<li><a href="http://adfasfsfaf.
com">adsfasf</a></li>
<li><a href="http://twitter.
com/safs">Twitter</a></li>
<li><a href="https://www.facebook.com/group.php?gid=10215yy20340498">Facebook Fan Page
</a></li>
</ul>
</div>
<div class="footer_column2">
<h3>RSS</h3>
<ul>
<li>RSS Feed</li>
<li>What is RSS?</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Try changing your Javascript to this:
window.onload = function () {
// Uncomment this line to make sure the script is loading/running, then delete it
// alert('Hello, your Javascript is running');
// Declare variables
var currentTime, bannerDiv, newImg;
// Get currentTime
currentTime = new Date().getHours();
// Get 'banner' div
bannerDiv = document.getElementById('banner');
// Get create a new <img>
newImg = document.createElement('img');
// Assign a src="" attribute depending on the time
newImg.src = (currentTime > 7 && currentTime < 19) ? 'images/banner_day.jpg' : 'images/banner_night.jpg';
// add the new image to the document
bannerDiv.appendChild(newImg);
};
And put it in the <head> of the document, so change
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<!-- ....... -->
<div id="banner">
<script src="http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js"></script>
</div>
<!-- ....... -->
...to:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js"></script>
</head>
<body>
<!-- ....... -->
<div id="banner"></div>
<!-- ....... -->
If it still doesn't work, one of two things is happening:
Your javascript file is not actually at http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js so the script cannot be loaded/run. Uncomment the first line to confirm that the script is running.
Your banner images cannot be found at images/banner_night.jpg - make sure your relative paths are correct.
Have you made sure that you've actually got the <script></script> in your head? I certainly can't see it there (unless it is in <?php wp_head();>?).
Having attempted to navigate to http://beta.website.com/wp-content/themes/theme/scripts/banner.js it returns a 404, you'll need to fix this issue first.
Remember that javascript is client side, and all you want to do with php is spit out the tags somewhere for the browser to deal with.
It might be path problem.
What is you images (full) path?
What is you page (full) path?
As it is now it looks for images directory beneath your current page. Is this correct?

Categories