gpt4 book ai didi

javascript - 使用 HTML 按钮调用 PHP 函数

转载 作者:行者123 更新时间:2023-11-29 00:05:34 26 4
gpt4 key购买 nike

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

<?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 函数]。如何实现?

最佳答案

试试这个

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

关于javascript - 使用 HTML 按钮调用 PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com