-
Oct 28th, 2021, 10:03 PM
#1
Thread Starter
Member
execute php thru javascript
I searched the internet on how to execute php thru javascript and the answer I found is no you cant.
Is there a way to call php using javascript ? May I ask for a simple code for it ?
Basically I need to get the data from the php. Thanks in advance.
-
Oct 29th, 2021, 03:44 PM
#2
Re: execute php thru javascript
If you are getting data then you can submit an AJAX request. What is it that you want to do?
-
Oct 29th, 2021, 08:55 PM
#3
Thread Starter
Member
Re: execute php thru javascript
I am executing php curl and get the data on button click
-
Oct 29th, 2021, 09:01 PM
#4
Re: execute php thru javascript
cURL requests are inherently HTTP requests. It can easily be converted to an AJAX request.
Could you show the request?
-
Oct 29th, 2021, 09:04 PM
#5
Thread Starter
Member
Re: execute php thru javascript
i cant type the whole this because im on mobile. but for purposes of example, just any curl request string
-
Oct 29th, 2021, 09:28 PM
#6
Re: execute php thru javascript
Assuming it is a POST request, it would look something like this:
Code:
// this is the data you wish to send
const data = JSON.stringify({ someProperty: 'some value' });
// this is the AJAX request object
const xhr = new XMLHttpRequest();
// this is the event listener to check when the readystate property has changed
xhr.addEventListener('readystatechange', function () {
// check if the readstate property is done
if (this.readyState === this.DONE) {
// if so, then do something with the response
console.log(this.responseText);
}
});
// start the POST request to the endpoint
xhr.open('POST', 'https://www.website-name.com/endpoint');
// indicate that we are sending JSON
xhr.setRequestHeader('Content-Type', 'application/json');
// GO! GO! GO!
xhr.send(data);
Last edited by dday9; Oct 29th, 2021 at 09:33 PM.
-
Oct 30th, 2021, 12:49 AM
#7
Thread Starter
Member
Re: execute php thru javascript
can I request for GET method?
-
Oct 30th, 2021, 11:37 AM
#8
Re: execute php thru javascript
Yes, you should just be able to change POST to GET and then just not send any data.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|