Sir Papilonius
Sir Papilonius

Reputation: 113

Having trouble parsing php variable to javascript readable variable

This is my first project in database management and it's been awhile since I've coded any html, php, or javascript and I'm very new to sql in general. I know that ideally html, php, css, and javascript should be in separate files. However, my issue is trying to get a php variable into my script function. The function is below. This is using Apache localhost server as it's not a live page, MySQL, and XAMPP Control Panel v3.2.1

I have tried: var data = "<?php echo $data2dArr[0][0]; ?>"; just as test code but cannot get a value to populate, it doesn't even execute the make visible code. I've also tried in the onclick function call this: <script> var data = "<?php echo $data2dArr[0][0]; ?>";</script> as well as:

<?php echo $data2dArr[0][0]; ?>

as a function parameter. Plus about a half dozen other solutions I found on stack that don't work. I'm wondering if there is a setting I need to adjust in order to get the server to acknowledge the PHP code within the javascript.

function editFunction(row, columnCount) {
// set the data for the boxes and make them visibile
   for (column = 0; column < columnCount; column++) {
    document.getElementById(column).style.visibility = "visible";
    var data = <?php echo json_encode($data2dArr[column][row]); ?>;
    document.getElementById("box" + column).value = data;
   }
}

Ideally, it should display the correct data from the column and row of the data so the user can change the data and send it back to the database to update the entry. (I don't need help with the sql statement, just getting the data from php.) Any help would be greatly appreciated.

Upvotes: 0

Views: 53

Answers (3)

Sir Papilonius
Sir Papilonius

Reputation: 113

Okay, so I figured out what I was doing wrong. I had tried calling getElementbyID for value but I never tried innerHTML. Here is the working code.

    function editFunction(row, columnCount) {
        // set the data for the boxes and make them visibile
        for (column = 0; column < columnCount; column++) {
            document.getElementById(column).style.visibility = "visible";
            document.getElementById("box" + column).value = document.getElementById("row" + row + "column" + column).innerHTML;
        }
    }

The php that sets the data:

<!--- row number from table --->
<?php for ($j = 0; $j <= count($data2dArr[0]); $j++) {
    if ($j < count($data2dArr[0])) {?>
        <tr>
        <!--- column number from table --->
        <?php for ($k = 0; $k <= count($fields); $k++) { 
                if ($k < count($fields)) { ?>
                        <td style="width: 7em" id="row<?php echo $j ?>column<?php echo $k ?>" ><?php print $data2dArr[$k][$j]; ?></td>
                        <?php }

And the onclick that calls the script:

<button id="edit<?php echo $j ?>" type="button" class="btn btn-default btn-outline-dark" 
                                        onclick="editFunction(<?php echo $j ?>, <?php echo count($fields) ?> )">
                                    <span class="fa fa-edit"></span> Edit
                                </button>

I was initially, purposefully vague because I was not asking people to do my homework for me. I have found that asking the question of someone else usually helps me figure out the solution. I am posting the answer for future people that may come across the same issue. Thank you to everyone that responded. This is using PHP, HTML, SQL, and javascript in one PHP file.

Upvotes: 0

pixelbath
pixelbath

Reputation: 55

Try:

<?php var_dump($data2dArr); ?>

This will dump the entire variable. If $data2dArr is undefined, PHP will output an error (if your configuration allows this). If it is defined, it should tell you not only the type, but the contents.

Upvotes: 1

Shane Weeks
Shane Weeks

Reputation: 77

In the PHP tags try echo 'test'; Does it show? If yes, is $data2dArr populated as you expect? If no, are you sure your server is setup to execute PHP within whatever file type you're trying

Upvotes: 0

Related Questions