bzc0fq
bzc0fq

Reputation: 719

Error getting data from mysql using javascript and ajax

I try to get data from mysql database using ajax. I adapted my old ajax code that works OK in different project, but It does not work here.

I use javascript function as bellow:

var text1 = document.getElementById('ST0_nazwaMenu');
var combo1 = document.getElementById('S0_dzienMenu');
var combo2 = document.getElementById('S0_posilekMenu');

function zaladujSzczegoly() {
        if (text1.value.trim() != "" && combo1.value.trim() != "" && combo2.value.trim() != "") {

                var nazwaMenu = text1.value.trim();
                var dzienMenu = combo1.value.trim();
                var posilekMenu = combo2.value.trim();

                $.ajax({
                        method: "POST",
                        url: "test.php",
                        data: {"jadlospis": nazwaMenu.val(), "dzien": combo1.val(), "posilek": combo2.val()},
                }).done(function( data ) {
                        var result = $.parseJSON(data);
                });


                alert(text1.value);

        }
}

text1.addEventListener('change', zaladujSzczegoly, false);
combo1.addEventListener('change', zaladujSzczegoly, false);
combo2.addEventListener('change', zaladujSzczegoly, false);

The test.php file is just for testing and it looks like this:

<?php

include 'db_connection.php';
include 'functions.php';


error_log("-->test.php:"."\n", 3, "/var/www/html/jadlospis/errors.log");

if (isset($_POST['jadlospis'],$_POST['dzien'],$_POST['posilek'])){

        error_log($_POST['jadlospis']."\n", 3, "/var/www/html/jadlospis/errors.log");

        $SQL = "SELECT COUNT(*) AS Konta FROM users WHERE jadlospis='".$_POST['jadlospis']."'";

        $result = mysqli_query($conn,$SQL ) or die(mysqli_error($this->dblink));
        while($row = mysqli_fetch_array($result))
        {
                $T_Count=$row['Konta'];
        }

        mysqli_close($conn);
        echo $result;
}

?>

After changing form controls the function is called and then, I got an error message in browser console saying: TypeError: nazwaMenu.val is not a function

Any hints on what the problem might be, please?

Upvotes: 0

Views: 31

Answers (2)

davidev
davidev

Reputation: 8517

Try this line:

data: {"jadlospis": nazwaMenu, "dzien": combo1, "posilek": combo2}

You won't need a , at the end, as it is the last value.

Edit: Please see @Irony Stack solution, val() is not needed anymore.

Upvotes: 1

Shankar
Shankar

Reputation: 3088

You have already fetched the value of text1 into variable nazwaMenu so you do not need to call nazwaMenu.val()
You can use text1.value.trim() or $("#ST0_nazwaMenu").val().trim() or just the varibale containing value nazwaMenu

because .val() can not be called on value or DOM, to usr .val() object needs to be of JQuery Object

replace your this line

data: {"jadlospis": nazwaMenu.val(), "dzien": combo1.val(), "posilek": combo2.val()},

with

data: {"jadlospis": nazwaMenu, "dzien": dzienMenu , "posilek": posilekMenu }

and you should be fine~

Upvotes: 2

Related Questions