Javascript 如何将JS应用程序与PHP文件连接-找不到PHP文件

Javascript 如何将JS应用程序与PHP文件连接-找不到PHP文件,javascript,php,html,jquery,npm,Javascript,Php,Html,Jquery,Npm,我的JS应用程序有问题,我无法通过php文件从jquery发送数据,因为我有错误: POSThttps://magazyn.rob-tech.pl/savesettings.php 404 我通过PORT=8080 npm运行启动运行应用程序 package.json看起来像: { "name": "qr-code-scanner", "version": "1.0.0", "descr

我的JS应用程序有问题,我无法通过php文件从jquery发送数据,因为我有错误: POST
https://magazyn.rob-tech.pl/savesettings.php 404

我通过
PORT=8080 npm运行启动运行应用程序
package.json看起来像:

    {
  "name": "qr-code-scanner",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "start": "parcel index.html --open",
    "build": "parcel build index.html"
  },
  "dependencies": {
    "express": "^4.17.1",
    "mysql": "^2.18.1"
  },
  "devDependencies": {
    "@babel/core": "7.2.0",
    "parcel-bundler": "^1.6.1"
  },
  "keywords": []
}
我认为404意味着应用程序找不到文件(保存设置)。也许我应该在同一个端口上运行php文件

function saveUserTimes() {
        
    $.post("savesettings.php",
    {
        name: 'bosch',
        pracownik: 888,
        czynnosc: 'Pobranie',
        data: '',
    },
    function(data,status){
        document.getElementById("saveWarningText").innerHTML = data;
        $( "#saveWarningText" ).fadeIn(100);
        setTimeout(function(){ $( "#saveWarningText" ).fadeOut(100); }, 3000);
    });
    
    }
但我不知道我该怎么做, my index.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Zapisywanie</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
    <link rel="stylesheet" href="src/styles.css" />
    <script src="https://rawgit.com/sitepoint-editors/jsqrcode/master/src/qr_packed.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU=" crossorigin="anonymous"></script>
    <?php require_once 'savesettings.php'; ?>
  </head>

  <body>
    <div id="container">
      <h1>Magazyn rob-tech</h1>
      <div id='saveWarningText'></div>
      <p id="demo" font-color="white"></p> 
      <a id="btn-scan-qr">
        <img src="logo.jpg">
      <a/>
      

      <canvas hidden="" id="qr-canvas"></canvas>

      <div id="qr-result" hidden="">
        <b>Narzędzie:</b> <span id="outputData"></span><br>
        
        <form>
          <b>Numer pracownika&nbsp;&nbsp;</b><input type="number" id="nr_pracownika" min="1" max="999"><br>
          <input type="radio" id="pobranie" >
          <label>Pobranie</label><br>
          <input type="radio" id="oddanie" >
          <label>Oddanie</label><br>
          <button class="button" onclick="naSerwer()">Wyślij na serwer</button>
        </form>

      </div>
      <div id="qr-result" hidden="">
        
        
      </div>
    </div>
    <script>

      function saveUserTimes() {
    $.post("savesettings.php",
    {
        name: 'bosch',
        pracownik: 888,
        czynnosc: 'Pobranie',
        data: '',
    },
    function(data,status){
        document.getElementById("saveWarningText").innerHTML = data;
        $( "#saveWarningText" ).fadeIn(100);
        setTimeout(function(){ $( "#saveWarningText" ).fadeOut(100); }, 3000);
    });
    
    }
    </script>

    <script src="./src/qrCodeScanner.js"></script>
    <script>
      


    function naSerwer() {
      saveUserTimes();
  }
  
    </script>

  </body>
</html>

扎皮西瓦尼
马加津罗布科技公司

