Retrieving multiple data via ajax

Discussion in 'Off Topic' started by springb5, Oct 9, 2019.

  1. springb5

    springb5 New Member

    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Hey. Anybody with Ajax & PHP knowledge that can help me?

    I have a booking form which I would like to check with the database before we submit.

    When the customer clicks on a product & selects a date, I want to know if it's available or not.

    Here's what I have so far... I've never used ajax before, so there's no wonder it doesn't work.

    Product fields ID's are: product, product2, product3. Date field ID is startdate.

    AJAX:

    <script language = "javascript" type = "text/javascript">
    <!--
    //Browser Support Code
    function ajaxFunction(){
    var ajaxRequest; // The variable that makes Ajax possible!

    try {
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
    }catch (e) {
    // Internet Explorer Browsers
    try {
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e) {
    try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
    }
    }
    }

    // Create a function that will receive data
    // sent from the server and will update
    // div section in the same page.
    ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
    var ajaxDisplay = document.getElementById('ajaxDiv');
    ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
    }

    // Now get the value from user and pass it to
    // server script.
    var p1 = document.getElementById('product').value;
    var p2 = document.getElementById('product2').value;
    var p3 = document.getElementById('product3').value;
    var sd = document.getElementById('startdate').value;
    var queryString = "?sd=" + sd ;

    queryString += "&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3;
    ajaxRequest.open("GET", "getuser.php" + queryString, true);
    ajaxRequest.send(null);
    }
    //-->
    </script>


    PHP: (getuser.php)
    <?php
    include 'connect.php';
    $q = intval($_GET['sd']);
    $p1 = intval($_GET['p1']);
    $p2 = intval($_GET['p2']);
    $p3 = intval($_GET['p3']);
    $sqlavailable="SELECT * FROM bookings WHERE startdate = '".$q."'";
    $result = mysqli_query($link,$sqlavailable);
    while($row = mysqli_fetch_array($result)) {
    IF (($row[product] == $p1) OR ($row[product2] == $p1) OR ($row[product3] == $p1)) {
    echo '".$p1." unavailable';
    } else {
    echo '".$p1." available';
    }
    IF (($row[product] == $p2) OR ($row[product2] == $p2) OR ($row[product3] == $p2)) {
    echo '".$p2." unavailable';
    } else {
    echo '".$p2." available';
    }

    IF (($row[product] == $p3) OR ($row[product2] == $p3) OR ($row[product3] == $p3)) {
    echo '".$p3." unavailable';
    } else {
    echo '".$p3." available';
    }
    }
    mysqli_close($link);
    ?>

    Would really appreciate any input. Thanks in advance.
     

Share This Page