on my website I want to show a Logout button when a user is logged in and a logout button when the user is not logged in.
I wrote this code:
if(isset($_SESSION["username"])){ echo "<li class='xy'><button class='x' onclick='location.href = 'index.php';'>Logout</button></li>"; } else { my login button... }
The buttons do appear but unfortunately the are not clickable.
How can I solve this problem?
You are not properly escaping quotations
Replace:
onclick='location.href = 'index.php';'
with:
onclick='location.href = \'index.php\';'
You need to escape the single quotes as they are mixing up with each other.
Corrected Code:
<?php
if (isset($_SESSION["username"])){
echo "<li class='xy'><button class='x' onclick='location.href = \'index.php\';'>Logout</button></li>";
}
else {
//my login button...
}
Another solution can be not to write HTML in PHP.
PHP and HTML can be embedded into each other without any restriction.
You are embedding HTML into PHP. Embed PHP in HTML:
<?php
if (isset($_SESSION["username"])){
?>
<li class='xy'><button class='x' onclick="location.href = 'index.php'">Logout</button></li>
<?php
}
else {
//my login button...
}
If you just want to send the user to an URL target (index.php in your example) use a link, instead of re-inventing a link and simulating it using JS. Style the link to look like a button. (There are plenty CSS examples on the web on how to do that.)
<?php
if( isset( $_SESSION["username"] ) ) {
echo '<li class="xy"><a class="button x" href="index.php">Logout</a></li>';
}
else {
// my login button...
}
Related
I recently created a new login-register system for myself but i'm having issues with htmls indenting when echoed from php, Now that i've really looked at it, i realized that echoing a entire page isnt the smartest idea, unless it is?
<?php
if (isset($_SESSION['sessionId'])) {
echo('This is the new content after login.');
} else{
echo 'This is the login page';
}
?>
above is my index.php the first echo would echo the entire page of content the user would see after they logged in.
Second is the login form.
what would be the easiest way to go about this?
You can do something like this
<?php
if (isset($_SESSION['sessionId'])) {
readfile("/path/to/html/file/new_content.html");
} else{
readfile("/path/to/html/file/login.html");
}
?>
You can try echoing your HTML output like so
<?php
if (isset($_SESSION['sessionId'])) {
echo '<br><p>';
echo'This is the new content after login.';
echo '</p>';
} else{
echo '<br><p>';
echo '  This is the login page';
echo '</p>';
}
?>
You'll see the above code in view source if you use it
Output:
<br>
<p>
  This is the content after login
</p>
second output
<br>
<p>
  This is the login page
