user3335796
user3335796

Reputation: 1237

Calling PHP function wth HTML button

I am developing website using php.I need to call php function using HTML Onclick event. How to do? Here I have given my code.

<?php

function insert() {
    echo "in insert ";
    $servername = "localhost";
    $username = "shwetharao";
    $password = "shwetha";
    $dbname = "semilab";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $uname = $_POST['usernameu'];
    $upass = $_POST['passwordu'];
    echo $uname;

    $sql = "INSERT INTO login (id, username, password)VALUES ('id','$uname','$upass')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $conn->close();
}
?> 

<!DOCTYPE HTML>
<head>
    <title></title>

</head>

<body>
    <div id="main">

        <div id="login">
            <h2>Create User Account</h2>

            <form action="" method="post">
                <br><br><br>
                <label>UserName :</label>
                <br><br>
                <input id="name" name="usernameu" placeholder="username" type="text">
                <br><br><br>
                <label>Password :</label>
                <br><br>
                <input id="password" name="passwordu" placeholder="**********" type="password">
                <br><br><br>
                <input name="button" type="button" value=" Create user " onclick="insert();">
            </form>
        </div>
    </div>

</body>
</html>

I need to call function insert()[which is php function] on clicking button.How to achieve this?

Upvotes: 1

Views: 763

Answers (5)

Jill
Jill

Reputation: 703

<?php
if($_GET){
if(isset($_GET['insert'])){
    insert();
}elseif(isset($_GET['select'])){
    select();
}
}

function select()
{
   echo "The select function is called.";
}
function insert()
{
   echo "The insert function is called.";
}

?>


<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />

===============================================

<?php
 if($_GET['button1']){fun1();}
 if($_GET['button2']){fun2();}

 function fun1()
 {
  //This function will update some column in database to 1
 }
function fun2()
{
//This function will update some column in database to 2
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <title></title>
 </head>
 <body>
  <button id="btnfun1" name="btnfun1" onClick='location.href="?button1=1"'>Update to 1</button>
 <button id="btnfun2" name="btnfun2" onClick='location.href="?button2=1"'>Update to 2</button>
 </body>
</html>

I did it using this code. Hope this helps.

Upvotes: 0

user4314601
user4314601

Reputation:

Just change

<input name="button" type="button" value="Create user" onclick="insert();">

to

<input name="button" type="submit" value="Create user">

And paste your php code inside this

<?php  if(isset($_POST['submit'])){
    //paste you php code here
     }
     ?>

Upvotes: 0

Priyank
Priyank

Reputation: 3868

PHP: Is only run by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).

HTML & Javascript: Is only run in someone's browser I'm assuming your file looks something like:

<?php
 function insert() {
  echo 'I just ran a php function';
  }
   if(isset($_POST['submit'])){
         insert(); 
     }
     ?>

 <!DOCTYPE HTML>
  <head>
 <title></title>

  </head>

 <body>
 <div id="main">

 <div id="login">
 <h2>Create User Account</h2>

 <form action="" method="post">
 <br><br><br>
 <label>UserName :</label>
  <br><br>
 <input id="name" name="usernameu" placeholder="username" type="text">
 <br><br><br>
 <label>Password :</label>
 <br><br>
 <input id="password" name="passwordu" placeholder="**********" type="password">
 <br><br><br>
<input name="submit" type="submit" value=" Create user" >
 </div>
 </div>

 </body>
 </html>

Upvotes: 0

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

you did two mistake first one is php tag at above, you wrote only "?" please change it to '<form action="" method="post"> inside of form you don't write action page name ..for example action = "myFile.php"

Upvotes: 0

I&#39;m Geeker
I&#39;m Geeker

Reputation: 4637

Try this

 <?php  if(isset($_POST['submit'])){
    insert(); 
    }
    ?>

<!DOCTYPE HTML>
  <head>
  <title></title>

  </head>

  <body>
  <div id="main">

  <div id="login">
  <h2>Create User Account</h2>

  <form action="" method="post">
  <br><br><br>
  <label>UserName :</label>
  <br><br>
  <input id="name" name="usernameu" placeholder="username" type="text">
  <br><br><br>
  <label>Password :</label>
  <br><br>
  <input id="password" name="passwordu" placeholder="**********" type="password">
  <br><br><br>
  <input name="submit" type="submit" value=" Create user" >
  </div>
  </div>

  </body>
  </html>

Upvotes: 1

Related Questions