I have a js script that uses ajax to post data to a php script. The php script will open a text file and write the data that's passed from the js script. The same script works on chrome on a different machine but after I migrate it to the server where only IE is available, it does not work.
After executing the POST script in js, my browser shows the success message but the data in the text file is not changed. One problem I can think of is the directory I put in my php script is not correct. So I added these lines from w3schools to see if the file is actually found, but nothing peculiar happened:
if(!file_exists("welcome.txt")) {
die("File not found");
} else {
$file=fopen("welcome.txt","r");
}
What are some other ways I can do for debugging in this case?
UPDATE
I think my php in general is not working for some reasons. I have replaced everything in my php file with
echo '<script type="text/javascript">alert("hello"); </script>';
I am assuming when I click the button on my webpage, the js will do a $ajax post and the php script will be executed showing the alert message. However nothing yet shows up
In a PHP script you can turn error reporting on with:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Error and warnings will show up that will give you possibly a hint where your script is failing.
Related
When I call a PHP function from a javascript function (located in an .js external file), the webpage refreshes and appears to stop loading at the PHP function.
function completePurchase(){
//check if perosn signed in
if (signedIn){
alert("Purchase Complete! Total : " + total);
//Upadte database - call PHP function in MainPage2
document.write('<?php echo upadtePurchaseToDB();?>');
}
}
The PHP function resides in the PHP page enclosed within PHP tags:
//PHP to handle data when purchase button is pressed
function upadtePurchaseToDB(){
echo "PHP CALLAED AS PURCHASE BUTTON PRESSED";
};
The idea is to update a database when the PHP function is called.
However when called the page refreshes and remains blank.
In the console if i navigate to the 'Element' tab I can see the PHP call:
Not sure what the underlying issue is wether its how I have called the PHP function using JS or where the PHP function is placed.
One problem is that you're trying to make JS genarate PHP code, and then expecting that to work: it can't. PHP runs on the server, and only "does things" when asked for a page by the browser: PHP generates source code for the browser to deal with. That's all it does.
When the browser has the page, JS kicks in, and PHP is no longer anywhere to be found: see Difference between Javascript and PHP for more information on this, and I can strongly recommend reading up on that.
However, the real problem that you're describing (your page seemingly reloading but dying on the PHP code) is one caused by your use of document.write(), which absolute doesn't do what you think it does, and you should not be using it in modern code.
document.write is one of the earliest functions in JS and is super low level: it does not write text into a webpage, it writes bytes into the document bytestream so that the document parser can pick up data from the bytestream at the same time and parse it into a page tree.
While the page is still being processed, that seems safe: document.write will simply inject bytes into the open bytestream, and the parser will parse that, and all will seem well. However, once the page tree is done and the document bytestream gets closed, things start to go very, very wrong:
Any call to document.write will try to write into the open bytestream, and if it's closed, document.write will simply open a bytestream: now you have an empty document bytestream. And because the document parser sees an open bytestream, it start building a page tree based on what's in it, and now your page is gone because that's what you told the browser to do.
It's also not the case that it "appears to stop loading at the PHP function", what actually happens is that you've told the page parser that the new page code to form a page tree off of is the byte sequence <?php echo upadtePurchaseToDB();?>. So the parser looks at that, sees <, and so knows that what comes after that will be a tag, because that's how HTML works. It then sees ? and goes "Error: this HTML is invalid HTML" and stops.
So the bottom line here is to read up on where and when PHP runs, vs where and when JS runs, but arguably even more importantly: never use document.write because it doesn't do what you think, at all.
I am writing some PHP code, I tried to run JS in my php with exec() function, but after I run on web browser JS came out with error, what should I do to fix this problem?
php
//after run below code, Javascript runtime error message shown
$response = exec("D:\xampp\htdocs\lazada.js");
echo $response;
I expected the JS could be run successfully after i run my php code on web browser
Load the file content in the variable and print that variable in the HTML.
<?php $jsContent = file_get_contents("D:\xampp\htdocs\lazada.js");?>
<script><?php echo $jsContent;?></script>
I am trying to start a javscript annimation and then submit a form. I have this but it's not working... I understand php is server side and javascript is client side; however, this works with alerts as long as I get rid of the header so I am confused why it wont work with my annimation.
if (isset($_POST['play'])) {
if ($mSecs >= .1) {
echo "<script>
spinWheel_1.startAnimation();
</script>";
}
sleep(5);
header("Location: winwheel.php");
}
You instruct the browser to immediately navigate away by sending the header. This is the reason why you see your animation without the header in the script.
Also, note that you cannot send the header after sending any output as a response.
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
I'm busy doing server side and client side validation for magento. this validation works fine on server side (php)
on the client side i am using javasrcript.
When i started on this. i had my javascript embedded on a phtml file and everything was working as expected.
because i am using magento so I decided to inject the javascript file via page.xml
When I added the javascript code instead of getting the message pulled I get the php as is.
Here is my javascript:
function DefaultAddressErrorChangeNotAllowedMessage() {
alert("<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>");
return;
}
I run this when a user hit the onclick it will point to this function DefaultAddressErrorChangeNotAllowedMessage()
and the
<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>
will be populated as is.
but when I embed this directly to a phtml file it pull the correct message.
I there a way for javasrcipt that I can use to escape the php and get the correct message which is pulled from config.xml
PHP is rendered server side only. If you need to "inject" PHP specific values to your javascript, then you either need to render the actual value as part of the output of the php script, or you need to take a new roundtrip to the server, using Ajax.
Javascript is clientside, PHP is server side, so all php has been evaluated when javascript is loaded. This means, you can alert php echos, but you can't run PHP operations or any PHP logic in Javascript. You need ajax for this.
sorry for my clumsy answer, but maybe you lost the simple things.
I see that your javascript contains php tag, so i think you should insert your javascript code into .php extension because .js extension can't recognise the php tag.
I am building a WordPress plugin. The plugin works beautifully on one site.
But on every other site I've tried so far one of the resource files used it getting a 404 (Not Found) error in the console. If I post the url, displayed in the console as not found, in the address bar the file is accessible.
The site it works on is not in a root directory, while one it doesn't work on is on the same server, in the root directory and is a multisite installation. I've tried it on other servers, and sometimes it works, on others it doesn't'. I'm baffled by what might cause this... Any feedback would be appreciated.
The following code is posted using a action into the header of a page:
echo '<script type="text/javascript">
(function($){
$(document).ready(function(){
lpStart();
});
var lpOnComplete = function(response) {
// do more processing
lpStart();
};
function lpStart() {
$.post("'.plugin_dir_url(__FILE__).'filename", {varname: "'.$value.'"}, lpOnComplete, "json");
};
})(jQuery);
</script>';
The URL that is said to not exist is the one that is posted to in function lpStart().
Interestingly, when it doesn't work I get the following error in the console as the page loads.
GET http://example.com/undefined 404 (Not Found)
Thank you in advance.
I suspect the value of __FILE__ is not that which you expect, as an example run the line "echo __FILE__;" here, you'll notice that the result is not www.site.com/homepage/... but rather /homepage/... This might not be the problem but it's where to start.
Second port of call, hopefully where the problem lies, is where the GET file is located. Say we have a file system structure of
home
ajaxStuff/
example.php
main/
index.php
and our site entry point is /main/
in order to call the ajaxStuff example the relative url must tell the server to step up a branch in the directory before trying to enter ajaxStuff and indeed run example.php by writing something like
url: "../ajaxStuff/example.php"
And if all else fails - are you porting over from a server which doesn't use a case sensitive file system (windows) to one that does, if so check the string case for each file.