button within PHP variable

1

Hello, In php is there any way to make the button below call the power off function ()

$button2 = "<input type='button' id='php_button' value='desliga' onclick='desliga()'>";
echo $button;

clicking the button displays the error: Uncaught ReferenceError: off is not defined onclick

I'll better explain what I need.

I need to create some buttons dynamically because the number of buttons can change. For example it may be that I need 5 buttons with different names that call the same function.

Using only html as described below works, but this way I can not create the buttons dynamically. In the way described below the code works, but what I want is to be able to create as many buttons as I want according to the num_rows result of a sql query.

<?PHP
function liga(){
echo "teste";
}

<input type="submit" name="liga" value="liga"/>

if (isset($_REQUEST['liga'])) {
    unset($_REQUEST['liga']);
    liga();

? >

    
asked by anonymous 15.03.2015 / 21:08

1 answer

1

In PHP it is possible, but you are calling a method in javascript. The closest to what you need is to do this method in javascript that will call a new page where the method will run in PHP:

HTML:

<input type='button' onclick='desliga(1)'>
<input type='button' onclick='desliga(2)'>
<script>
    function desliga(id) {
        window.location.href = 'enderecoMetodoDesliga/?id='+id;
    }
</script>

Note that an id is being passed as a parameter, so you can have multiple buttons on the same page, calling the same method, differentiating the action with that id.

PHP     

desliga($_GET['id']);

function desliga($id){
    echo "teste, id: " . $id ;
}

?>

Consider more about client-side (HTML, javascript, etc.) and server-side (PHP, access to bank, etc) requests. This solution can get more "elegant" in ajax. Consider studying this too.

    
16.03.2015 / 15:45