简体   繁体   中英

How to get data from '$.post' in a localhost php file?

I'm building an extension that extracts DOMs from a website(not mine) and automates a button click with filling some inputs.

I made a local database which the extension will be extracting values from to fill the inputs, I could successfuly done that with xmlhttprequest that reads my php file from my content-script js file.

Now I want to send my php file that the button was clicked so it updates the database with new values. I tried $.post() but I can't get it with $_POST[];

content-script.js

setTimeout(
      function(){
        var finsih_div_found = $(dialog_div_found).find("div").get(12);
        var finish_button_found = $(finsih_div_found).find("button").get(2);
        finish_button_found.dispatchEvent(new Event('click', {bubbles: true}))

        $.post('http://localhost:8012/extension-Oasis/php/getIntervenant.php', {button: 'Clicked'}, function(e){
            console.log("posted");
        });
},2000);

php file

$status = $_POST["button"]; //Gives an error of 'Undefined index: button'.

Please note that the website is not mine I don't have nor the back end nor the front end nor its API's. I just want to automate a button click that's done regularly.

Have you called your PHP file directly? Then it is a HTTP GET request and therefore $_POST['button'] throws and 'Undefined index: button' error. In other words, you can only access the button value via $_POST[] if you use your jQuery $.post to do so.

   $.post('http://localhost:8012/extension-Oasis/php/getIntervenant.php', {button: 'Clicked'}, function(e){
        console.log(e); // log everything that is echoed in PHP file to browser's console
    });

In your PHP file, add a echo $status; in the last line.

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