Bootstrap dropdown stop working after php die - php

Hi i have a script in which there are bootstrap dropdowns. The code for it is as follows:-
<ul class="nav navbar-nav navbar-right">
<li class="dropdown dropdown-user" style="font-size:15px;">
<a class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-user"></i> <span><?php echo $name; ?></span> <i class="caret"></i></a>
<ul class="dropdown-menu dropdown-menu-right width-220">
<li><i class="icon-lock2"></i> Change Password</li>
<li><i class="icon-switch2"></i> Logout</li>
</ul>
</li>
</ul>
Now i run a php script after a form is submitted in which i am checking whether a table is there in the database or not. If it is there it fethces it and if it is not there php script dies. The code for it is below:-
<?php
if(isset($_POST['submit']))
{
$sql="SELECT * FROM table1";
$results = $con->query($sql);
if ($results === FALSE) {
echo "<script> noty({text: 'No Data Found',layout: 'topRight',timeout: 3500,closeWith: ['click', 'hover'],type: 'error'});</script>";
die();
} else {
........
}
Now whenever the php script dies the dropdowns stop working.
I am just starting to learn php so please let me know what I am doing wrong or if there is a better way of looking at this problem. Any help is highly appreciated.

Check if you included bootstrap js file at the end of your page.
So when PHP dies, The bootsrap will not be loaded to the page. If its the reason change die with something else or move bootstrap js file to header.
PS:Besides use die whenever something goes wrong is not the best idea to handle errors.

Related

How to embed PHP inside a text file to include later?

I have a simple template created through my ClassPage. The class gets called and reads html code from a text file. Inside the navigation I want to echo a PHP variable but all I am getting is the embedded php commented out on the browser's console?
I'm thinking perhaps the file that gets read isn't being processed by the server and that is why the php isn't being processed? Or I'm missing something really simple?
Heres the code:
ClassPage -> getBody which gets the file called "superNav.txt". The string that gets returned is put together with the needed HTML head tags etc then outputted.
private function getBody() {
$text = "";
if ($this->specialUser) {
$text .= file_get_contents("Template/superNav.txt");
} else {
$text .= file_get_contents("Template/userNav.txt");
}
return $text;
}
This is the Text File:
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span id="glyph" class="glyphicon glyphicon-th-large"></span></button>
<a class="navbar-brand" href="#"><img alt="brand" id="brandLogo" src="Images/logo.png"></a>
<p id="navbarText" class="navbar-text">Webmin</p>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li>Rota</li>
<li>Rota Admin</li>
<li>User Admin</li>
<li>Archive</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span><?php echo $user->getName(); ?></li>
<li><span class="glyphicon glyphicon-log-out"></span> Log Out</li>
</ul>
</div>
</div>
</nav>
This is the line that is giving me an issue:
<li><span class="glyphicon glyphicon-user"></span><?php echo $user->getName(); ?></li>
The php does not get shown to screen, the browsers console shows that the code is commented.
Any help is welcomed. Thanks in advance!
For this first get the content from the .txt file and then do something like this :
$name = $user->getName();
$text = file_get_contents("Template/superNav.txt");
$text = str_replace('USERNAME', $name, $message);
$text$message = str_replace('logo_url', $base_url, $message);
and in your .txt file replace the php value with some Constants.
like this
<li><span class="glyphicon glyphicon-user"></span>USERNAME</li>
So here you can see we have replaced the value of USERNAME with dynamic value. Hope it helps!!!
file_get_contents does not execute php, as you have discovered.
Instead you can use output buffering and include.
You will also need to make sure $user is in scope for the getBody method as include inherets its scope from the calling block.
In this example i have used a made up method to illustrate that:
private function getBody()
{
//create local user variable somehow
$user = $this->getUser();
//start buffer
ob_start();
if ($this->specialUser) {
include "Template/superNav.txt";
} else {
include "Template/userNav.txt";
}
//return contents of buffer as string
return ob_get_clean();
}

Hiding GET parameters in php

I am working on local machine and my URL is:
http://localhost:91/GlobalVision/index.php?mm=1&sm=1
<? $mm=$_GET["mm"];
$sm=$_GET["sm"]; ?>
<ul class="sidebar-menu">
<li class="header">MAIN NAVIGATION</li>
<li class=" <? if($mm==1) echo "active" ?> treeview">
<a href="#">
<i class="fa fa-dashboard"></i> <span>Dashboard</span> <i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<li <? if($mm==1 && $sm==1) echo "class=\"active\"" ?>><i class="fa fa-circle-o"></i> Dashboard1</li>
<li <? if($mm==1 && $sm==2) echo "class=\"active\"" ?>><i class="fa fa-circle-o"></i> index2</li>
</ul>
</li>
</ul>
i have created this file as separate menu file and included in other php files.
i wanted url as:
http://localhost:91/GlobalSDK/index
I tried writing:
^([a-zA-Z]+)/$ index.php?mm=$1&sm=$2 [L]
this rules in .htaccess file but it gives me error.
What should be the rule to achieve this. Even when index page get change it should display only page name not parameters. What should be the rule for this?
Try these methods
1) Use a form and POST the information.
2) Use session variables to carry information from page to page.
3) Use "encoded" or non-sensical information in the QueryString in
place of the real data.

