I am attempting to build a PHPMailer based email system for a basic website.
This is the line of code that is giving me trouble:
$xajax->printJavascript('xajax/');
Now, this is the tutorial I am using.
Regarding the above line of code, the tutorial says this:
How to use the code inside a webpage?
Place the form (variable), the function (and the includes) before of all html code. Next we need to include some JavaScript file in the documents HTML header (place also php tags):
$xajax->printJavascript('xajax/');
When I run all of the code (including: PHPMailer script; Ajax script), I get this error, on the aforementioned line of code.
Fatal error: Call to a member function on a non-object
So, my question is, do I need to in someway customize this code or make it run to a filepath of some ajax core file or something?
I would be willing to be $xajax is not defined. Try adding this after including the libraries but before including the call to that method:
$xajax = new xajax();
There may be some additional setup required by xajax. A more complete code example could help troubleshoot.
Related
I want to run code from awesomeQuery.php on multiple pages, so I use PHP require.
<?php require_once("awesomeQuery.php") ?>
The awesomeQuery.php looks something like this:
<?php
require 'vendor/autoload.php';
use Parse\ParseQuery;
[query code here that works]
?>
Now here's spiffyPage.php:
<?php
//This line here works beautifully!
require_once("awesomeQuery.php");
//If I make a new query code in this file I get the following error:
//Fatal error: Class 'ParseQuery' not found in /path/spiffyPage.php on line 45
[some other similar query code]
?>
Is there a reason why this new query isn't working? Didn't I already call use Parse\ParseQuery from awesomeQuery.php? Does it not carry over into spiffyPage.php?
If I call use Parse\ParseQuery separately in spiffyPage.php, then the new query code does work… but I'd rather not have to call it every time!
My guess: All the PHP code in awesomeQuery.php gets executed before being pasted into spiffyPage.php, so the code use Parse\ParseQuery doesn't get pasted into spiffyPage.php. Is this correct? If so, can I paste code into a file from another file without executing the code first?
the PHP manual clearly says that
Note:
Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.
http://php.net/manual/en/language.namespaces.importing.php
I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.
I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:
define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));
include(SBT_PLUGIN_URL . 'competition_table.php');
inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:
function add_table() {
//Run code here
}
add_shortcode('competition_table', 'add_table');
When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:
Call to undefined function add_shortcode()
However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.
So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?
Thanks in advance
You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".
Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:
include_once __DIR__ . '/competition_table.php';
Thanks to the feedback from #b__ I have managed to solve this issue.
For some reason, Magic Constants don't always work with wordpress, however, you can use it's equivalent to get the same effect:
include_once dirname(__FILE__) . '/competition_table.php';
When including files for use in a wordpress plugin you should always include via a PATH, not by a URL.
I have an app in Yii and i want to ajax form submition without inline script unlike this link said, because inline script is bad for SEO.
So i create php file and put my js+php code in it with header
Header("content-type: application/javascript");
And include it as Javascript file.
But java script return this error :
Fatal error: Class 'Yii' not found in
/var/www/mydomain/js/sendmessage.php on line 16
Which line 16 contains:
var sendurl = request->getUrl()
?>+"/handler";
What should i do to make it work ?
Your problem is that Yii is not yet initiated in your file.
You should probably make a JsController or something like that, and let it serve the js content for you instead of just making one external file like that. That will ensure that you have all the classes loaded that you want.
I mean, you wouldn't create a PHP file like that for anything else on your website right? So why do quickfixes just because it's js.
I am using Selenium version 1, with PHP. I need to execute a large Javascript code snippet. When I use the runScript() function, it gives me this error:
Request URI Too Large
How can I handle this? Or is it possible to include it as an external js file but run it as well at once? I have tried using the addScript() function, that contains my js code, but it did not work. I have also tried to include my all code in a function like this:
function executeMyCode() {
// all my code here
}
And then include it with addScript(), and then use runScript() like this: $this->selenium->runScript('executeMyCode()').
But it did not work either.
Thanks.
Hey everybody, this issue has had me stumped for the last week or so, here's the situation:
I've got a site hosted using GoDaddy hosting. The three files used in this issue are index.html , milktruck.js , and xml_http_request.php all hosted in the same directory.
The index.html file makes reference to the milktruck.js file with the following code:
<script type="text/javascript" src="milktruck.js"></script>
The milktruck.js file automatically fires when the site is opened. The xml_http_request.php has not fired at this point.
On line 79 out of 2000 I'm passing the variable "simple" to a function within the milktruck.js file with:
placem('p2','pp2', simple, window['lla0_2'],window['lla1_2'],window['lla2_2']);
"simple" was never initialized within the milktruck.js file. Instead I've included the following line of code in the xml_http_request.php file:
echo "<script> var simple = 'string o text'; </script>";
At this point I have not made any reference whatsoever to the xml_http_request.php file within the milktruck.js file. I don't reference that file until line 661 of the milktruck.js file with the following line of code:
xmlhttp.open('GET',"xml_http_request.php?pid="+pid+"&unLoader=true", false);
Everything compiles (I'm assuming because my game runs) , however the placem function doesn't run properly because the string 'string o text' never shows up.
If I was to comment out the line of code within the php file initializing "simple" and include the following line of code just before I call the function placem, everything works fine and the text shows up:
var simple = 'string o text';
Where do you think the problem is here? Do I need to call the php file before I try using the "simple" variable in the javascript file? How would I do that? Or is there something wrong with my code?
So, we meet again!
Buried in the question comments is the link to the actual Javascript file. It's 2,200 lines, 73kb, and poorly formatted. It's also derived from a demo for the Google Earth API.
As noted in both the comments here and in previous questions, you may be suffering from a fundamental misunderstanding about how PHP works, and how PHP interacts with Javascript.
Let's take a look at lines 62-67 of milktruck.js:
//experiment with php and javascript interaction
//'<?php $simpleString = "i hope this works"; ?>'
//var simple = "<?php echo $simpleString; ?>";
The reason this never worked is because files with the .js extension are not processed by PHP without doing some bizarre configuration changes on your server. Being on shared hosting, you won't be able to do that. Instead, you can rename the file with the .php extension. This will allow PHP to process the file, and allow the commands you entered to actually work.
You will need to make one more change to the file. At the very top, the very very top, before anything else, you will need the following line:
<?php header('Content-Type: text/javascript'); ?>
This command will tell the browser that the file being returned is Javascript. This is needed because PHP normally outputs HTML, not Javascript. Some browsers will not recognize the script if it isn't identified as Javascript.
Now that we've got that out of the way...
Instead I've included the following line of code in the xml_http_request.php file: <a script tag>
This is very unlikely to work. If it does work, it's probably by accident. We're not dealing with a normal ajax library here. We're dealing with some wacky thing created by the Google Earth folks a very, very long time ago.
Except for one or two in that entire monolithic chunk of code, there are no ajax requests that actually process the result. This means that it's unlikely that the script tag could be processed. Further, the one or two that do process the result actually treat it as XML and return a document. It's very unlikely that the script tag is processed there either.
This is going to explain why the variable never shows up reliably in Javascript.
If you need to return executable code from your ajax calls, and do so reliably, you'll want to adopt a mature, well-tested Javascript library like jQuery. Don't worry, you can mix and match the existing code and jQuery if you really wanted to. There's an API call just to load additional scripts. If you just wanted to return data, that's what JSON is for. You can have PHP code emit JSON and have jQuery fetch it. That's a heck of a lot faster, easier, and more convenient than your current unfortunate mess.
Oh, and get Firebug or use Chrome / Safari's dev tools, they will save you a great deal of Javascript pain.
However...
I'm going to be very frank here. This is bad code. This is horrible code. It's poorly formatted, the commenting is a joke, and there are roughly one point seven billion global variables. The code scares me. It scares me deeply. I would be hesitant to touch it with a ten foot pole.
I would not wish maintenance of this code on my worst enemy, and here you are, trying to do something odd with it.
I heartily encourage you to hone your skills on a codebase that is less archaic and obtuse than this one before returning to this project. Save your sanity, get out while you still can!
perhaps init your values like this:
window.simple = 'blah blah blah'
then pass window.simple
You could try the debugger to see what is going on, eg. FireBug