0

So I am trying to store the date inside a database and to do so I need to pass the variable 'date' to the PHP file store.pro.php however, I am not sure how to do this. I have tried Ajax but at the moment it doesn't seem to be working.

Javascipt code:

// variables for fetching current date
n = new Date();
y = n.getFullYear();
m = n.getMonth() + 1;
d = n.getDate();

// variables for displaying current date
let displayDay = 0;
let displayMonth = 0;

// If m or d are only one digit, add a leading 0 to the value
if (d < 10) {
  displayDay = '0' + d.toString();
} else {
  displayDay = d.toString();
}

if (m < 10) {
  displayMonth = '0' + m.toString();
} else {
  displayMonth = m.toString();
}

// storing the current date within raceDate
var date = displayDay + '/' + displayMonth + '/' + y;

$.ajax({
  url: "processes/store.pro.php",
  type: "POST",
  data: { x: date }
});

PHP code in store.pro.php

if (isset($_POST['x'])) {
  $raceDate = $_POST['x'];

  echo($raceDate);
} else {
  echo "no";
}
1
  • Not everything in programming is called "parsing". You meant "passing". (Duplicate question title filter?) Commented Feb 22, 2020 at 15:41

1 Answer 1

2

How do you know "it doesn't seem to be working" ?

add success method to your ajax, like this:

$.ajax({
  url: "processes/store.pro.php",
  type: "POST",
  data: { x: date },
  success: function(res) {
      res = JSON.parse(res);
      console.log(res);
  }
});

Then, in store.pro.php put this:

if (isset($_POST['x'])) {
  $raceDate = $_POST['x'];

  echo json_encode($raceDate);
} else {
  echo json_encode("no");
}
exit; // You may need remove this line, after you check, that ajax call is working

and check console in your browser

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry but I am pretty new to PHP and so im not quite sure how to check the console in my browser. However, I added your code and I'm still returned with 'no'. Do I need to do something specific to use Ajax?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.