0

I am using Jquery array to fetch variables on dynamic input forms. I could group and output the arrays into the console.log, but no data could be inserted within the multiple table database itself.

here's the Jquery code with ajax and console.log


    $("button[name='submit_addPackresCategories']").click(function(){
        var packres_categories_name;
        $("input[id='packres_categories_name']").each(function(){
            packres_categories_name = this.value;
        });
        console.log(packres_categories_name);

        var packres_categories_description;
        $("textarea[id='packres_categories_description']").each(function(){
            packres_categories_description = this.value;
        });
        console.log(packres_categories_description);

        var packres_categories_paxhead_day = {};
        $("input[id^='packres_categories_paxhead_day']").each(function(){
            packres_categories_paxhead_day[this.id] = this.value;
        });
        console.log(packres_categories_paxhead_day);

        var packres_categories_paxhead_price = {};
        $("input[id^='packres_categories_paxhead_price']").each(function(){
            packres_categories_paxhead_price[this.id] = this.value;
        });
        console.log(packres_categories_paxhead_price);

        var packres_categories_paxgroup_size = {};
        $("input[id^='packres_categories_paxgroup_size']").each(function(){
            packres_categories_paxgroup_size[this.id] = this.value;
        });
        console.log(packres_categories_paxgroup_size);

        var packres_categories_paxgroup_consumable = {};
        $("input[id^='packres_categories_paxgroup_consumable']").each(function(){
            packres_categories_paxgroup_consumable[this.id] = this.value;
        });
        console.log(packres_categories_paxgroup_consumable);

        $.ajax({
            url: "ajax/addPackresCategories.php",
            type: "post",
            data: {
                packres_categories_name: packres_categories_name,
                packres_categories_description: packres_categories_description,
                packres_categories_paxhead_day: packres_categories_paxhead_day,
                packres_categories_paxhead_price: packres_categories_paxhead_price,
                packres_categories_paxgroup_size: packres_categories_paxgroup_size,
                packres_categories_paxgroup_consumable: packres_categories_paxgroup_consumable
            }
        })
        .done(function(response){
            if (JSON.parse(response)) {
                alert("Successfully Added New Package!");
            }
            else {
                alert("Failed!");
            }
        });
    });

here's the url for the ajax

<?php
    include_once("../db.php");
    include "admin_session.php";

    $packres_categories_name = $_POST["packres_categories_name"];
    $packres_categories_description = $_POST["packres_categories_description"];
    $packres_categories_paxhead = array_combine($_POST["packres_categories_paxhead_day"], $_POST["packres_categories_paxhead_price"]);
    $packres_categories_paxgroup = array_combine($_POST["packres_categories_paxgroup_size"], $_POST["packres_categories_paxgroup_consumable"])

    $sql_insertPackresNameDesc_packresCategories = "INSERT INTO packres_categories(packres_categories_name, packres_categories_description) VALUES('" . mysqli_real_escape_string($conn, $packres_categories_name) . "', '" . mysqli_real_escape_string($conn, $packres_categories_description) . "')";
    $query_sql_insertPackresNameDesc_packresCategories = $conn->query($sql_insertPackresNameDesc_packresCategories);
    $affectedRows_query_sql_insertPackresNameDesc_packresCategories = mysqli_affected_rows($conn); //will be used to verify if inserted properly on database later

    $packres_id = mysqli_insert_id($conn);//will be used later

    foreach ($packres_categories_paxhead as $key => $value) {
        $sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead = "INSERT INTO packres_categories_paxhead(packres_id, packres_categories_paxhead_day, packres_categories_paxhead_day) VALUES('" . mysqli_real_escape_string($conn, $packres_id) . "', '" . mysqli_real_escape_string($conn, $key) . "', '" . mysqli_real_escape_string($conn, $value) . "')";
        $query_sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead = $conn->query($insertPackresCategoriesPaxhead_packresCategoriesPaxHead);
        $affectedRows_query_sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead += mysqli_affected_rows($conn);
    }

    foreach ($packres_categories_paxgroup as $key => $value) {
        $sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup = "INSERT INTO packres_categories_paxgroup(packres_id, packres_categories_paxgroup_size, packres_categories_paxgroup_consumable) VALUES('" . mysqli_real_escape_string($conn, $packres_id) . "', '" . mysqli_real_escape_string($conn, $key) . "', '" . mysqli_real_escape_string($conn, $value) . "')";
        $query_sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup = $conn->query($sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup);
        $affectedRows_query_sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup += mysqli_affected_rows($conn);
    }

    $conn->close();

    if (($affectedRows_query_sql_insertPackresNameDesc_packresCategories != 0) && ($affectedRows_query_sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead != 0) && ($$affectedRows_query_sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup != 0)) {
        echo json_encode(true);
    }
    else {
        echo json_encode(false);
    }
?>

arrays of data should be inserted properly into the database, but no data were entering the database.

error line being thrown at console

Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (master.js:70)
    at u (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)
1
  • 1
    will look into that. thank you :) Commented Jul 17, 2019 at 8:48

1 Answer 1

1

json_encode()- returns the JSON representation of a value. And you are returning boolean

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

Comments

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.