0

I am trying to build a Chrome plugin. In the main folder, I have a popup.html which runs by default and uses the following syntax in manifest.json

"browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },

the popup.html is working absolutely fine

what is my popup.html is doing?

It is inputting email from the user and storing it in local phpmyadmin. Following is the code of popup.html

<!doctype html>
<html style="min-width:350px;">
  <head>
    <title>Welcome</title>   
  </head>
  <body>
<h3> Enter email </h3>

<form action=”info.php” method=”post”>
Enter email: <input type=”email” name=”email” />
<input type="submit" value="Submit" >
</form>

  </body>
</html>

The form action is linked to info.php where the php connects the database and inserts the data into the table in phpMyAdmin. Following is the info.php code

<html>

<body>

<?php 
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
    echo'Could not connect to the server';
}
if (!mysqli_select_db($con,'test'))
{
    echo 'Database Not Selected';
}
$Email = $_POST[email];

$sql = "INSERT INTO test_table(Email) VALUES ('$Email')";
if(!mysqli_query($con,$sql))
{
    echo 'Could not add to database';
}

else
{
    echo 'Thank you the data is added';
}

header("refresh:2; url=popup.html");
?>
</body>

</html>

What problem am I facing? After I enter the email in the input field it gives an error that Your file was not found It may have been moved or deleted. ERR_FILE_NOT_FOUND

Maybe I am getting this error because info.php has to added in the manifest file? If this is the problem then how can I add multiple urls in the manifest.json file?

Tamanbir
  • 76
  • 7
  • The browsers don't have a built-in PHP engine so linking to a `.php` file without a full URL simply open the file as a local html page inside the extension directory. You probably want to use `action="http://localhost/info.php"`. – wOxxOm Nov 19 '19 at 12:51
  • @wOxxOm info.html is in the same folder as that of popup.html still i would need to do this? – Tamanbir Nov 19 '19 at 12:54
  • The browsers don't have a built-in PHP engine so it doesn't matter how you name the file or where you put it inside your extension directory. If you want PHP being processed you need a separate server. – wOxxOm Nov 19 '19 at 13:30
  • @wOxxOm so what if i host info.php online on free web hosting websites? would it work? what do you suggest – Tamanbir Nov 19 '19 at 13:33
  • Well, go ahead. – wOxxOm Nov 19 '19 at 13:34
  • On the other hand, this is not how extensions are usually implemented. A proper solution is to use an API via `fetch()` or `XMLHttpRequest()`. – wOxxOm Nov 19 '19 at 13:35
  • @wOxxOm i am a beginner to this. Could you please elaborate – Tamanbir Nov 19 '19 at 13:36
  • That's a broad topic. Find examples and modify them to your needs. – wOxxOm Nov 19 '19 at 13:36

2 Answers2

0

Your header won't work, since you have already echoed output.

Instead of outputting immediately, stick the output into an $html variable.

This might not completely fix your issue? But it will fix the header.

<?php 

$html = '';

$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
    $html .= 'Could not connect to the server';
}
if (!mysqli_select_db($con,'test'))
{
    $html .= 'Database Not Selected';
}
$Email = $_POST[email];

$sql = "INSERT INTO test_table(Email) VALUES ('$Email')";
if(!mysqli_query($con,$sql))
{
    $html .= 'Could not add to database';
}

if (empty($html)) {
    header("refresh:2; url=popup.html");
} 

echo $html;
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • 1
    i thought you might, since no mention of that error appears anywhere in your code. However your header issue is now fixed! Search your code for the error message – delboy1978uk Nov 19 '19 at 13:05
  • Also, delete the else section 'the data was added'. We dont want output on success, just the header call. Updating my answer – delboy1978uk Nov 19 '19 at 13:06
  • The browsers don't have a built-in PHP engine so the contents of the file is interpreted as a normal HTML, all PHP instructions are ignored. – wOxxOm Nov 19 '19 at 13:32
  • you need to attach your PHP to your web server, if not already, and not open the files directly – delboy1978uk Nov 20 '19 at 12:48
0

with @wOxxOm help the problem was solved. In the popup.html instead of giving info.html directly, it would have been through the server such as http://localhost/foldername/info.php

Tamanbir
  • 76
  • 7