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';
}
}
Related
I have two php files (1.php and 2.php) linked with require
php.1 has a qr <img src="http://chart.googleapis.com/chart?chs=125x125&cht=qr&chl=<?php echo $_jattu; ?>" width="50%"> which gets its value from a string variable in 2.php $_jattu;
What I want is for <?php echo $_jattu; ?> to only echo when <a class="w3-button2 w3-black2"></a> is clicked and not when the page is loaded or refreshed, what can I do to achieve this?
This is imposible to do on the server side. Because the "onClick" event jumps on the client side when de user do the action.
You have few options.
Enable another URL for load the content of $_jattu and when the user click load it with an AJAX request. Is the best way to do it and the result is more smooth and user friendly
As you say you want to do it refreshing. So, slightly refresh the page with a new parameter on your url that tolds you that the "onClick" event has jump. Like:
.../your/path?hasClick=true
And in your php code:
if(isset($_GET["hasClick"]) && $_GET["hasClick"]){
echo $_jetty;
}
http://php.net/manual/en/reserved.variables.get.php
if you want to remember that the user has clicked "forever" you can setup a cookie.
http://php.net/manual/en/function.setcookie.php
You can use ajax. Make a request to whatever php script you want which will return $_jattu when clicking on the link. Then update your img link in javascript using the return of the ajax request.
I am teaching myself, so I apologize in advance if this is a 'newbie' question, but I have searched the internet and cannot find a solution.
So I've created a HTML form, and in that form I have added a Dropzone.js where people upload a file. When finished they click a button which saves it into a MySQL form, that all works great. The issue is when people click the button before the file finishes uploading. To counteract this issue, I've created an 'If Statement' that checks if the uploaded file exists. If it hasn't uploaded, then a JavaScript alert is triggered, which all works, but it refreshes the page, which erases all the imputed data.
// This is when the button is clicked
if(isset($_POST['Upload'])){
if (file_exists(blah blah) {
//All my MySQL stuff is in here
//If it isn't uploaded
} else {
echo '<script language="javascript">';
echo 'alert("Please upload at least one file")';
echo 'return false';
echo '</script>';
}}
Despite the 'return false'; it still resets the page?
Any help would be appreciated, thanks!
Can you show me the all code. You can do it with lots of methods i do it with validation. you can set a variable which increase on file upload. if variable value zero then return false form will not submit.
Php does not support like this echo 'return false'. you have to use JavaScript validation for this purpose.
<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
}
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.
I am Using redirect function of Codeigniter with alert of JavaScript. I want to display message first before redirect to other page, but my code just directly redirect the page without displaying alert message. I need code in method of controller of codeigniter.I used code as follows in method of controller:
echo "<script type='text/javascript'>alert('Duplicate Users')</script>";
redirect('auth/login'),'location');
Plz Advice me
Waiting for Response
Thank You very Much for supporting me.
See this ..
Server code (PHP -> redirect) is fired up before Client Code (JS -> alert). There are several solutions to this but the easiest one is to use one type of code for the functionality (either PHP or JS) ..
Let's use PHP to fire up JS for now, since that's what you're using ..
echo "<script>alert('Duplicate Users') ; window.location.href = 'auth/login'</script>"
<script>
alert("Duplicate Users");
window.location.href = "auth/login";
</script>