Results 1 to 8 of 8

Thread: execute php thru javascript

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    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.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: execute php thru javascript

    I am executing php curl and get the data on button click

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    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

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: execute php thru javascript

    can I request for GET method?

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: execute php thru javascript

    Yes, you should just be able to change POST to GET and then just not send any data.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width