How to show username between html with PHP

My issue is: I have a login system and I wanna show the current username in the page.
calendar.php:
// SOME CODE
<ul class="nav navbar-nav">
<li>Welcome <? echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
// SOME CODE
This shows the "Welcome" but don't show username...
How can I do this? Thank you all in advance.
Make sure $_SESSION['username'] is already set, i.e:
index.php
<?php
session_start();
$_SESSION['username'] = "someusername";
calendar.php
<?php
session_start();
?>
<ul class="nav navbar-nav">
<li>Welcome <?php echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
Note:
Ensure session_start(); is the first statement on the script, i.e.:
<?php
session_start();
Otherwise you may get the error:
"Headers already sent..."
Learn how to use php sessions across multiple files here
Unless you are using a rather old system, you need
<li>Welcome <?php echo $_SESSION['username'] ?></li>
If it still doesn't show, ensure that the page is being parsed for PHP. Then, make sure that $_SESSION['username'] has a value.
You forgot to write php in your code:--
<ul class="nav navbar-nav">
<li>Welcome <?php echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
Note:-
1.This code file name will be .php not .html and if your Session have username index and have some value then only it will show.
2.As you asked for the error you need to put this code on very upside of your php page
<?php
session_start();
$_SESSION['username'] = 'any user name';
After that my above code and then everything works fine.
3.If you want this session value to any other page on that page write first session_start(); on top and then you will get the value.

LAMP Server not executing PHP scripts properly

I have a LAMP(Ubuntu) Server running on a physical machine. My goal is to host a website on the server. Right now, I have all my files in my /var/www/html directory and inside certain HTML files, I run PHP scripts to check if the user is logged in or not such as below:
<div class="collapse navbar-collapse" id = "navCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active link-nav">Home</li>
<li class="link-nav">Categories</li>
<li class="link-nav">About</li>
<!-- This part -->
<?php
if($_SESSION['loggedin'] != 1){
echo '<li class="link-nav">
Log In
</li>
<li class="link-nav">
Register
</li>';
}else{
echo "<li class = 'link-nav'> Log Out ";
}
?>
<!-- End of PHP -->
</ul>
</div>
When I open up to my server in the browser, all I get is this:
The $_SESSION['loggedin'] variable has already been set but both the Login and Logout buttons show. Also, the text in between the two sets of strings is showing on the page.
When I display this on my local computer using XAMPP, this doesn't happen.
Any ideas on how to fix this?
Thanks in advance!
This line is invalid. With proper error reporting on, you would see the error. When testing, it will help a lot to have error reporting turned on so you can see the helpful errors :)
echo "<li class = 'link-nav'> Log Out ";
You can not use the double quotes inside themselves without escaping them!
Instead write it like this:
echo '<li class="link-nav">Log Out';
See if that works.
Try this, It's much easier to work with code that has islands rather than everything being a PHP string(Also, take a look at http://www.Laravel.com and http://www.Laracasts.com):
<?php if($_SESSION['loggedin'] != 1){ ?>
<li class="link-nav">
Log In
</li>
<li class="link-nav">
Register
</li>
<?php }else{ ?>
<li class="link-nav"> Log Out
<?php } ?>

How do I apply my php code to a bootstrap code?

I am trying to display something from my database on a bootstrap bar and I have no idea how to combine or implement the two. Ive made my bootstrap html's file a .php, was that the correct thing to do? Here's my code (ignore the filler words)
<ul class="nav nav-list well">
<li class="nav-header"></li>
<li class="active">HIT INFO</li>
<li>Linky link</li>
<li class="divider"></li>
<li>ANOTHER HIT INFO</li>
<li>ANOTHER LINKY LINK</li>
<li class="divider"></li>
<li>YET ANOTHER HIT</li>
<li>AAAAAnd another link</li>
</ul>
and this is the php i want to include (the code to making what i want to display is in the file)
<?php include 'one.php'; ?>
where "linky link" is, i want to display this but it didnt seem to work when i put that php right there. Another thing i want to do is when a link from the database gets displayed, it displays as a bootstrap button. i tried and am just not sure how to implement php code in my bootstrap code.
Print " <td>".$row['link'] . "</td></tr> ";
How would i add this bootstrap code to that so when a link from my database gets displayed, it gets displayed as this button
<i class="icon-heart icon-white"></i>Do this HIT!
Your code seems right to me except to your "<td> and <tr>" tag. You're not using it right.
Try this approach
In one.php
<?php
$links = array( 0 => array("url"=>"http://google.com","text"=>"Google") );
?>
In Menu
<?php include('one.php')?>
<ul>
<li>Menu one</li>
<?php foreach( $links as $link){ ?>
<li><a href="<?php print $link['url']?>"><?php print $link['text']?></li>
<?php } ?>
</ul>

Categories