button inside PHP variable

Asked

Viewed 1,186 times

1

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

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

when clicking the button appears the error: Uncaught Referenceerror: turns off is not defined onclick

I’ll explain better what I need.

I need to create some buttons dynamically because the number of buttons can change. For example I might 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. As described below the code works, but what I want is to be able to create as many buttons as I want according to the result of num_rows of a query sql.

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

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

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

?>

  • It’s impossible to do what you want the way you’re shown. If you have other pieces of code that help clarify what you are trying to do, you may have another solution. But in general it seems impossible. PHP and JS are different languages and completely independent.

  • I tried to explain better see if you can help me

  • 1

    Improved. Where is this function liga()? Is it written in JS or PHP? If it is in JS, I already know that it is not possible to do this. As I said, languages are independent, despite the popular belief that they work together. It is not easy to respond with partial codes. It does not mean that you need to put everything you have but something minimal that can produce a useful result. http://answall.com/help/mcve

  • right, I put an example of function

  • 1

    I still can’t figure out what you want. Aside from a few organizational errors and syntax what you are demonstrating works but I doubt it will do what you want. If you can’t demonstrate what you want to do, I won’t be able to answer. I hope someone else can, but it’s probably the best guess. See how http://ideone.com/bRPhwd. Let me repeat: if you think PHP and JS are the same language, that you can write everything mixed up and it will work, forget it. If you want a solution to the problem, consistently describe the real problem.

  • is the typical error of beginners in mixing server side and client side.. I think a better way is to guide Roberto to understand server-side and client-side, browser, servers, etc.

Show 1 more comment

1 answer

1

In PHP it is possible yes, 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 you will run the method 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 by that id.

PHP

desliga($_GET['id']);

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

?>

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

Browser other questions tagged

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