how to execute a php function from a separate php file onClick - javascript

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.

Related

Load removes previous PHP scripts

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.

Is it possible the invoke a js in html after php from validation and redirection? Now trying to solve with Ajax

I have a html form on my page. Im using a php file to send an email from the form, and after a successful email send its redirects back to the html form.
After that i want to fade in a bootstrap success-alert.
Is there any event that can handle this or anyway to solve this problem?
This is my JS now, but its dont run because if I hit the send button, its call the php file.:
<script type="text/javascript">
$(document).ready(function() {
$('#btnSubmit').click()(function(){
$('#myAlert').show('fade');
});
});
</script>
I'm assuming you need show alert after the form request is processed and page is redirected to original form page, In that case You need to pass some parameter as a flag in query string while redirecting from server side. On form page you need to check for that flag and show alert.
On server side while redirecting
header("Location:form_page.php?status=success");
On form page
$status = $_GET['status'];
Assign value of $status to js variable and show alert.
Use ajax to achieve that.
Catch the submit event with
$("#btnSubmit").submit(function(){//your ajax});
In the success do your $('#myAlert').show('fade');
You have a good example here : How to submit html form without redirection?

insert php into javascript

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';
}
}

Reload window DOM after injecting php element

I have two different playlist in php file and I have two buttons playlist1 and playlist2.
<div class="player">
<?php include ('playlist1.php');?>
<?php include ('playlist2.php');?>
</div>
If I click playlist1 in the homepage it has to reload and append the <?php include ('playlist1.php');?> before page loads.
If I click playlist2 in the homepage it has to reload and append the <?php include ('playlist2.php');?> before page loads.
I tried something but no luck.
$( "playlist1" ).click(function() {
var append_php = "<?php include ('playlist2.php');?>";
$('#playlist').append("<div id='playlist2'>"+append_php+"</div>");
});
You're trying to emit PHP directives in Javascript which will never work. Keep in mind that Javascript runs on the client (i.e. on a web browser on the user's machine) and PHP runs on the server. You, therefore, cannot render PHP directly on the client - it must be run on the server.
Running on the server requires you to either navigate to a different page (perhaps using a querystring to pick what playlist you wish to render) or look into using AJAX instead.
Given that you say:
If I click playlist1 in the homepage it has to reload and append the before page loads.
If I click playlist2 in the homepage it has to reload and append the before page loads.
I'd suggest you change your PHP to look something like the following:
<div class="player">
<?php
$playlist = isset($_GET['playlist']) ? $_GET['playlist'] : '1';
include("playlist".$playlist."php")
?>
</div>
Playlist 1
Playlist 2
This basically inspects the querystring and then renders whatever is in playlist1.php or playlist2.php based upon what you pass. By default it renders playlist1.php. It assumes that the PHP file you're running is called index.php.

Reload parent page after submit in iframe

I have the same issue as in this question. (which has no correct answer, I just test all of them):
Reload page after submit in iframe (javascript/jquery)
What I need is very well explained there.
I have a button in a form, all this inside an iframe (same domain) and I need to reload the parent page, just after the iframe has made the whole submit process, I do not want to "cut" it or interrupt it.
UPDATE:
I kept searching and I found this. But I can't understand the code in the correct answer, is that for asp? I'm on PHP.
Reload page after submit in iframe (javascript/jquery)
http://dotnetspidor.blogspot.mx/2011/07/refresh-parent-page-partially-from.html
Once the form has been submitted in the iframe
<form method="POST" action="getForm.php">
The page reloads into getForm.php, and in that file you do:
<?php
$input = $_POST['input'];
// process form and do your stuff etc
echo "<!DOCTYPE html>";
echo "<head>";
echo "<title>Form submitted</title>";
echo "<script type='text/javascript'>window.parent.location.reload()</script>";
echo "</head>";
echo "<body></body></html>";
?>
and that would reload the parent page after the form has submitted, now the only question is, why are you submitting through an iFrame if you're going to reload the parent page anyway.
all that you need to do is add the below line,
window.parent.location.reload();
at the end of the function that is called onclick of your submit button.

Categories