Calling a PHP function from an HTML form in the same file
This cannot be done in the fashion you are talking about. PHP is server-side while the form exists on the client-side. You will need to look into using JavaScript and/or Ajax if you don't want to refresh the page.
test.php
<form action="javascript:void(0);" method="post">
<input type="text" name="user" placeholder="enter a text" />
<input type="submit" value="submit" />
</form>
<script type="text/javascript">
$("form").submit(function(){
var str = $(this).serialize();
$.ajax('getResult.php', str, function(result){
alert(result); // The result variable will contain any text echoed by getResult.php
}
return(false);
});
</script>
It will call getResult.php
and pass the serialized form to it so the PHP can read those values. Anything getResult.php
echos will be returned to the JavaScript function in the result
variable back on test.php
and (in this case) shown in an alert box.getResult.php
<?php
echo "The name you typed is: " . $_REQUEST['user'];
?>
NOTEThis example uses jQuery, a third-party JavaScript wrapper. I suggest you first develop a better understanding of how these web technologies work together before complicating things for yourself further.
How to call php function on html form submit - in the same page
A very simple way to do is to do following :
yourpage.php
<?php
if(isset($_POST)){
//data posted , save it to the database
//display message etc
}
?>
<form method="post" action="yourpage.php" >....
Html form and php function on same file
Use
action="<?=$_SERVER['PHP_SELF']?>"
$_SERVER
is a reserved variable -- see more here.
Call php function from class in another file with html button
You mean something like this:
require 'path_to_your_class';
if isset($_POST['submit']) {
$nabava = new nabava();
$vnosNarocila = $nabava->vnos_narocila();
}
How to run a PHP function from an HTML form?
The "function" you have is server-side. Server-side code runs before and only before data is returned to your browser (typically, displayed as a page, but also could be an ajax request).
The form you have is client-side. This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing.
Therefore, to run the function, the following flow has to happen:
- Server outputs the page with the form. No server-side processing needs to happen.
- Browser loads that page and displays the form.
- User types data into the form.
- User presses submit button, an HTTP request is made to your server with the data.
- The page handling the request (could be the same as the first request) takes the data from the request, runs your function, and outputs the result into an HTML page.
<?php
function addNumbers($firstNumber, $secondNumber) {
return $firstNumber + $secondNumber;
}
if (isset($_POST['number1']) && isset($_POST['number2'])) {
$result = addNumbers(intval($_POST['number1']), intval($_POST['number2']));
}
?>
<html>
<body>
<?php if (isset($result)) { ?>
<h1> Result: <?php echo $result ?></h1>
<?php } ?>
<form action="" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<p><input type="submit"/></p>
</body>
</html>
Please note:- Even though this "page" contains both PHP and HTML code, your browser never knows what the PHP code was. All it sees is the HTML output that resulted. Everything inside
<?php ... ?>
is executed by the server (and in this case,echo
creates the only output from this execution), while everything outside the PHP tags — specifically, the HTML code — is output to the HTTP Response directly. - You'll notice that the
<h1>Result:...
HTML code is inside a PHPif
statement. This means that this line will not be output on the first pass, because there is no$result
. - Because the form
action
has no value, the form submits to the same page (URL) that the browser is already on.
Call a PHP function only when form submitted
First, give your input a name:
<input name="submit" value="Update">
Then in your code, assuming PHP 7+:if ('Update' === ($_POST['submit'] ?? false)) {
wp_update_post($post, true);
}
How it works: when you click a button of type submit in the browser, the browser packages up the named input elements and sends them over the wire. The web server unravels the HTTP message and sends them to PHP, which makes them available in the associative array (aka dictionary) named $_POST
. (Or if the method is GET, then $_GET
). You can then check this array for the expected keys and their values.As an aside, you don't strictly need to name your button. You could also do:
if (count($_POST)) {
...
}
which asserts there is at least one key value pair in the posted data.You might also consider using var_dump('<pre>', $_POST)
as a diagnostic aid.
Finally, it's not clear to me where your $id
comes from, but that needs to be set properly, too.
Calling a particular PHP function on form submit
In the following line
<form method="post" action="display()">
the action should be the name of your script and you should call the function, Something like this<form method="post" action="yourFileName.php">
<input type="text" name="studentname">
<input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>
<?php
function display()
{
echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
display();
}
?>
HTML call PHP function on Submit without leaving the page
Yes, just don't useHTML call PHP function on Submit without leaving the page
action
and the form will be submitted to the current page.<form method="POST">
<!-- your input tags -->
</form>
To call a PHP function, check if it is submitted, then call the functionif(isset($_POST['user_input'])){
myFunction(); //here goes the function call
}
No, because the submit button is an HTML element and when you click on it, you will need JavaScript (e.g. via Ajax) to send it to the server (PHP) and then the PHP page on the server can execute the function needed.Is it possible to call a php function from inside a php script after pressing the submit button (the way it would normally execute in a page like the one below)?
You may use something like <form method="POST" onsubmit="myJSFunction()">
. The JavaScript function myJSFunction()
is called when you click on the submit button, using this function you may send the required data to the server (e.g. via ajax) and execute your PHP function there on the server.
Related Topics
Cannot Set Date.Timezone in PHP.Ini File
How to Refresh Select2 Dropdown Menu After Ajax Loading Different Content
Check and Return Duplicates Array PHP
Convert Multidimensional Objects to Array
PHP Considers Null Is Equal to Zero
In Magento 1.9.0.0 How to Enable Template Path Hint
How to Keep a PHP Session Active Even If the Browser Is Closed
Compare Object Properties and Show Diff in PHP
How to Sort a PHP Array by an Element Nested Inside
How to Extract Frames of an Animated Gif with PHP
Resource Interpreted as Image But Transferred with Mime Type Text/HTML - Magento
PHP Code Inside a Laravel 5 Blade Template
Laravel - Validate File Size When PHP Max Upload Size Limit Is Exceeded
Upload Progress Using Pure PHP/Ajax