1

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?

5

5 Answers 5

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Initially i tried to work through ajax but I could not make that to work. Thank you so much...It worked. :-).
0

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"

Comments

0

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>

Comments

0

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
     }
     ?>

Comments

0
<?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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.