如何使用HTML按钮单击调用PHP函数? [关闭]
I am a beginner with HTML and PHP and i am having problems with my code. all I need to do is to call a particular function on the button click in html. both the fuction and html are on same page. whenever i run this this doesn't generate the required output. I shall be very thankful for help. please help as soon as possiblemyfirstpage.php
我是HTML和PHP的初学者,我的代码出现问题。 我需要做的就是在html按钮上调用一个特定的函数。 功能和html都在同一页面上。 每当我运行它时,这不会产生所需的输出。 我非常感谢你的帮助。 请尽快帮助 myfirstpage.php p> div >
Like this?
<?php
if (isset($_POST['submit'])) {
someFunction();
}
function someFunction() {
echo 'HI';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<input type="submit" name="submit">
</form>
</body>
</html>
Although quite weird, this will work, you can use $_GET
if you want
You are communicating with two languages:
HTML
which is client-side
language
PHP
which is server-side
language
You can't run PHP
without communicating with server.
So to achieve that you need to write ajax
function in javascript
to make
$('button').click(function() {
$.ajax({
type: "POST",
url: "sum.php",
data: { input: "your values here" },
function (response) {
alert('sum performed sucessfully');
}
})
});
And your sum function
in sum.php
file
Try these code:
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}else{
alert("Your name has been successfully submitted!!");
return true;
}
}
<form name="myForm" action="/action_page_post.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</div>