1

I'm beginner of php and javascript so i dont really now whats the wrong in my code. i just want to send a javascript variable to php and its not working, i dont now why. These files all different pages. So its a click function in javascript:

file: SelectTableRow.js.

var productTable = document.getElementById('ProductTable'), rIndexB;

for(var i = 0; i < productTable.rows.length; i++)
{
    productTable.rows[i].onclick = function()
    {
        rIndex = this.rowsIndexB;
        var bAzon = this.cells[0].innerHTML;

        $.post("AddProductTable.php", {BAzon: bAzon}, function(){
        $('#load').load('AddProductTable.php');
        });

        console.log(bAzon);
    }
}

The database is running clear too. The data what should display:

file: AddProductTable.php

<table class="DataTable">
    <th>Azonosito</th>
    <th>Név</th>
    <th>Mennyiség</th>
    <th>Mértékegység</th>
    <th>Ár</th>
    <th>Törlés</th>


<?php

require_once 'Connect.php'; // its 100% working.

$BAzon = $_POST['BAzon'];  // 0 result :(

if($result = $link_database->query("SELECT bevetAzon, termekAzon, bevettMennyiseg, bevettAr FROM bevetttermek WHERE bevetAzon = '$BAzon'"))
{
    $table = $result->fetch_all();

    foreach($table as $row)
    {   
            echo "<tr>";
            foreach($row as $record)
            {   
                echo "<td>".$record."</td>";
            }
            echo "</tr>";
    }
}

echo "BAZON: ".$BAzon;  //0 result
echo "POST: ".$_POST['BAzon'];  // 0 result

?>
</table>

and the file where i call this(just the important part):

Index.php

<div id="load">

</div>

When im trying a test number, its working, display the datas. Please help me, i cant go through this one.

J.Do
  • 303
  • 6
  • 26
  • 1
    open developer tools in your browser, open the Network tab and see that request once it's made - you can see what POST data is being sent to AddProductTable – Marcin Feb 05 '19 at 14:32
  • 1
    Ohh, thanks, thats shows me, the datas are send. And its sent, but why i cant display? :( BAZON: 98POST: 98
    Azonosito Név Mennyiség Mértékegység Ár Törlés
    9841010
    9841010
    9841010
    9841010
    9811010
    – Márton Csaba Feb 05 '19 at 14:38
  • what's the names of the POST fields? – Marcin Feb 05 '19 at 14:42
  • $BAzon = $_POST['BAzon']; or what kinda of fields? :D – Márton Csaba Feb 05 '19 at 14:48
  • In your code you post your data to AddProductTable.php, but in the next line you get a completely fresh AddProductTable.php with no data posted. (I hope this explanation makes sense) I think you will find your answer here: https://stackoverflow.com/questions/1562043/jquery-post-functions-result-data – Hans Dash Feb 05 '19 at 14:56
  • You get my problem, thanks for helping!! :D – Márton Csaba Feb 05 '19 at 15:06

1 Answers1

1

So this should do the trick:

var productTable = document.getElementById('ProductTable'), rIndexB;

for(var i = 0; i < productTable.rows.length; i++)
{
    productTable.rows[i].onclick = function()
    {
        rIndex = this.rowsIndexB;
        var bAzon = this.cells[0].innerHTML;

        $.post("AddProductTable.php", {BAzon: bAzon}, function(data){
        $('#load').html(data);
        });

        console.log(bAzon);
    }
}
Hans Dash
  • 761
  • 4
  • 18