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