0

Hi I am trying to save some data, when a user reaches the order confirmation page, to a newly created table in the database and I am stuck. I know the AJAX call is communicating with the php file since I can simply console.log the response(output) and I get what is expected (Ie.: echo 'hello') from the php file - so all good here.

I also registered the new table alias inside the init.php.

Here is the JS code from the file order_message.tpl:

var newData = {
   "one":"test one",
   "two":"test two"
};

$.ajax({ url: '/newDir/newFile.php',
    data: {
        dataToSend: newData
    },
    type: 'post',
    success: function(output){
        console.log(output);
    },
    error: function(xhr, textStatus, error){
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
    }
});

Here is the PHP code from the file root/newDir/newFile.php:

require '../init.php';
require '../include/func/func.db.php';

if(isset($_POST['dataToSend']) && !empty($_POST['dataToSend'])) {
   $ssData = json_decode($_POST['dataToSend']);
   myFunc($ssData);
} else {
   die( header('Location: ../404.html') );
}

function myFunc($dt){
   $toSaveData = array(
      'one' => $dt->one,
      'two' => $dt->two
   );
   func_array2insert ('my_new_table', $toSaveData);
}

I can see the AJAX call being made in the dev tools, but its outputting these errors:

I did not reference anywhere in the code this path - which does not exist - https://localhost/site/newDir/index.php, so I'm not sure what is happening here. The next 3 errors are coming from the AJAX error method. And in the database no data is being stored.

If I comment out "require '../init.php';" I get no errors, but instead get a long string of html code. But still no data saved to the database.

Anyone can see what the issue may be?

Thanks a lot.

Sergio
  • 792
  • 3
  • 10
  • 35

1 Answers1

1

1) Move the newFile.php script to the same folder where the stock cart.php script is located.

2) Apply the patch to the PHP file

--- old.php 2019-03-14 15:19:10.162222071 +0400
+++ new.php 2019-03-14 15:25:41.160826722 +0400
@@ -1,5 +1,11 @@
-require '../init.php';
-require '../include/func/func.db.php';
+require __DIR__.'/top.inc.php';
+
+define('QUICK_START', true);
+define('SKIP_CHECK_REQUIREMENTS.PHP', true);
+define('USE_SIMPLE_SESSION_INTERFACE', true);
+define('USE_SIMPLE_DB_INTERFACE', true);
+
+require __DIR__.'/init.php';

 if(isset($_POST['dataToSend']) && !empty($_POST['dataToSend'])) {
    $ssData = json_decode($_POST['dataToSend']);

or download the new version here https://bt.x-cart.com/view.php?id=50556#attachments

3) Change the URL in the line

$.ajax({ url: '/newDir/newFile.php',

to the new one. Use relative or absolute path.

Ildar Amankulov
  • 526
  • 4
  • 19
  • Thank you Ildar. Can you tell me what the `---`, `+++`, `-` and the `+` at the beginning of the lines are meant to be. If I leave this code as is, I get a syntax error right on the first`-`. – Sergio Mar 14 '19 at 14:15
  • Ildar, why would I need to patch it if it is a brand new file? Sorry, this is something I never done before, so I am a little confused on the process. – Sergio Mar 15 '19 at 12:29
  • I have uploaded the newFile.php file here https://bt.x-cart.com/view.php?id=50556#attachments – Ildar Amankulov Mar 15 '19 at 13:22