简体   繁体   中英

Calling a php file via Javascript

I currently have this code below working A-OK, it basically loads up a pretty notification box with a message on a onClick event, so when a user presses the button then the notification pops :

<button id="buttonsuccess" class="btn btn-success" onClick="FunctionSuccess()">button</button>

<script>
function FunctionSuccess(){
Lobibox.notify('success', {
iconSource: 'fontAwesome',
size: 'mini',
delay: 8000,
sound: false,
title: 'Success!',
msg: 'Success message here.'
});
}
</script>

However, I would like to run a PHP file on the onClick event. My code to run the PHP file within the Javascript code is as follows:

<button id="buttonsuccess" class="btn btn-success" onClick="FunctionSuccess()">button</button>

    <script>
    function FunctionSuccess(){

$.ajax({
    url: '/copy/test.php'
})

Lobibox.notify('success', {
iconSource: 'fontAwesome',
size: 'mini',
delay: 8000,
sound: false,
title: 'Success!',
msg: 'Success message here.'
});
}
</script>

But this doesn't work. I actually get the notification box message as I did before, but the PHP code does not run. Instead, I get the following error on console:

GET http://domain.com/copy/test.php 500 (Internal Server Error)

I just need to execute the code within the test.php file, I don't need to get data from it or post data to it, just execute the PHP file. BTW, I've tested the PHP file, it works fine.

error_log shows no errors, no PHP errors either. The permissions on the php file is also OK.

Much appreciate any insight you may be able to provide.

Thanks.

(Assuming you're using chrome..)

Check your console, and enable 'LogXMLHTTPRequests' if it isn't checked.

When you click the button, you should see the url being called in the console. Right click on that, and open in a new window.

Chances are, the wrong domain is coming up, or there's an error in the php code.

You can also open the console and select the network tab. If you have a request that generated a 500, it'll appear red, you can right click on that line, select 'copy response' and paste that into your favorite text editor.

I am sorry I could not put this in a comment. Please try to put url in double quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM