PHP include works but JQuery load doesn't - php

I have a bit of code in a div which needs to be refreshed every few seconds.
If I use a PHP include, the page works fine, but obviously can't be refreshed.
When I use a jquery load, none of my PHP functions work within the included file and none of the php variables are recognised. Any help would be hugely appreciated!
<script type="text/javascript">
function refreshPosts() {
$('div#refresh').load('/include/included_file.php');
setTimeout("refreshPosts()",1000);
}
</script>
<script type="text/javascript">
$(document).ready(function() {
refreshPosts();
});
</script>
In my php file, there are calls to various functions, which result in this:
Fatal error: Call to undefined function myFunction()

Since you did not add any code or helpful errors, we can only speculate on problem. My guess is the problem is the following:
Your main page has in it's php code some includes, or some object instantiations, which does not exist on your "jquery loaded" (I guess you mean an ajax request by that) page. I'm about 90% sure this is your problem, but can't give more details since... well... lack of things to go on.

Related

Unable to use $.get within WordPress page

I'm trying to use an ajax $.get() request to display calculated data within one of my WordPress pages. I haven't decided if I'm going to host the php calculation file on a different server of mine, or the one that WordPress is hosted on (don't think this would make a difference anyway since its a data request). I'm looking to add the following code to either the header.php file, or even better, within the page created in wp-admin:
<script>
$(document).ready(function(){
$.get("http://my-other-website.com/parse-list.php",function(data){
alert(data);
});
});
</script>
I know this is a basic function, but I'm just really stumped on how it doesn't work within WordPress, but works when on my other server that doesn't host WordPress. I tested jQuery to make sure it was being loaded properly, with a simple alert("hey");, and the alert works just fine. Has anyone else had this problem? Any help would be greatly appreciated!
jQuery in WordPress runs in noConflict mode which means the global $ shortcut for jQuery isn't available. Replace your code with the following:
<script>
jQuery(document).ready(function($){
$.get("http://my-other-website.com/parse-list.php",function(data){
alert(data);
});
});
</script>

Script-Tags generated by Javascript do not work?

I'm creating a webpage using php, mysql, html5&css3 and javascript (ajax).
To improve the performance, i decided to use AJAX to get only the content div (<div id="content">) that actually changes, i dont want to reload the whole code all the time. That works as follows: after having received the new content from a php-file, javascript does the following:
document.getElementById("content").innerHTML = new_content;
After that, the new_content is well displayed...
Now the Problem:
In the new_content, i have some javascript, too, e.g.:
<script type="text/javascript" lang="JAVASCRIPT">
alert('Hello!');
...
</script>
Unfortunately, when inserting it in the div, it isn't executed at all...when calling the php-file "naturally", the javascript code does what i want it to do, so there's no error in it.
Thanks in advance
EDIT: Problem is solved, i included jquery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
and used $("#content").html(new_content); in javascript as mentioned below.
Use jQuery's $.html. It will automatically execute scripts for you.
$("#content").html(new_content);
Indeed, by default script execution is disabled when just replacing HTML - makes sense if you look at it. You should really consider using a library like jQuery or Mootools to abstract issues like this away.
If you look at Mootools' Request.HTML implementation you'll see it has its evalScripts option defaulting to true, which later on during the call results in this bit of code being executed:
response.html = text.stripScripts(function(script){
response.javascript = script;
});
...
if (options.evalScripts) Browser.exec(response.javascript);
However this probably won't work on its own without Mootools' other bits of browser-abstracting code in place, so yes I'd definitely recommend including a library on your project.

issue calling JavaScript function from PHP

