Can I please have some help to call a PHP file via Javascript when an HTML page has successfully loaded.
Here is my code:
<script class="code" type="text/javascript">
$(document).ready(function(){
//Call fb_marketing.php
});
</script>
The name of the PHP file is 'fb_marketing.php'. Can I please have some help to get this code working?
Thanks in advance
You can call this file in AJAX as Musa said. Here is a sample code that tells what I mean:
<script class="code" type="text/javascript">
$(document).ready(function(){
$('#div').load('fb_marketing.php');
});
</script>
In this code, #div calls the place that you want to put your PHP file. You can use this instead of '#div'.
you have to watch the outcome you expect from php, one way to do is use JSON notation for a quick reading of the data. To this we must put the corresponding header in our php file which will return the result.
Related
Below is my code
<a class="foot" href="<?php echo someurl.com?id;?>" >Info</a>
Iam setting a click function for the class 'foot'
$('.foot').click(function(){
alert('run some functions');
});
As you can see on the code above First it runs Jquery later on it will be passed to specified Url... But is it possible to pass to specific url then run Jquery.. ???
You have to use Ajax request if you want to call the URL without moving to another page. otherwise your javascript code won't execute.
http://api.jquery.com/jQuery.ajax/
if you are doing this for a fallback in case your client doesn't support Javascipt then you have to do it like this.
$('a').click(function(e){
e.preventDefault();
//your code
});
if you want the code to run after page load then I suggest you introduce:
$(document).ready(handler)
That way jquery runs AFTER page load.
You need to use prventDefaults and then trigger document.location.href to your clicked link.
http://api.jquery.com/event.preventDefault/
Hopefully this will solve your problem.
Why you are not using $(document).ready(handler)
<script type="text/javascript">
$(document).ready(function() {
$('.foot').click(function(){
alert('run some functions');
});
});
</script>
<a class="foot" href="hello.php" >Info</a>
I have this jQuery script in my web page:
<script>
$(function() {
function callAjax(){
$('#lastlogins').load("ur.php");
}
setInterval(callAjax, 1000 );
});
</script>
But, in container lastlogins, there isn't shown file ur.php, it is just empty. I have created div like that:
<div id="lastlogins">
</div>
I don't see any wrong on your code. Only problem would be with your ur.php file. Just try to access your file directly and see, if that works.
In the meanwhile, refer my sample code.
<script>
$(function() {
function callAjax(){
$('#lastlogins').load("/SivaCharan/EGxWL/show");
}
setInterval(callAjax, 1000 );
});
</script>
<div id="lastlogins">
</div>
Refer my LIVE DEMO
After a quick review of the .load function, there can only be one possibility... your PHP file is not returning anything useful. Run it directly in a browser and make sure it returns valid HTML.
Your jQuery function is correct as is.
i've a question, in a php script I've to do some things, and I need to be really fust, but in the script I've to do some database controlls, too, so, I would know if it's possible run an external php page, that do something, but without wait for its results.
Thanks
(P.S.: sorry for my english)
You'll want to look at using Javascript and Ajax. This allows you to run a php script from within a page asynchronously.
If you use a javascript library, such as jQuery then you can use something similar to this:
$.get('my_script.php', function(response) {
// This code is ran when the page has been loaded
// `response` is the content you get back from script
});
For more information have a look at the jQuery documentation on the $.get function.
You can use Ajax to do that, it let another script run in background.
Yes, you can use Ajax for this. Using Ajax you can run php code in the background, however if user navigates to another page, the execution of that php code will be terminated. The simplest way to get started with Ajax is to use a library like jQuery. See http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/
Hope this helps
--EDIT--
This how you can achieve calling exec(php script.php) using Ajax and PHP
home.php (here you are using Ajax to run exec.php)
<!--include the jQuery library -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js"></script>
<script type="text/javascript">
function runInBackground(){
$.get('exec.php', function(data) {
});
}
</script>
exec.php
<?php
exec(php script.php);
?>
I'm trying to use php tag in a javascript function. But unfortunately its not working. I even used <?php but still not working. I checked php.ini file and there the short tag is already enabled.
Any sort of suggestion will be appreciated.
You should be aware that a php tag inside javascript gets evaluated on the serverside before it gets sent to the client. Example: If the php variable $feeling holds the string "love" "I love cheese." gets alerted.
<script type="text/javascript">
var text = "I <?php echo $feeling;?> cheese";
alert(text);
</script>
In that case the javascript code that gets returned from the server will look like:
<script type="text/javascript">
var text = "I love cheese";
alert(text);
</script>
I hope this will be of help to you to solve your issue.
as you described here, your code should be working. you can use php code in JavaScript with PHP tags() if you are using core php.but its not working in smarty structure. so please mind it and check if you are using any framework or structures.
Thanks.
i need to have some php code inside javascript
<script ...>
<?php
echo " ... ";
?>
</script>
but this doesnt work. how can u implement php inside javascript that is in a own file javascript.php?
That doesn't do what you probably think it does. It'll work, but the PHP gets run once, when the page is loaded, not every time the JavaScript function is called.
Just for clarification, this is what will happen
index.php
<script type="text/javascript">
<?php echo "alert('hello!');"; ?>
</script>
output html in browser
<script type="text/javascript">
alert('hello!');
</script>
If that is what you want to do, then you can output all the javascript you like. What you cannot do is execute PHP code in the user's browser.
your can use php to dynamically generate javascript code, but you cannot execute php client side. If you need to execute php you will need to postback or use AJAX
There seems to be a good bit of misunderstanding of the question... Here is what you want to do to generate JS from PHP on the server:
file javascript.js.php
<?php
header('Content-Type: text/javascript');
?>
// javascript code here
function PrintTime()
{
alert("The time is " + <?php echo json_encode(time()); ?>);
}
Now, include it on the HTML page using normal script tags:
<script type="text/javascript" src="/url/to/javascript.js.php"></script>
The server will process the PHP file, and return javascript from it.
You can't run PHP inside a javascript file. Primarily because PHP runs server side and is processed before the client is sent any actual http info. JavaScript is processed by the browser on the client side and is sent as text.
It looks like you want to pass some kind of dynamic info to the JavaScript. You can do this by passing a variable like this:
<?php $variable="its me"; ?>
<script>
alert('<?php print($variable)?>')
</script>
The output passed to the client is:
<script>
alert('its me')
</script>
What are you trying to accomplish and maybe we can help you come up with a better solution?