Doubt with passing php value to javascript

0

On the page that I show below I am calling a php to pass the value of a field to me. When I do onclick in a row of my table the process works correctly, it brings me the comment field.

In an HTML segment I also have a button, which I need to work in the same way, but I have not succeeded, the file cumpl_alerta.php returns me undefined.

index.php

    <script type="text/javascript">
        function cumplir_alerta(id_entabla) {
        var num_id2 = id_entabla;
            
            if (num_id2 != "") {
                $.post("cumplir_alerta.php", {valorBusqueda: num_id2},
                function(mensaje) {                        
                    $("#comentario1").css('text-align','center');
                        $("#comentario1").html(mensaje);
                }); 
            };
        };  

    <script>

    <div id="fade" class="overlay"></div>
    <div id="light" class="modal">
        <p id="comentario1"></p>
        <p id="botones_alerta">
        <input type='button' value='Cumplido' id='cumplido' onclick="cumplir_alerta(<?=$id?>);">
        <input type='button' value='Leído' id='leido' onclick="javascript:vw_nomostrar();"></p>
    </div>

    <?php
    while (($fila = mysqli_fetch_array($result))!=NULL){
        $id=trim($fila['id']);
        $vsocio=trim($fila['numero_socio']);
        $vcomentarios=trim($fila['observaciones']);

        echo "<tr>\n";
        echo "    <td align='center'>". $vsocio . "</td>\n";
        echo "    <td align='left'>" .substr($vnombre . ", " . $vapellido,0,30) . "</td>\n";

        if (strlen($vcomentarios) == 0 || $vcomentarios === '-'){
            echo "    <td align='center'><input type=\"button\" value=\"     \" id=\"Ver2\" onclick=\"javascript:buscar('".$id."')\";></td>\n";
        }else{
            echo "    <td align='center'><input type=\"button\" value=\"Ver\" id=\"Ver\" onclick=\"javascript:buscar('".$id."')\";></td>\n";
        }
    ?>

cumpl_alerta.php

<?php
    $consultaBusqueda = $_POST['valorBusqueda'];
    $mensaje="OK ... EL ID ES " . $consultaBusqueda;
    echo nl2br($mensaje);
?>

The latter I put together as to show what is necessary. What am I doing wrong?

    
asked by look68 13.08.2018 в 21:59
source

2 answers

1

I have tried the code on my computer and it works correctly, I was going to put the comment but so far I can only answer.

The problem or what you are doing wrong is in the code that has the button fulfilled since the variable $id does not exist even in that part of the code

This is because you create it within the while and for that reason you get a undefined no value is reaching it

If you use it in while after assigning the $ id it will work for you

    <div id="fade" class="overlay"></div>
    <div id="light" class="modal">
        <p id="comentario1"></p>
        <p id="botones_alerta">
        <!-- $id no existe en esta parte del codigo -->
        <input type='button' value='Cumplido' id='cumplido' 
            onclick="cumplir_alerta(<?=$id?>);">
        <input type='button' value='Leído' id='leido' onclick="javascript:vw_nomostrar();"></p>
    </div>

    <?php
    while (($fila = mysqli_fetch_array($result))!=NULL){
        // Aqui se asigna la variable $id, puedes usarla despues de esto
        $id=trim($fila['id']);             $vsocio=trim($fila['numero_socio']);
        $vcomentarios=trim($fila['observaciones']);

        // en este echo si te va a funcionar por que $id tiene un valor
        echo "<tr>\n <input type='button' value='Cumplido' onclick="cumplir_alerta(<?=$id?>);">";
   /* ... */
    }

Update

Your problem is really the variable $ id without value and put where it does not exist

But I was going to suggest the same thing that @M says. Gress in your answer, it is preferable, as good practices, that if you are using jquery do not use the attribute onclick if not through jQuery with $("mi-id").click(()=>{....})

    
answered by 13.08.2018 / 23:01
source
0

Well first of all I would have to suggest that you use some JavaScript to be more precise Jquery

In your Modal add a hidden input where you deposit the ID when the function Search

is executed
<div id="fade" class="overlay"></div>
<div id="light" class="modal">
    <p id="comentario1"></p>
    <p id="botones_alerta">
    <input type='button' value='Cumplido' id='cumplido' onclick="cumplir_alerta(<?=$id?>);">
    <input type='text' id='TuID' hidden='hidden'>
    <input type='button' value='Leído' id='leido' onclick="javascript:vw_nomostrar();"></p>
</div>

This so that when you click on the modal button you can know what ID you are associating with in the javascript, it would be something like this:

function Buscar(ID)
{   
   /*Colocas el ID en el input que creamos*/
   $("#TuID").val(ID);
}

function cumplir_alerta()
{
  var ID = $("#TuID").val(); //Así obtienes el ID del input donde lo colocamos anteriormente
 /*Tu código*/
]
    
answered by 13.08.2018 в 22:57