0

I am trying to make a Chrome extension. Currently I have an AWS database running on MySQL workbench and I am trying to get it connected to the extension. My PHP file is simply just trying to connect to the database on the submit of an HTML form. I am following the videos (parts 1-4) form this link https://www.youtube.com/watch?v=dpLbADRh830

Right now I am using localhost as my server and on submit of the form, my extension says "This site can’t be reached localhost refused to connect." I have no clue where to go from here and this is honestly my first database experience so I apologize if this is a silly problem.

<?php
    $name=$_POST['name'];
    $email=$_POST['email'];
    if(!isset($_POST['submit'])){
        echo $email;
    }
?>

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form action="http://localhost/db/tester_script.php" method="POST">
            <input type="text" name="name" placeholder="name"><br>
            <input type="text" name="email" placeholder="email"><br>
            <input type="submit" value="submit">

        </form>

    </body>

</html>

Here is how I fixed the solution:

    <body>
    <form id="thing">
        <input type="text" id="name" name="name" placeholder="name"><br>
        <input type="text" id="email" name="email" placeholder="email"> 
        <input type="submit" id="submit" value="submit">
    </form>
<script
    src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
<script>
$("#thing").on("submit", function(){
    var name = $("#name").val();
    var email = $("#email").val();
    $.ajax({
        type: "POST",
        url: "http://localhost:8000/tester_script.php",
        data: {name : name, email : email},
        success : function(data){
            console.log(data);
        }
        });
    return false;
    })
    </script>
</body>
hardScrabble
  • 261
  • 2
  • 6
  • You should first check the security group of your AWS database to make sure that your IP address is not being blocked – Jamie_D Nov 10 '18 at 13:59
  • Thanks Jamie. I did do that and it's not. – hardScrabble Nov 10 '18 at 14:20
  • Are you seeing any error messages in the development console other than the one you describe? – Jamie_D Nov 10 '18 at 15:15
  • No, the console stays clean. When I open the script.php file in the browser with local host, it registers so I know local host is running fine. The only error is the one that pops up when I try to submit the form in the chrome extension. Sorry this doesn't help much. I just have no clue what is going wrong. I followed the video tutorial exactly. – hardScrabble Nov 10 '18 at 15:43
  • Can you post any source code? It may help in determining the issue .... (Do not reveal any connection usernames/passwords) – Jamie_D Nov 10 '18 at 16:16
  • I made a small tester that follows the tutorial that I provided the link above except I didn't go as far as to push to the database. I am simply now just trying to send the HTML form input the the PHP script and have it echo out the variables and I still get the localhost error. Code in edit above. – hardScrabble Nov 10 '18 at 17:09
  • Are you sure the form's action is correct? I assume `http://localhost` shows you're index page ? – Jamie_D Nov 10 '18 at 18:31
  • I guess I am not sure what else it should be. The tutorial did this same thing. Should it just be /db/tester_script.php? I actually just tested with this and it just spits out the PHP script content. – hardScrabble Nov 10 '18 at 18:44
  • It looks like you have not set up you're server environment properly. – Jamie_D Nov 10 '18 at 18:52
  • I go to the directory of my PHP script and run "php -S localhost:8000" If I go to "localhost:8000/tester_script.php" it will print any simple echo statements that don't have variables. When I try to run it in the extension with variables it doesn't work. – hardScrabble Nov 10 '18 at 19:03
  • Change the action url to `http://localhost:8080/db/tester_script.php` – Jamie_D Nov 10 '18 at 19:37
  • My mistake: Should be: `http://localhost:8000/db/tester_script.php` – Jamie_D Nov 10 '18 at 19:54
  • Remember that you need to run `php -S localhost:8000` after every reboot – Jamie_D Nov 10 '18 at 19:56
  • Your file was not found It may have been moved or deleted. ERR_FILE_NOT_FOUND This is the error when I change the action. I test the script file in local host before trying it in the extension. I can get it to echo when I just run localhost:8000/db/tester_script.php. But then the chrome extension still errors – hardScrabble Nov 10 '18 at 21:07
  • Perhaps this may help: [link]https://stackoverflow.com/questions/7668502/cant-connect-to-localhost-from-chrome-extension – Jamie_D Nov 11 '18 at 04:21
  • Jamie_D Thank you so much for constant help. I had tried the link you provided. Here was my issue: I had to send the html form data as a JSON object rather than as variables from the form. So my PHP script only takes the form data from the JSON post object. Also, I added ajax post request and had to format it correctly to get the post to work. I added some example code above for anyone else that may run into this issue. – hardScrabble Nov 17 '18 at 04:57

0 Answers0