Yii Ajax form submition without inline script - php

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.

Related

DOMDocument->loadHTMLFile() I/O warning : failed to load external entity, only fails when called by ajax

I have a function which creates a new instance of a form template I've made (not using a template engine) and fills it in with various data based on an object thats passed to it. I use this function to create forms for existing objects when the page is loaded, but I also have a button in the interface which makes an ajax request to a separate script which creates a new object and then calls that function to create a form for the object to be sent as a response to the ajax.
The error is:
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "templates/form_edit_event.html" in C:\wamp64\www\<private>\src\<private>\components\component_edit_event.php on line 12
The code it references:
11 $eventDom = new DOMDocument();
12 $eventDom->loadHTMLFile('templates/form_edit_event.html');
The thing is though, when the page is first loaded, my script and the loadHTMLFile function work just fine to create the forms for the already existing objects. The I/O error only occurs when I use the button to try to make a new one. The exact text of which is as follows, although I obscured some directory names that shouldn't matter:
The files it's loading is pure html, not even a header/footer/etc, just <form>contents</form>. I have no idea why it would fail only sometimes.
The entire script (minus some irrelevent use/includes) being called from ajax is this, with component_edit_event.php being the script mentioned above which creates the form:
include_once __DIR__ . '\..\components\component_edit_event.php';
$tourneyId = $_POST['tourneyId'];
/** #var DimTournament $tourney */
$tourney = DimTournament::findById($tourneyId);
$newEvent = new DimEvent($tourney);
echo createEventForm($newEvent)->saveHTML();
I don't think I should need to include any other code but I absolutely can if needed.
Swapping the relative path in $eventDom->loadHTMLFile('templates/form_edit_event.html'); to an absolute path fixed the issue. The script that was throwing the error was in a different directory from the script which was working fine.
I'll be making a config file with some sort of $srcRoot variable for the sake of keeping some semblance of pseudo-relativity in the path names.

Load and Execute an javascript function in an External JS file using Node.js

I'd like to load and execute a Javascript function in external Javascript file using Node.js and similarly retrieve back with desired result after execution. Is it possible/not possible? If not, any other alternatives.
Any help regarding this is greatly appreciated.
Yes, you can load another JS file as a module using Node.js module system and function require. There's quite good description of how to do it in Node.js documentation, take a look int this page.
Basically you have to load your external file calling function require and passing path to that file:
var externalFunction = require('./path/to/external/file')`;
but to make this work you have to export that function by writing something like
module.exports = functionToExport;
in your external file, where functionToExport is the name of the function which you want to use.

jQuery load php file as text?

Is it possible to load a php file as text with jquery?
$('#loader').load('somefile.php', function(e){
console.log(e);
});
This always interprets/execute the php file but I'm looking for a way to only load it as text, without to resort to renaming my php file as .txt
Is it possible?
Cheers
It is not possible without making any server side modification. The web server will always interpret the php file and return the output. However does not matter what solution you find it'll be very dangereous since you'll be dumping content of your php file to public.
Possible solutions with server side modifications:
Create a PHP file that dumps the content of a file, which name is specified by a url argument
Rename the file (I know the op does not want this, just included since it's an option)
As #nicholas-young suggested, get rid of the PHP tags.
I'm not sure why you need this type of need but I want to emphasize that this might not be a good idea in most of the cases since you'll be make a working PHP file available to public. If you can explain more why you need this we might offer better solutions.
Update:
Create a dumper.php that requires authorization and call this file from the javascript side with passing the filename that you want to be dumped as a parameter (dumper.php?file=index.php)
echo file_get_contents($_GET['file']);
It is of course not possibile.
.load will make an HTTP request to yourwebsite.com/somefile.php hence you will obtain the result of your script not the PHP code inside it.
If you really need the raw code inside your javascript as a string you should output it from the php itself:
<script>
var yourCode = <?=json_encode(file_get_contents('somefile.php')) ?>;
</script>
NO! Would be a major security problem if possible. The header will not matter. If making request towards php file, it will execute prior to delivery.
Use some parameter to print out contents from file instead. But do it in the file itself.

Requesting assistance with 1 line of code regarding xajax

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.

Problem with AJAX and PHP

I have a small problem, I want to load data from a PHP file and put them on a DIV.
Here's the Jquery code
// Store the username in a variable
var jq_username = $("#txt_checkuser").val();
// Prepare the link variable
var link = 'user.php?action=check&username=' + jq_username;
$('div #checkuser_hint').load(link);
So it works! but instead of loading the result (compiled PHP) it loads the PHP code.
If I write the long URL "http://localhost/project..." it doesn't load anything!
Any idea how to do that?
I think you might be accessing your javascript file as a file on your local filesystem, a request to the same directory would go through the filesystem and not through your webserver, processing the PHP into the desired output. This also explains why http://localhost/project for the AJAX call doesn't work: Javascript might be enforcing the same-origin policy on you.
Verify that you're actually accessing this javascript file through http://localhost/ (as opposed to something like file://C:/My PHP Files/ ).
Does the page return anything when you use your browser?
Are you sure it should not be 'div#checkuser_hint' instead of 'div #checkuser_hint' ?
And this looks like the correct way according to the documentation.
var link = 'user.php';
$('div#checkuser_hint').load(link, {'action':'check', 'username':jq_username});
Are you able to access the script manually on your own? (try accessing it via your browser: htp://localhost/...) It may be the case that you're missing your opening <?php and/or closing ?> in the script-file itself.

Categories