Concatenate variable javascript in php path

0
$(document).on('click','.view-info',function(){
    id_ = $(this).val();
    url = '<?= site_url("Venta/detail/'+id_+'") ?>',

This is stressing me out, as I can concatenate the variable id_ in my route that is seen in the code. I have already tried a thousand and one way and it marks me wrong.

another attempt:

url = '<?= site_url("Venta/detail/'.$id_.'") ?>',
<p>Severity: Notice</p>
<p>Message:  Undefined variable: id_</p>
<p>Filename: views/venta_detalle.php</p>
<p>Line Number: 56</p>
    
asked by DoubleM 23.05.2018 в 01:49
source

1 answer

1

The problem you have "roughly" has no solution. Both the JavaScript code and the HTML code is processed by the PHP kernel that after processing it generates it and the browser shows it to you. Example:

$a = "<h1>hola</h1>";

With this code, the PHP kernel will generate the following document, which is what it will send to the browser:

<html>
    <head></head>
    <body>
     <h1>hola</h1>
    </body>
</html>

What I mean is that you can not try to enter JavaScript variables in PHP code with the intention that this process the value of the JavaScript variable, since PHP will generate the JavaScript variable as is so that the browser uses it together your JavaScript code That is, you can not pass a JavaScript variable to the PHP function and expect it to recognize the value and use it. Now, in your specific case, the following may work for you:

$(document).on('click','.view-info',function(){
     id_ = $(this).val();
     url = '<?php site_url("Venta/detail/") ?>' + id_;

This in the document that PHP generates would be something like:

$(document).on('click','.view-info',function(){
     id_ = $(this).val();
     url = 'lo_que_la_función_php_site_url_genere' + id_;

If this last one does not work for you, I doubt you can do what you want, you will surely have to change the approach of your program. If you provide more information about what you want to do with your program, we may be able to help you.

    
answered by 23.05.2018 в 20:40