纳兹·扎伊:
普拉科尼卡数字
波布拉尼
奥达尼
韦利吉·纳塞尔 函数saveUserTimes(){ $.post(“savesettings.php”, { 名称:“博世”, pracownik:888, czynnosc:“Pobranie”, 数据:“”, }, 功能(数据、状态){ document.getElementById(“saveWarningText”).innerHTML=数据; $(#saveWarningText”).fadeIn(100); setTimeout(function(){$(“#saveWarningText”).fadeOut(100);},3000); }); } 函数naSerwer(){ saveUserTimes(); }
我的php文件:

<?php
$servername = "localhost";
$username = "admin_mag";
$password = "passowrd";
$dbname = "admin_magazyn";

$conn = new mysqli($servername, $username, $password, $dbname); // Create connection
if ($conn->connect_error) {     // Check connection
    die("Connection failed: " . $conn->connect_error);
} 

$narzedzie = mysqli_real_escape_string($conn, $_POST['narzedzie']);
$pracownik = mysqli_real_escape_string($conn, $_POST['pracownik']);
$czynnosc = mysqli_real_escape_string($conn, $_POST['czynnosc']);
$data = mysqli_real_escape_string($conn, $_POST['data']);

if (strlen($times) > 200000) {  $times = "";    }

$sql = "INSERT INTO narzedzia (narzedzie,pracownik,czynnosc,data)
VALUES ('makita', '123', 'pobranie', 'CURDATE()') ON DUPLICATE KEY UPDATE    
date=CURDATE()";

if ($conn->query($sql) === TRUE) {
    echo "Page saved!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

?>

若savesettings.php和index.html(带JS)在同一个目录下,那个么添加./

function saveUserTimes() {
        
    $.post("./savesettings.php",
:
:
:
否则,这里有一些提示,以防有帮助。。。 正如我所看到的,我们需要考虑.js文件的文件夹(其中.js文件包含Ajax代码)。 如果php代码在同一个文件夹中,我们可以使用

$.post("./savesettings.php",   //or
$.post("savesettings.php",
$.post("/savesettings.php",
$.post("../php/savesettings.php",
如果(使用绝对目录引用)php代码位于服务器根目录中,我们可以使用

$.post("./savesettings.php",   //or
$.post("savesettings.php",
$.post("/savesettings.php",
$.post("../php/savesettings.php",
或者(使用相对目录引用)如果.js在c:/somefolder1/js中 当.php在c:/somefolder1/php中时,我们可以使用

$.post("./savesettings.php",   //or
$.post("savesettings.php",
$.post("/savesettings.php",
$.post("../php/savesettings.php",

其中../表示我们要向上移动到父目录。'希望它能有所帮助

若savesettings.php和index.html(使用JS)在同一个目录下,那个么添加./

function saveUserTimes() {
        
    $.post("./savesettings.php",
:
:
:
否则,这里有一些提示,以防有帮助。。。 正如我所看到的,我们需要考虑.js文件的文件夹(其中.js文件包含Ajax代码)。 如果php代码在同一个文件夹中,我们可以使用

$.post("./savesettings.php",   //or
$.post("savesettings.php",
$.post("/savesettings.php",
$.post("../php/savesettings.php",
如果(使用绝对目录引用)php代码位于服务器根目录中,我们可以使用

$.post("./savesettings.php",   //or
$.post("savesettings.php",
$.post("/savesettings.php",
$.post("../php/savesettings.php",
或者(使用相对目录引用)如果.js在c:/somefolder1/js中 当.php在c:/somefolder1/php中时,我们可以使用

$.post("./savesettings.php",   //or
$.post("savesettings.php",
$.post("/savesettings.php",
$.post("../php/savesettings.php",

其中../表示我们要向上移动到父目录。'希望这对我有所帮助

如果我理解了你的问题,你正在尝试从你的ParcelJS服务代码向
发送
请求https://magazyn.rob-tech.pl/savesettings.php
。您是否尝试访问
https://magazyn.rob-tech.pl/savesettings.php
使用邮递员或类似工具?在代码中使用
$conn->error
是一个非常糟糕的主意,因为它可能会泄漏敏感信息。更多解释请参见本文:如果我理解您的问题,您正试图从您的ParcelJS服务代码向
发出
post
请求https://magazyn.rob-tech.pl/savesettings.php
。您是否尝试访问
https://magazyn.rob-tech.pl/savesettings.php
使用邮递员或类似工具?在代码中使用
$conn->error
是一个非常糟糕的主意,因为它可能会泄漏敏感信息。有关更多说明,请参阅本文: