Call PHP function onClick HTML button

5

I want to call a PHP function that I have on the same page, in 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 show 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

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

Why does not it work? Is it by having two forms?

    
asked by anonymous 22.10.2014 / 15:51

1 answer

9

The code has several errors

1 - Remove the double quotation marks from tables and field names unless some name uses some reserved sqlite word in this case use: backstick ( '' ), double quotation marks ( "" ) or brackets %). documentation

change:

INSERT INTO \"event\"

and other occurrences for:

INSERT INTO event

1.1 - To specify values in text type fields (char, varchar, character etc) use single quotation marks ( [] ).

change:

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

To:

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

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

Required reading: variable scope

Change the function signature to receive arguments:

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

To:

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

3 - By leaving '' blank or adding action value the form is sent to itself.

    
22.10.2014 / 16:25