Pass php variables to function onClick

0
<script>
$.fn.Recibe = function(id,p) {
        alert(id+' '+p);
} 
</script>

En PHP
$diPesca=1;
$var="Enviar parametrosa funcion js";

$fila="<div title='click para enviar' ";
$fila .=" onClick=\"$(this).Recibe('".$idPesca."','".$var."');\">";
$fila .="aqui";

$fila ."</div>";
echo $fila
    
asked by Julio César Páez 29.08.2017 в 21:54
source

3 answers

1

I suggest you implement it like this:

<script>
function Recibe(id, p) {
	alert(id+' '+p);
}
</script>

In PHP

$idPesca=1; 
$var="Enviar parametrosa funcion js";
$fila="<div title='click para enviar' "; 
$fila .="onClick=\"Recibe('".$idPesca."','".$var."');\">"; 
$fila .="aqui";
$fila ."</div>"; 

I hope it serves you! Greetings

    
answered by 29.08.2017 в 22:23
0

It is not clear if you have questions or are consulting, but as I understand, adjust some lines to your code:

<script>
    $.fn.Recibe = function(id,p) {
    alert(id+' '+p);
    } 
</script>

<?php
$idPesca=1;
$var="Enviar parametrosa funcion js";

$fila="<div title='click para enviar' ";
$fila .=" onclick='$(this).Recibe($idPesca,\"$var\");'>";
$fila .="aqui";

$fila .="</div>";
echo $fila;
?>

I guess you're already importing the JQuery library.
Without Jquery it would be:

<script>
    Recibe = function(id,p) {
    alert(id+' '+p);
    } 
</script>

<?php
$idPesca=1;
$var="Enviar parametrosa funcion js";

$fila="<div title='click para enviar' ";
$fila .=" onclick='Recibe($idPesca,\"$var\");'>";
$fila .="aqui";

$fila .="</div>";
echo $fila;
?>
    
answered by 29.08.2017 в 22:27
0

Good morning, you have some syntax errors;

  • the $ idPesca variable is not declared, you have it as diPesca .
  • All PHP lines end in ; Your echo line does not end in;
  • In the penultimate line you are not concatenating the row variable you are missing the = . I enclose the corrections: And I think that's why I miss the fact that you can not pass the parameters from PHP to JS, I tried it with the corrections that I show you and it works.
  • Note: do not forget to import the JQuery library to make it work.

        
    answered by 29.08.2017 в 22:32