basically I am trying to call a javascript function from my PHP and I am using code I know works in other situations however here is it not and I am at a loss as to why?
It may be something stupid as I have been staring at this screen for a long time :)
here is where I call the function:
if(isset($test_details['done_test'])){
echo "getting here";
echo "<SCRIPT LANGUAGE='javascript'>user_error();</SCRIPT>";
}
I successfully get 'getting here' printed however it does not call the JS function.
javascript function:
function user_error(){
document.write("working");
//alert("User has already taken this test. Your are being redirected...");
//setTimeout("window.location='home_student.php'",3000);
}
The commented it what I do eventually want it to do.
Could anyone please shed some light.
Many thanks,
#Crimson - Here is what I tried after your advice...still no luck.
javascript now:
$(document).ready(function () {
var done = "<?= $test_details['done_test'] ?>";
if(typeof done != 'undefined'){
$('WORKING').appendTo('#bodyArea'); // just to test
}
});
By echoing <script>...</script> with PHP, you are not going to get the browser run the JS function!
PHP only outputs the HTML file that you want to send to the browser. The browser then parses this HTML and does a multitude of things before the page is displayed to the user.
Next, the user interacts with the displayed page (or some other browser related event like 'onload' happens) and the attached JS gets called.
So, if there is some JS that you want to run at a certain time, say immediately after the browser has finished loading the page, you need to create JS in the HTML file such that there is a JS function which gets called at the page load event like this:
<body onload="/*do something here*/"> ... </body>
It is better to use JQuery or some other JS frmework to accomplish something like this though.
Are you sure the function is already defined? Perhaps you declared your function after making the function call.
Additionally, although it's not really going to matter here, the proper way to have a javascript script tag is.
<script type="text/javascript"></script>
i.e. not language="javascript"
Replace the line you call the JS function with this:
echo '<script type="text/javascript">window.onload = user_error </script>' ;
It should solve the problem. Because at the time you are calling user_error(), the function may not have been initialized by the browser. So you'll get an error since the function could not be found.
If the function is placed in an external .js file, it's very likely that you get this error, since the external file usually takes a while to be loaded. If the function deceleration is in the same file but after where you are calling it, same thing happens.

jquery effects are NOT working

I have some experience in PHP but not in JQuery that much.
My admin.php page has a div which has id named "table1" where content gets loaded via ajax:
document.getElementById("table1").innerHTML=xmlhttp.responseText;
xmlhttp gets data from sorgula1.php page which has some JQuery effects like highlighting table rows. When I'm trying to run the sorgula1.php alone, the highlighting works but when it is loaded via ajax to admin.php, highlighting and other JQuery effects are not working. I've tried everything to make it work but, I always failed.
For those of you who will ask me to remove the $(document).ready(function() statement, I'm informing you that it doesn't work.
Here is the sorgula.php code: sorgula1.php
please be specific about the answers guys.Thanx for all answers.
Maybe when you use innerHTML property, the javascript doesn't work. I think you can solve this problem by using jQuery load() function.
The problem is most likely that the elements loaded via ajax do not have the effects applied to them - have you tried calling the $("#myTable").tablesorter(); javascript (again) after the ajax response has been received and injected into the DOM?
Edit sorry it should probably be this code that you call:
$("tr").not(':first').hover(
function () {
$(this).css("background","yellow");
},
function () {
$(this).css("background","");
}
);
or use .live()
Try to rewrite your events to Live(). I would say that it doesn't work, because elements are loaded after the jQuery functions are registered. So try
$("tr").not(':first').live('hover',function(){ // CODE });

jQuery eval of ajax inline script not throwing errors

Debugging Ajax code with Firebug
This question is quite similar, though old and without real answers.
I'm currently putting together an app that has scripts that get loaded in with an ajax request.
An example:
var main = _main.get();
main.load( someurl );
Where someurl is a page that contains an inline script element:
<script type="text/javascript">
$(document).ready( function(){
var activities = new activities();
activities.init();
});
</script>
jQuery will do a line by line eval of js that lives in inline script tags. The problem is, I get no errors or any information whatsoever in firebug when something goes awry.
Does anyone have a good solution for this? Or a better practice for loading pages which contain javascript functionality?
Edit: A little progress... so at the top of the page that is being loaded in via ajax, I have another script that was being included like this:
<script type="text/javascript" src="javascript/pages/activities.js"></script>
When I moved the inline $(document).ready() code in the page to the end of this included file, instead, syntax errors were now properly getting thrown.
As an aside, I threw a console.log() into the inline script tag, and it was being logged just fine. I also tried removing the $(document).ready() altogether, and also switching it out for a $(window).load() event. No difference. May have something to do with the inline scripts dependency on the included activities.js, I guess.
:: shakes head :: javascript can be a nightmare.
I suggest using http://api.jquery.com/jQuery.getScript/ for lazy loading Javascript or for getting html content http://api.jquery.com/load/
respectively:
$.getScript('path/to/file.js');
.. and
$('#content-wrap').load('path/to/html/file.html');
Both of theses methods are wraps around http://api.jquery.com/jQuery.ajax/ and have optional callback function parameters.

Categories