1

I'm doing a chrome extension tutorial. The pop.html file has a POST form that connects to a php file called script.php. When I open script.php in the browser, the code executes an echo statement for testing purposes. However, when I run the chrome extension, I fill out the form and click the "submit" button, but instead of executing the code in script.php, it simply prints the code in script.php as plain text within the popup window.

Can anyone explain why the php code won't work for the extension? Throughout the day I've tried all the solutions described in the following posts: post1 post2 post3

Below are my files and code for reference.

In my MAMP directory, I have the following files in a project folder called "GlassExt":

manifest.json

{
  "manifest_version": 2,
  "name": "Glass - Connect to Database",
  "description": "test database functionality",
  "browser_action":{
   "default_title":"Hello, Yo!",
   "default_popup":"pop.html"
  },
  "version": "1",
  "permissions": [
 "http://localhost/*"
  ]
}

pop.html

(I previously had the "action" attribute set to "http://localhost/glassExt/script.php", but for some reason the form would not submit at all.)

<DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>This is a title, yo</title>
</head>

<body>
  <form action="script.php" method="POST">
    <input type="text" name="name" placeholder="Your Name"><br>
    <input type="text" name="email" placeholder="Your Email"><br>
    <input type="submit" value="Go">
  </form>
</body>
</html>

script.php

<?php

$test = "this is a test";
echo $test

?>
Brianna Singer
  • 119
  • 1
  • 1
  • 8
  • Chrome doesn't include PHP interpreter nor built-in web server. So submitting forms is useless and just reloads the page. Use JavaScript and normal buttons. – wOxxOm Aug 16 '17 at 03:45
  • The php script seems to work in the chrome extension tutorial here: https://www.youtube.com/watch?v=RXBxAlzmmgk. Do I need a different server other than MAMP? – Brianna Singer Aug 16 '17 at 03:49
  • Regardless of what it seems, Chrome doesn't have a web server not PHP interpreter. Note the chrome-extension:// protocol of the popup which you can see in devtools and how it's different from http:// served by an external server. – wOxxOm Aug 16 '17 at 03:53

1 Answers1

0

Chrome extension can't execute a php script. But you can try posting data to a server like following, but it will not be part of your chrome extension.

<form action="http://domain/script.php" method="POST">
    ...
</form>
nithinTa
  • 1,632
  • 2
  • 16
  • 32
  • Can you clarify what "doman" is here? Also, is there a way to connect a chrome extension to a database without php? – Brianna Singer Aug 16 '17 at 15:51
  • For that you have to host your php files in some server then you can post data to that server for eg: http://yourdomain.com/script.php, then you will be able to store data in that server but not in client machine. For local storage see https://developer.chrome.com/extensions/storage – nithinTa Aug 17 '17 at 04:25
  • It sounds like you are suggesting I post to a database using a php script. Did you not just explain that you can't use php with chrome extension? – Brianna Singer Aug 19 '17 at 03:09