用HTML按钮调用PHP函数

用HTML按钮调用PHP函数

本文介绍了用HTML按钮调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php开发网站.我需要使用HTML Onclick事件调用php函数.怎么做?在这里,我给出了我的代码.

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>

我需要在单击按钮时调用函数insert() [这是php函数].如何实现?

I need to call function insert()[which is php function] on clicking button.How to achieve 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>

这篇关于用HTML按钮调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 19:00