1

I have problem with my JS APP, I can't send data from jquery via php file, because I have error: POST https://magazyn.rob-tech.pl/savesettings.php 404

I run app via PORT=8080 npm run start and package.json looks like:

    {
  "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": []
}

I think that 404 means that APP can't find file (savesettings). Maybe I should run php file somehow on the same port?

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);
    });
    
    }

but I don't know, how should I do it, 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>

My php file:

<?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();

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Grzegorz07
  • 11
  • 1
  • 1
    If I understand your question, you are trying to make a `POST` request from your ParcelJS served codes to `https://magazyn.rob-tech.pl/savesettings.php`. Have you tried accessing `https://magazyn.rob-tech.pl/savesettings.php` using Postman or similar tools? – J.L Jul 05 '20 at 09:18
  • 1
    It is a very bad idea to use `$conn->error` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jul 05 '20 at 18:22

1 Answers1

0

perhaps if savesettings.php is on same directory as index.html (with the JS) then add ./ to make it

function saveUserTimes() {
        
    $.post("./savesettings.php",
:
:
:

Otherwise, here's some tips in case it helps... as I see it we need to consider the folder with the .js file (where .js file contains the AJAX code).

if php code is in the same folder, we can use

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

if (with absolute dir reference) php code is in server root directory, we can use

$.post("/savesettings.php",

or (with relative dir reference) if e.g. .js is in c:/somefolder1/js while .php is in c:/somefolder1/php, we can use

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

where ../ means we want to move up to parent directory . ' hope it helps

Ruperto
  • 326
  • 2
  • 8