jQuery .load() stopped working with POST parameter - php

So my website has been working fine the last year or so, and parts of it stopped working when I checked it today. This seemed strange to me since I hadn't changed any code.
After some investigation, I think I've pinned the problem down to the jQuery .load() function. I have something like the following in a file called "debug.php":
...
<div id="testd"></div>
<p id="mess"></p>
...
<script type="text/javascript">
$(document).ready(function() {
$("#testd").load("../scripts/debug_script.php", {varx: 10}, function() {
$("#mess").html("inside testd reached")
});
});
</script>
The file debug_script.php is simply:
<?php
$x= $_POST["varx"];
echo "<p>x is $x</p>";
?>
With the code above, in the file debug.php, nothing is loaded in the "testd" div, but the message in "mess" shows.
If I delete the {varx: 10} argument to .load(), the message in "testd" shows up fine (without the actual value of x, obviously).
So it looks like the POST argument in .load is breaking the function somehow. Even when I delete the line $x= $_POST["varx"]; in the php file being loaded and have just a simple echo, nothing is loaded when the POST argument is included in .load().
Does anyone have any ideas as to why this problem is occurring? Like I said earlier, these .load() calls were working fine on my site until recently, and I have not changed anything at all between when it was working and when it stopped working.
I am linking to jQuery 1.7.2 from Google API (http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js), and my host is 000webhost.com. Is it possible that the problem is related to the host? I don't have a local server installed, so all of my testing has been done live, through 000webhost.
Anyway, I would appreciate any ideas people might have. Just let me know if any more info from my end might be useful. Thanks!

Try sending varx variable name as a string, like this:
$("#testd").load("../scripts/debug_script.php", {'varx': 10}, function() {
$("#mess").html("inside testd reached")
});

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>

PHP include works but JQuery load doesn't

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.

AJAX/PHP – callback after finished loading data

(Not sure if I missed an already similar answered question…)
On click of a button, I'm loading various images from a database via PHP/MySQL and appending it to the body (the actual images are of course not stored in the database, the correct selection of the images is based on a posted variable).
My goal is to display a loading indicator after pressing the button and hiding the indicator after all the image data has completely loaded and displayed. This may be an easy to solve callback issue but I'm just getting started with AJAX. :)
The following is the code I currently managed to come up with. I'm guessing the load() function is not really the right one here?
Thanks for your help!
$("#somebutton").click(function(){
alert("fetching…");
$.post('loadmore.php', {
somevariable: somevariable
},
function(data){
$("body").append(data);
$(window).load(function(){
alert("finished loading…");
});
});
});
The function you have with the finished loading... alert is a success callback, so it gets executed once the AJAX call has finished. This means you don't need to use $(window).load.
Also, you can use the html method on an element to change its contents and display a message.
Something like this would work fine:
$("#somebutton").click(function(){
$('#divID').html('Loading...');
$.post('loadmore.php', {
somevariable: somevariable
},
function(data){
$("body").append(data);
$('#divID').html('');
});
});
Read the docs http://api.jquery.com/jQuery.ajax/
Use the success callback to append the body and then the complete and error callbacks to clear things up correctly.
$("#somebutton").click(function(){
alert("fetching…");
$.post('loadmore.php', {
somevariable: somevariable
})
.success(function(data){$("body").append(data)})
.error(function(){alert("oh dear")})
.complete(function(){alert("finished loading…")});
});
Remember to always have a fallback for removing the loader - nothing worse than just having a loader and no way to remove it from the page and continue using the application / web site.
I managed to solve my problem by reading and tweaking the code in the following article.
The function load() with the equation containing the self-explanatory variables [imagesLoaded >= imageCount] did the trick.
Know when images are done loading in AJAX response

Cannot get .ajax to call my php script

for the life of me I cannot get my PHP file to be called using the ajax function. Here is my relevant HTML:
$(document).ready( function(){
$("#generate").click(function(){
alert("Been clicked");
$.ajax({
url: "filterScriptForMe.php",
success: function(data){
alert("response");
}
});
});
});
Here is my PHP file, which I can run by calling separately, and will print to the error log/echo/create the necessary image no problem when run on it's own:
error_log("script has been called");
$hello = imagecreatefrompng("./images/stock.png");
$hello = imagecreatefrompng("./images/stock.png");
imagealphablending($hello,false);
imagesavealpha($hello,true);
$x=imagecolorallocatealpha($hello,0,0,0,127);
$color = imagecolorat($hello,350,500);
for($y=0;$y<512;$y++)
for($z=0;$z<597;$z++)
{
if($color!=imagecolorat($hello,$y,$z))
{
imagesetpixel($hello,$y,$z,$x);
}
}
imagepng($hello,"done.png");
echo "done";
I am beginning to wonder if there is a problem on my server at this point. When run it will print the first alert saying "been clicked" but it does not seem to be running the php script as I get no output to my log files when clicking the button from the html file, as opposed to visiting the php directly in the broswer. Both the html and php are in the same directory, and I have double checked the names to make sure they are correct.
I know other jQuery functions are working on the page as well, as I've been successful in getting some jQueryUI elements to display and respond to manipulation using jQuery alone. Added to that the first alert seems to be firing, so I'm fairly sure the library/code is in there. I've tried going through with firebug, but it's a little beyond me when it starts getting deep into the library and I lose track of what's being done and why.
Any help that you can provide would be much appreciated, at least to get the PHP file called from the html page. Thanks!

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.

Categories