</p>
P.S. BTW why are you using brackets for one echo and none for the other
???
I'm using Colorbox for my PM system. Clickin on the 'Inbox' link, opens colorbox and loads the inboxpage via ajax. The inboxpage shows linked titles of the users messages. Clickin on that, should open the read_message page inside the same colorbox. Unfortunately, that is not the case. Whenever I click on the link, it just opens the page outside the box.
I've tried different things, but no love whatsoever. Any help would be appreciated!
Piece of javascript (If you need the whole javascript of Colorbox.js, please let me know so!)
$(document).ready(function(){
$("#ajax").colorbox({ajax:true, width:"500px", height:"450px"});
});
Page that opens the Colorbox once $newMessages or $noNewMessages has been clicked
<div class="pageHeader">
<p class="title"><?php echo $myAccount ?></p>
<a class="viewMessages" id="ajax" href="message/inbox.php">
<?php
if (mysqli_num_rows($getAmountMessages) > 0 ) {
$row = mysqli_fetch_array($getAmountMessages);
if($row['message_read'] == 0){
echo $newMessages;
} else {
echo $noNewMessages;
}
}
?>
</a>
</div>
Inbox page - Clickin on the Linked Titles should open the read_message.php file within the Colorbox
//Select messages in db
$getMessages = mysqli_query($mysqli,"SELECT * FROM messages WHERE recipient = '".$_SESSION['user_id']."' ORDER BY message_id DESC");
$numMessages = mysqli_num_rows($getMessages);
//Message(s) available for user
if (mysqli_num_rows($getMessages) > 0 ) {
echo '<ul>';
for($count = 1; $count <= $numMessages; $count++)
{
$row = mysqli_fetch_array($getMessages);
//Show if a message is still new
if($row['message_read'] == 0)
{
echo '<a id="ajax" href="message/read_message.php?messageid='.$row['message_id'].'">'.$row['message_title'].'</a>(NEW MESSAGE)<br>';
}else{
echo '<a id="ajax" href="message/read_message.php?messageid='.$row['message_id'].'">'.$row['message_title'].'</a><br>';
}
}
echo '</ul>';
//No message(s) available for user
}else{
echo ("<p class='messagesinfo'>Er zijn geen nieuwe berichten</p>");
}
Thank you.
When clicking on a message (link), you are telling the browser to do a redirect to that link, which will result in a new page load.
Instead, you need to call $.colorbox via javascript.
Change your links from:
<a id="ajax" href="message/read_message.php?messageid='.$row['message_id'].'">'.$row['message_title'].'</a>
to
<a id="ajax" onclick="showMessage($row['message_id'])">'.$row['message_title'].'</a>
In your Javascript add the following function:
function showMessage(id) {
$.colorbox({href:"message/read_message.php?messageid=" + id});
}
Hope this helps!
I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.
I am trying to create some text based share buttons for my Wordpress that also output the shared amount. So far I got it working with Facebook and Delicious but I am not sure how to get it going with Twitter.
This is what I did for Delicious.
<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$deliciousStats = json_decode(file_get_contents('http://feeds.delicious.com/v2/json/urlinfo/data?url='.$shareUrl));
?>
<a onclick='window.open("http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>' class='delicious'>
<?php
if($deliciousStats[0]->total_posts == 0) {
echo 'Save';
} elseif($deliciousStats[0]->total_posts == 1) {
echo 'One save';
} else {
echo $deliciousStats[0]->total_posts.' saves';
}
?>
</a>
I also got the API Url which calls the tweeted numbers and URL.
http://urls.api.twitter.com/1/urls/count.json?url=SOMESITEURLHERE&callback=twttr.receiveCount
Basically it calls the JSON encoded file, and then gives you the option to share the link in <A></A> tags but instead of showing some text such as Share, it will show the count instead. I'm basically creating some CSS share buttons.
Just use Twitter's own tweet button.
It does that for you and you can style it with .twitter-share-button
(I would post this as a reply but I don't have the privilege.)
Probably you have figured out a solution yourself by now. I just had the same problem and solved it this way:
$handle = fopen('http://urls.api.twitter.com/1/urls/count.json?url=nu.nl', 'rb');
$twitCount = json_decode(stream_get_contents($handle));
fclose($handle);
print_r($twitCount->count);
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
function total($url){
return get_tweets($url); }
Then, use this to get the twitte share count in required place.
<?php echo total("http://website.com/"); ?>
so after all the confusion here (my apologize)
here is what I have figure I will just re-write it here and make it clear and just for the future notes (I know people won't really need this) but then will just be like a case study
what I was actually wanted is like this example
***********************************************
** link1 - link2 - link3 - link4 - link5 **
***********************************************
** content here, here's the words **
** content here, here's the words **
** content here, here's the words **
** content here, here's the words **
** content here, here's the words **
***********************************************
then I found it something like this
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
<?php
if(!isset($HTTP_GET_VARS['link'])){
$link = 1;
} else {
$link = $HTTP_GET_VARS['link'];
}
if ($link == 1) {
echo "<div id="player1"blah blah with video link1";
} elseif ($link == 2) {
echo "<div id="player2"blah blah with video link2"";
} elseif ($link == 3) {
echo "<p>Some text about link 3</p>";
} elseif ($link == 4) {
echo "<p>Some text about link 4</p>";
} else {
echo "<p>Some text about link 1</p>";
}
?>
then #royrui clarify me that
the solution should be like this
<?php
if(!isset($_GET['link'])){
$ch = 1;
} else {
$ch = $_GET['link'];
}
if ($link == 1) {
echo "<div id='player1'blah blah with video link1'";
} elseif ($link == 2) {
echo "<div id='player2'blah blah with video link2'";
} elseif ($link == 3) {
echo "<p>Some text about link 3</p>";
} elseif ($link == 4) {
echo "<p>Some text about link 4</p>";
} else {
echo "<p>Some text about link 1</p>";
}
?>
so if you notice first thing I changed from if(!isset($HTTP_GET_VARS['link'])){
to if(!isset($_GET['link'])){
in the div all it was " so I change it to ' and now the div can be place in echo
and now every single time when people click on link 1 then that div will be place and 2 and so on.
I know it was my problem with the confusion questions but then I really appreciated with all the help from here.
I want to vote the best answers but all it was useful so I just thought of put together to answer myself in this first post
Thanks once again!
The trick is that your PHP code is running on the server and not the browser. You need a combination of code running on both sides to accomplish this (known as AJAX).
Here is some example JavaScript code (no library needed) to replace the contents of a div:
<script type="text/javascript">
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
function getLinkText(linkNo) {
http.open("GET", "getLinkText.PHP?link=" + linkNo, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
document.getElementById('divLinkText').innerHTML = http.responseText;
}
}
http.send(null);
}
</script>
<p>Link1</p>
<div id="divLinkText">
This will be replaced by the AJAX Call.
</div>
The next step is to make a PHP page that returns the contents of the DIV tag at getLinkText.PHP?link=1. I'll leave that to you as it seems you have that part down.
As others have mentioned, adding the jQuery library makes this code a lot more compact and I recommend it.
I'm doing this with a javascript library called jquery, because the way you specify it, it looks like you want some AJAX functionality. You can do this in javascript without the use of a library, but then you have to work around quite some browser incompatibilities.
For this example, I will assume that all your navigation items are placed in an element with an id of nav.
then your javascript code should look like this:
$('#nav a').click(function(){
$('#destinationdiv').load($(this).attr('href'));
});
The php looks like this: First, you make your main php file, such as page.php or whatever.
<?php
$secure=true; //This will ensure that users can't request any content without this script in between.
$pagenum = (isset($_GET['page'])) ? $_GET['page'] : 1;
require('page'.$pagenum.'.php');
?>
and now, you add php files for every page of content that you want to serve, they should all look like this:
<?php if(!$secure) die("access denied!"); ?>
<h1>My awesome page content!</h1>
<p>bla bla bla</p>
so, to glue it all together
On your server, you should have files for each page you want the user to ba able to view. In your navigation, your a tags should have a href tag of page.php?page=1 or whatever. So, if you have a file called page1.php, and then put page.php?page=1, the javascript will stop the link from actually linking to that file. Instead, it will make a request to your server for page.php with a get variable called page, with a value of 1. the php script now looks on the server for a php file called page1.php and includes that in the current script. When PHP is done executing, the output will be sent back to the browser. Than the javascript load function that we called to request the page will insert it into the div we selected.
<li>Link 1</li>
<li>Link 5</li>
</ul>
<?php
if(!isset($_GET['link'])){
$link = 1;
} else {
$link = $_GET['link'];
}
if ($link == 1) {
echo "<div id='player1'>content here, here's the words plus html'video link1'</div>";
} elseif ($link == 2) {
echo "<div id='player1'>content here, here's the words plus html'video link1'</div>";
} elseif ($link == 3) {
...
} else {
echo "<p>Some text about link 1</p>";
}
?>