How to call javascript function on button click in php

What a weird question. If anyone has a better title for this, please go ahead and edit it; I can't think of what else to call it. I am printing a button from PHP using echo. The purpose of a button is to show a form by calling a JavaScript function that uses document.getElementById() to change the style attribute of the form tag to visible so the form is visible to the user. The problem I'm having is that the echoed string has to have quotes around it, the onclick event has to have quotes around it, and the parameter passed to the JavaScript function has to have quotes around it. I tried escpaing the quotes for the parameter with backslashes but that didn't work. Can anyone help me?

Here is my code:

echo "";//this shows the form and needs quotes

JavaScript function:

function showform(id)
{
document.getElementById(id).style.visibility = "visible";
}

asked Apr 25, 2013 at 9:49

How to call javascript function on button click in php

2

This works fine for me? (working example)

";//this shows the form and needs quotes

Generates:


answered Apr 25, 2013 at 10:00

How to call javascript function on button click in php

Daan TimmerDaan Timmer

14.6k5 gold badges33 silver badges65 bronze badges

0

Try concatenating the string using a combination of single and double quotes like this:

echo '';

answered Apr 25, 2013 at 9:54

Try this:

echo "";

Additionally, you could try jQuery, a super popular JS Library that makes life easier.

I would make the following CSS class.

.hide {
   display:none;
}

Than I would add the following jQuery:

$("#show-password-reset").click(function(event){
 event.preventDefault();
 $("#passwdform").show();
});

And finally include your show button:


Make sure to use class="hide" on the passwdform element so its hidden when page loads.

answered Apr 25, 2013 at 10:03

Inside PHP \ is already an escape character. If you want to output "\" you need to echo "\\".

answered Apr 25, 2013 at 9:54

KillerXKillerX

1,3761 gold badge14 silver badges23 bronze badges

2

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a document containing HTML and PHP code and the task is to call the PHP function after clicking on the button. There are various methods to solve this problem. Also, apart from doing this with the click of a button, a PHP function can be called using Ajax, JavaScript, and JQuery. But this article mainly focuses on the button oriented approach of calling the PHP Function.

    Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

    Program 1:

    <html>

    <head>

        <title>

            How to call PHP function

            on the click of a Button ?

        title>

    head>

    <body style="text-align:center;">

        <h2 style="color:green;">

            GeeksforGeeks

        h2>

        <h4>

            How to call PHP function

            on the click of a Button ?

        h4>

        php

            if(array_key_exists('button1', $_POST)) {

                button1();

            }

            else if(array_key_exists('button2', $_POST)) {

                button2();

            }

            function button1() {

                echo "This is Button1 that is selected";

            }

            function button2() {

                echo "This is Button2 that is selected";

            }

        ?>

        <form method="post">

            <input type="submit" name="button1"

                    class="button" value="Button1" />

            <input type="submit" name="button2"

                    class="button" value="Button2" />

        form>

    head>

    html>

    Output:

    How to call javascript function on button click in php

    Let’s take a look with the GET or POST methods, as most developers use POST method due to privacy issues, the following example is based on POST method only:

    Program 2: This program uses isset() function to call PHP function.

    <html>

    <head>

        <title>

            How to call PHP function

            on the click of a Button ?

        title>

    head>

    <body style="text-align:center;">

        <h2 style="color:green;">

            GeeksforGeeks

        h2>

        <h4>

            How to call PHP function

            on the click of a Button ?

        h4>

        php

            if(isset($_POST['button1'])) {

                echo "This is Button1 that is selected";

            }

            if(isset($_POST['button2'])) {

                echo "This is Button2 that is selected";

            }

        ?>

        <form method="post">

            <input type="submit" name="button1"

                    value="Button1"/>

            <input type="submit" name="button2"

                    value="Button2"/>

        form>

    head>

    html>

    Output:

    How to call javascript function on button click in php


    How do you call a PHP function on the click of a button?

    Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

    How do you call a function after clicking the button?

    JavaScript – Call Function on Button Click.
    Method 1: Use addEventListener() Method. Get the reference to the button. ... .
    Method 2: Use onclick Attribute. Add onclick attribute to the button in HTML code. ... .
    Conclusion. In this JavaScript Tutorial, we learned how to call a function when button is clicked..

    How do you call a JavaScript function from a button in HTML?

    To call a JavaScript function from a button click, you need to add an event handler and listen for the click event from your

    How call JavaScript function in PHP if condition?

    php if (isset($_SESSION['loggedin'])) {$login='t';}else{$login='f';} ?>