Send JS variable value to PHP

Asked

Viewed 29 times

0

So, I have a table where I display the database data. On each row there is a button where I added an event onClick , which calls a JS function and opens a modal. In this function, it passes the value that is in the attribute var-data:

<button data-var="<?php echo $cont; ?>" onClick="funcaoModal(this)" class="dropdown-item" data-toggle="modal" data-target="#modal-atualizar">Atualizar</button>

Within the function:

 function funcaoModal(element) {
        var dataVar = $(element).attr('data-var')
        alert(dataVar)

    }

I can visualize q it passes a certain value of "$cont", which would be the index of my Data Array coming from the database, because I want to do the following, send to the input ticket, for example, the value "<php $result[dataVar]['ticket]; ?>", but the problem is that you can’t put the variable dataVar within the PHP code...

It would work if I needed only 1 dice, because then I would already put the PHP code in the "data-var" button, move to the function, and send it to the modal. But I need a lot of information, so I’m sending the index number, because I can pull any data through it, but I can’t in any way concatenate the JS in PHP, or turn everything into JS.

Someone has a solution?

It can be: Assign various values to the button, and when clicked, it sends all to the function, which assigns to each Modal input Or a way to send only the index value to PHP, so that I concatenate the information to the call of the array, that way "<php $result[dataVar]['ticket]; ?>".

Does anyone have any solution?

Obs. I took the following test:

function funcaoModal(element) {
        var dataVar = $(element).attr('data-var')
        $('#ticket').val("<"+"?php echo $result[2]['ticket']?>")
    }

This sets the value in the modal "value", but for some reason it looks like this:

Ao invés de ficar o valor do "result", ele pega como uma string, porém, quando coloco o mesmo trecho direto no "value" do modal, ele volta a informação do "result"

Instead of getting the value of "result", it picks up as a string, however, when I put the same chunk directly in the "value" of the modal, it returns the information of "result"

1 answer

-2

Although it’s not a good technique, just write inside the JS with PHP. Don’t forget that PHP requires ";" at the end of each line, unlike JS.

function funcaoModal(element) {
  var dataVar = $(element).attr('data-var')
  $('#ticket').val("<?php echo $result[2]['ticket']; ?>")
}

  • So, I think that works, the problem is that instead of [2] I need to pass the variable dataVar, and then it doesn’t recognize because the variable is JS. And then when I tried to break, close the PHP tag after "[" it doesn’t let either, the php syntax gets wrong

  • 2

    1) If it is not a "good practice", the ideal would not be to avoid doing it? 2) It is not enough to write PHP inside the JS; this can work in the "html file", because it is the PHP file being processed on the server and returning an HTML, but it is not common for PHP to also serve the application’s JS files - in fact there is no reason for it, so the PHP code inside the JS file will not only not be treated by the server but could, depending on the situation, break the JS syntax. 3) PHP does not demands the ; at the end of the line to the last expression before the ?>, so the character is optional here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.