1

I hope I am able to explain what I'm trying to do properly, but I am trying to use AJAX to pass (using POST) quite a number of Dates to PHP file, to do checks if dates are valid (e.g. 31-FEB, 31-Apr). I am not sure how many dates there will be, there are 12 entry boxes, maximum case 12 dates, minimum case 1. So the number of dates passed to the PHP file could be anything. I want to be able to "roll" through (using numbers 0 1 2 like in an array) all the dates (and their D, M, Y) inside the PHP file, and also "roll" through dates passed back to the JS from PHP.

Jquery

var DAT = {};  //is such a declaration valid for an array?
k=0;
for(x=1;x<=12;x++)
    {   I = "#ED" + x;      
        d= $(I).children('.dd').val();
        m= $(I).children('.mmm').val();
        y= $(I).children('.yyyy').val();
        c= date_chk(d,m,y);                //returns '1' if date ok
        if(c==1)  //Date OK
        { DATE[k] = [d,m,y];
          alert(DATE[k][0]+" "+DATE[k][1]+" "+DATE[k][2]);                                              
          k++;
        }
    }
    

AJ.onreadystatechange = function(){
if((AJ.readyState == 4) && (AJ.status == 200))
        {   var PH_RP = AJ.responseText; 
            alert("PHP REPLY:: " + PH_RP);
            var DATE_Lst = JSON.parse(PH_RP);       
            alert("DATE1 Day::" + DATE_Lst.DT[0][0] + "DATE1 Month::" + DATE_Lst.DT[0][1] +"DATE1 Year::" + DATE_Lst.DT[0][2] );
            alert("DATE2 Day::" + DATE_Lst.DT[1][0] + "DATE2 Month::" + DATE_Lst.DT[1][1] +"DATE2 Year::" + DATE_Lst.DT[1][2] );
                
        }   
        var para = "DATESS=" + DATE;                        //? how do I send the whole stack of dates to the PHP file?
        AJ.open("POST", "PHP/4_PHP_Check_Dates.php", true);
        AJ.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        AJ.send(para);

Here is the PHP file::

            $LOGARRY['DT'] = $_POST['DATESS'];
            or
            $LOGARRY['DT1'] = $_POST['DATESS'][1];
            $LOGARRY['DT2'] = $_POST['DATESS'][2];

            echo json_encode($LOGARRY);

Thanks.. any help appreciated

Madventures
  • 119
  • 8

1 Answers1

1

If you just want to check if a date is valid, you do not need the roundtrip to PHP, you can do that right in Javascript.

Detecting an "invalid date" Date instance in JavaScript

You asked how to send an array to PHP via XMLHttpRequest.

Since I avoid JQuery like the plague, I cannot really comment on your code, but here is a simple way to send X dates to you PHP script:

Three things:

  1. it is simple (no errorchecking etc)

  2. it used GET. Feel free to make it into a POST.

  3. Open your javascript console to see the result.

<html>
<body>
<script>
// I assume you have an array with strings that might represent dates, like:
// dates[0] = "2022-10-09";
// dates[1] = "2021-13-09";
// ....
// dates[19] = "I am surely not a date";
// dates[20] = "2000-01-05";
var dates = [];
dates[0] = "2022-10-09";
dates[1] = "2021-13-09";
dates[2] = "I am surely not a date";
dates[3] = "2000-01-05";

var myTestDates = JSON.stringify(dates);

var myReq = new XMLHttpRequest();
myReq.addEventListener("load", reqListener);          
var url = "dateChecker.php?dates=" + encodeURIComponent(myTestDates);
url += "&rand=" + Math.random();
myReq.open("GET", url , true);
myReq.send();

function reqListener () {
   let passeddates = JSON.parse(this.responseText); 
   console.log(passeddates);
}

</script>

</body>
</html>

And the PHP script (named dateChecker.php):

<?php
$passedDates = json_decode( $_GET["dates"] );
$checkedDates = [];
foreach ($passedDates as $oneDate){
    $d = DateTime::createFromFormat('Y-m-d', $oneDate);
    $valid = ($d && $d->format('Y-m-d') === $oneDate) ? "valid" : "not valid";
    $checkedDates[] = $oneDate . " is {$valid}";
}
echo json_encode($checkedDates);
?>
Erwin Moller
  • 2,375
  • 14
  • 22
  • Hi Erwin, on the page I have split the dates into their D M Y because there's a script that detects if a "Day" is valid". My main question is how do I pass to the PHP an array of arrays via AJAX, and be able to roll thru them when same is returned. i.e. whats the most concise way to pass and return an array of arrays via Ajax. – Madventures Oct 25 '22 at 13:33
  • @Madventures Aha, so I don't get away with my easy answer and avoiding reading your actual code? ;-) I'll take a look and update my reaction. – Erwin Moller Oct 25 '22 at 13:45
  • haha! yeah I don't let people off easy :D, no worries, take your time!! thanks Erwin! – Madventures Oct 26 '22 at 02:51