Call PHP function in onClick HTML button

Asked

Viewed 15,614 times

5

I want to call a PHP function that I have on the same page, on the HTML button.

My PHP function:

if(isset($_POST['insert']))
{
    insert();
} 
function insert()
{
    $requete = "INSERT INTO \"event\" (
                title,
                start,
                end,
                description
                ) 
                VALUES
                (
                \"$txttitle\",
                \"$txtstart\",
                \"$txtend\",
                \"$txtdescription\"
                )";

    $resultat = $base_hndl->exec($requete);
    return $resultat;                                                               

    echo "feito";

}

And the HTML button:

<form method="post" action="insert()">
    <input type=submit required  name='insert' value=save>
</form>

I’m trying to do this. A new button, and a new form to display the data.

            function select($dir, $base){       
                $base_hndl  =   new SQLite3($dir.$base);                
                $requete    =   "SELECT id, title, start, end, description FROM \"event\" ORDER BY id DESC";    
                $resultat   =   $base_hndl->query($requete);     
                $affiche    =   $resultat->fetchArray();

                echo "<br><label><b>ID: $affiche[id]</b></label><br>";
                echo "<label><b>Title: $affiche[title]</b></label><br>";
                echo "<label><b>Start: $affiche[start];</b></label><br>";
                echo "<label><b>End: $affiche[end]</b></label><br>";
                echo "<label><b>Description: $affiche[description]</b></label><br>";

            }

if(isset($_POST['mostrar']))
            {
               select($dir, $base); //affiche évènements 
            } 

And the button

<input type=submit value=Mostrar name='mostrar' >

Why doesn’t it work? It’s because it has two forms?

  • In that case just leave the action of the empty form or action="#"

  • 1

    If PHP is in another file you have to do <form method="post" action="nome_do_ficheiro.php">, otherwise it is to do as the lost said.

  • That’s right, I’m using some text boxes in that same form, to call in the function, and insert into a db. Just call $txttitle. The input name is txttitle.

  • All elements(inputs, radios, selects etc) must be within the tag <form>, to redeem the values in php use $_POST['nome_do_input']. Has no exctract() thereabout?

  • 1

    Why the Javascript tag and the onClick reference?

1 answer

10

The code has several errors

1 - Remove double quotes from table and field names unless some name uses a reserved sqlite word in this case use: backstick(``), double quotes("") or brackets ([]). documentation

change:

INSERT INTO \"event\"

and other occurrences to:

INSERT INTO event

1.1 - To specify values in text type fields (char, varchar, Character etc) use only single quotes ('').

change:

   VALUES(\"$txttitle\",\"$txtstart\",

To:

   VALUES('$txttitle', '$txtstart',

2 - Scope, external/global variables cannot be accessed within a function they need to be passed as argument.

Must-read : scope of variables

Change function signature to receive arguments:

function insert(){
  //código....
}

for:

function insert($txttitle, $txtstart, $txtend, $txtdescription){
  //código....
}

3 - When leaving attribute blank action or add value # the form is sent to himself.

Browser other questions tagged

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