0

Currently I have stored variables in a Javascript array. The goal here is to turn them into PHP variables so that I can use these variables and insert them into my database.

The problem with this code is that the AJAX part doesn't work. Please get me in the right direction as I am extremely new to AJAX. I did try to read about them but still do not understand much. Doing it without refreshing the page is not necessary. Methods other than AJAX are welcomed.

Here is my current code:

<button onclick="Bookings()">Book</button>
<script>
    function Bookings() {
        var t2Cells = document.getElementsByClassName("t2");
        for (var i = 0; i < t2Cells.length; i++) {
            var t2CellsIndex [i] = t2Cells[i].cellIndex
            var t2CellsRow [i] = t2Cells[i].parentNode.rowIndex
            //alert('Coordinates are: '+t2Cells [i].cellIndex+'x'+t2Cells [i].parentNode.rowIndex);
            var tbl = document.getElementById("tblMain");
            //alert (tbl.rows[0].cells[t2CellsIndex].innerHTML);
            var studioSelected = tbl.rows[0].cells[t2CellsIndex].innerHTML
            var Timeselected = tbl.rows[t2CellsRow].cells[0].innerHTML

            $.ajax({
                type: "POST",
                url: 'bookingconfirm.php',
                data: "studioSelected=" + studioSelect,
                success: function(data) {
                    alert("success!");
                }
            });
        }
    }
</script>

<?php 
    //bookingconfirmed.php
    if (isset($_POST['studioSelect'])) {
        $uid = $_POST['studioSelect'];
        //steps to insert into database.
6
  • Well what doesn't work? Do you get any errors? is the data not being sent to the ajax file? Commented Nov 6, 2015 at 8:59
  • 2
    So, do I understand it right? You set data key-value pair studioSelected, but check if t2CellsIndex is set? Commented Nov 6, 2015 at 9:00
  • 1
    What does print_r($_POST) return in the ajax target file bookingconfirm.php? Commented Nov 6, 2015 at 9:00
  • One more thing here you are calling $.ajax in side a loop that means there will be multiple request to the sever depending on your loop iteration. Please check with Chrome Developer tool (Press F12 at chrome) and check network tabe for all XHR request, you will get some idea how data is being passed for AJAX. Commented Nov 6, 2015 at 9:02
  • @YeldarKurmangaliyev Mistake on my part, the PHP part was the wrong one. I revised it already. Commented Nov 6, 2015 at 9:34

1 Answer 1

1

First, you should move ajax call outside of foreach

var usefulData = [];
var t2Cells = document.getElementsByClassName("t2");
for (var i = 0; i < t2Cells.length; i++) {
    var t2CellsIndex [i] = t2Cells[i].cellIndex
    var t2CellsRow [i] = t2Cells[i].parentNode.rowIndex
    //alert('Coordinates are: '+t2Cells [i].cellIndex+'x'+t2Cells [i].parentNode.rowIndex);
    var tbl = document.getElementById("tblMain");
    //alert (tbl.rows[0].cells[t2CellsIndex].innerHTML);
    var studioSelected = tbl.rows[0].cells[t2CellsIndex].innerHTML

    // add data to array
    usefulData.push(studioSelected);

    var Timeselected = tbl.rows[t2CellsRow].cells[0].innerHTML
}

        $.ajax({
            type: "POST",
            url: 'bookingconfirm.php',
            data: {'usefuldata': usefulData},
            success: function(data) {
                alert("success!");
            }
        });

Then in your php file:

if (isset($_POST['usefuldata'])) {
    var_dump($_POST['usefuldata']);
}
Sign up to request clarification or add additional context in comments.

5 Comments

The codes stop running till usefulData.push(studioSelected); . Any idea why?
any errors in console? can you set up a jsFiddle with your example?
I am not knowledgeable in using console on jsFiddle. Instead i duplicated my codes into the jsfiddle for your better understanding. The success: function(data) { alert("success!"); doesn't trigger. jsfiddle.net/rn7uqhtw/4
add var usefulData = []; to start of your function.
The array code does work now but the ajax part is still not running. Sorry for the late reply, was down with a fever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.