Is it possible to disable jQuery and Javascript interactions unless the user is logged in?
At first I wanted to put all the interactions in a seperate file, and then load it in with PHP when the user is logged in, but is there a different way to do this, or would you think it's better to do the following:
Create a log-in screen before the user comes to the main part of the website.
We have HTML5 now! Just create a HTML session or a php $COOKIE and control your javascript functions with a simple if statement.
WEB STORAGE
localStorage.setItem("is_logged_in", var);
if(is_logged_in){
//all of your functions in here
}
You can read more here http://www.w3schools.com/html/html5_webstorage.asp
PHP
Easier way and personally my favorite is using PHP.
$var = "is_logged_in";
setcookie($var , time() + (86400 * 30), "/")
And then for the check:
<?php if(isset($_COOKIE[$var])) { ?>
//all of your functions in here
<?php } ?>
You can read more here http://www.w3schools.com/php/php_cookies.asp
EDIT
And ofcourse you can always instead of having a huge if statement you can disable all controls in a very smaller if.
<?php if(isset($_COOKIE[$var])) { ?>
$('button').attr('disabled', 'disabled');
<?php } ?>
Potentially if there is an object on the page present when you are logged in, that isn't if you are not, then you could wrap something like this around your JS code:
if ($('LoggedInElement').length) {
// All my code
}
No. Disabling JavaScript is a browser function and cannot be controlled programmatically.
Related
So i have this code:
<?php define('load', 'loaded'); ?>
<body>
Load
</body>
and here is my script:
<script>
$(document).ready(function(){
$('#load').on('click', function(e){
e.preventDefault();
$('body').load('joke.php');
})
})
</script>
my joke.php contains:
<?php echo load; ?>
I wonder if there is a way that the loaded file can read previous scripts sir. Please help
so if click the load, then the joke.php will be loaded in the body. The joke.php contains echo $load, however, after the file is loaded, it doesnt recognize the $load variable, even if i put it above the body or before html
You have to pass the value back to the server from the client.
<script>
$(document).ready(function(){
$('#load').on('click', function(e){
e.preventDefault();
$('body').load('joke.php?load=<php echo $load; ?>');
})
})
</script>
Basically your URL will look like this joke.php?load=loaded,
Then in your joke.php you can retrive it by using $_GET['load'], or in your case echo $_GET['load'].
Alternatively you can use POST, but you can't do that using load. You'll have to use $.ajax or $.post for that.
You can't access it the way you were doing it because the client and the server are 2 separate and distinct entities.
I suggest reading up on the differences between the client and the server and what is a client or server side language. I would offer an explanation but it's outside the scope of this answer and there are plenty of sources on the internet that can do a better job of explaining then I can in a format like this (Stack Overflow).
The best I can do is think about putting that URL right in the browser (which is perfectly acceptable to test it). Now with it in the browser URL bar, where does that value (for load) come from. It has to come from somewhere, this is essentially what you are doing with load(). You are making a separate and distinct request to the server from the client.
The only other way would be to store that data in some kind of persistent data storage, such as a file, a DB, the Session, etc.
I write some html, javascript and php code that normally have to execute php code when a html button is pressed if you click on "ok" on the "confirm box", so there is my html code :
<input onclick="logout()" name="logout" type="submit" value="Logout me" />
Javascript and php code :
function logout() {
if (confirm("Are you sure you want to logout ?") == true) {
<?php echo "Hello world !"; ?>
} else {
<?php echo "Goodbye world !"; ?>
}
}
I think that the javascript code work because when i click on the html button the confirm box appear like it has to appear, but the php code is not executed.
Please if someone would help me it will be greatfull.
You can't use php in javascript because php is a serverside programming lenguage and javascript is a client side programming lenguage, so php is executed (by the server) before javascript (that is executed by the browser of the user).
You have to use Ajax requests for your needs.
Quickstart Ajax guide: https://www.w3schools.com/xml/ajax_intro.asp
Even though it is possible to write JS inside of PHP, I don't think this is the way to solve this problem. You can only (as far as I know) echo PHP values inside JS that is executed inside tags, when you write HTML inside .php files.
PHP is indeed server side and the Javascript is in this situation client side. You're thinking wrong.
First you should extract your logout functionality to a logout.php file for example. Then you could do either one of the two:
When you really want to use the logout function as a direct event listener to the onClick event on your input, your logout callback function should make an AJAX call to logout.php, which logs out your user. See the answer from #Helio to see more about AJAX.
Your input should be placed inside a <form>, which has an action attribute, that calls to your logout.php. Then when submit is called, it is available to you through the $_POST superglobal, and you can check if it is pressed. Then you simply log out the user.
That doesn't appear to be valid Javascript.
Within the Javacript function, you are dropping a PHP statement as if you were writing it to the HTML, this will not work. I'm surprised you got the dialog to popup.
PHP is a server side language and Javascript is not, therefore you should not even call PHP code like that from any kind of Javascript.
To help your curiosity, you could get away with using the document.write() function, but don't use this.
I would recommend rewriting your function to redirect to a specific PHP page.
function logout() {
if (confirm("Are you sure you want to logout ?") == true)
{
// try to avoid this....but still works
document.write('<?php echo "Hello world !"; ?>');
// try this instead
window.location.href = '/helloworld.php';
}
else
{
// try to avoid this....but still works
document.write('<?php echo "Goodbye world !"; ?>');
// try this instead
window.location.href = '/goodbyeworld.php';
}
}
So I'm making this web control panel but I'm not good with JS. I'm using MaterializeCSS + HTML + PHP for the DB login and so on.
I would like to have the following simple functionality but for some reason I'm failing.
I'd like to fill in the username and password in my form and press Log in. Since I'm not sure how Toasts work, I suppose once I switch the page the Toast actually disappears so maybe it's better to display the toast in the new Account page that opens instead of the Login page itself. I tried both but it doesn't appear.
Code to show Toast:
echo "<script> Materialize.toast('Login successful!', 3000, 'rounded') </script>";
I found a topic somewhere in Russian saying this has a problem with DOM and used its solution without success again.
Note: I already use other JS-dependent components such as Modals and Tooltips.
Console error: Uncaught ReferenceError: Materialize is not defined
I had 2 problems. Most importantly, I was testing the code in an IF that wasn't even executed. Stupid me. Secondly, I wasn't using onDocumentReady - $(function(){});
The document referrer is just to check which page sent me there but here's my working code to create a MaterializeCSS Toast from PHP:
echo "
<script>
$(function(){
if (document.referrer == 'http://yourdomain/index.php?p=Login'){
Materialize.toast('Login successful', 2200, 'rounded')
}
});
</script>";
I had the same problem with one of my PHP scripts. In the end after a lot of research I found that I had not included JQuery. Materialize CSS needs JQuery loaded before materialize.js file. Make sure that jquery is loaded. The console error shows up because JQuery is not loaded.
I tested this code on my localhost without jquery toast doesn't work but when loaded it works fine.
<?php
if (isset($_GET['msg'])){
$msg = $_GET['msg'];
echo "<script>Materialize.toast('$msg', 3000, 'rounded');</script>";
}
?>
you can put your if condition in php like this
<?php
`if (document.referrer == 'http://yourdomain/index.php?p=Login')
{
echo "
<script>
Materialize.toast('Login successful', 2200, 'rounded');
</script>";
}`
?>
<script type="text/javascript">
confirm("Delete user?.");
window.location.href = "users.php";
</script>
$qqq = mysql_query("DELETE from users WHERE panelistname='$theuser'") or die(mysql_error())
considering the code above, (inside a php file, so no worries with certain syntax errors you might notice) the problem here is that when click cancel on the confirm() dialog box that will show up. the delete action still executes. This question might be considered a double since, yeah, I found some questions relevant to this one but I just can't fixed this one myself.
the one I found codes it something like this:
"if (confirm('Are you...?')) commentDelete(1); return false"
I can't find a way to solve this problem, I don't know which part should I insert the SQL command(delete) in this format. Please someone show me how to do this right. :) thanks!
EDIT: I just saw that Nick Zuber posted a similar answer around 1 minute before I posted mine (actually, while I was writing it :P)
I don't clearly understand what you are trying to do.
You want to show the user a confirm window, and if they click Yes, delete some entry in the database, and if they click No, redirect them to the page 'users.php' ?
If it's what you want to do, then you can't do it like this. You can't use JS conditions with PHP. The PHP code is executed server-side (in the server), whereas the JS code is executed client-side (in the browser). What you would need is to do something like this:
warning: don't use this code, it's unsecure and shouldn't ever be used in a real app, it's just to show you how the whole thing works
(IN USERS.PHP)
if(isset($_GET['delete_dat_user']))
{
$qqq = mysql_query("DELETE from users WHERE panelistname='" . $_GET['delete_dat_user'] . "'") or die(mysql_error());
}
(IN THE WEBPAGE)
if(confirm('u serious u want to delete the user'))
{
window.location = 'users.php?delete_dat_user=theUserName';
}
else
{
nope
}
When your page loads, the PHP on your page will automatically execute, regardless of your JavaScript. Instead, try to prompt the user if they want to delete the account and if they click yes redirect them to a page that has your PHP query.
Also, the confirm function returns a boolean value depending on which option is clicked by the user. Try putting it in an if statement:
if(confirm("Delete user?.")){
window.location.href = "delete_user_page.php";
}else{
// cancel was clicked
}
I have a page where a user creates an item to auction it. If he submits the item creation form, the browser automatically redirects him to the newly created page via the following line of php code:
header('Location: item.php?itemid='.$itemid);
I would like to display a notification right after the item creation (on the newly created page) saying that the item has been created. This is the (working) code I use to call the notifications:
<script type="text/javascript">
$(function(){
$container = $("#container").notify();
notifyItemCreation();
});
</script>
EDIT: I think people are misunderstanding the problem.
So my sellitem.php page contains a form to sell an item, if this is submitted it gets send to createitem.php, this is where I do the validation checks.
If everything is okay this is where I redirect to the newly created item.php?item='$itemid' page. I want on this (item.php?item='$itemid') page the notification to be displayed.
EDIT2: I do know how to create the item.php?itemid='$itemid' page, this gets done perfectly, it is the notification to appear that is the problem. I am using the notifications from http://www.erichynds.com/blog/a-jquery-ui-growl-ubuntu-notification-widget
Can the people who are downvoting also explain why they are.
You can use session variables. In the next request if a certain variable exists or indicates that the current item is new, you can echo the script tag and unset the variable.
You could just wrap the JS in a condition
<?php if(is_numeric($itemid)){ ?>
<script type="text/javascript">
$(function(){
$container = $("#container").notify();
notifyItemCreation();
});
</script>
<?php } ?>
You could use ISSET() as well