Yes, you need Ajax here. Please refer to the code below for more details.
Change your markup like this
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
jQuery:
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'ajax.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert("action performed successfully");
});
});
});
In ajax.php
<?php
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'insert':
insert();
break;
case 'select':
select();
break;
}
}
function select() {
echo "The select function is called.";
exit;
}
function insert() {
echo "The insert function is called.";
exit;
}
?>
Answer from Roopendra on Stack OverflowYes, you need Ajax here. Please refer to the code below for more details.
Change your markup like this
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
jQuery:
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'ajax.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert("action performed successfully");
});
});
});
In ajax.php
<?php
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'insert':
insert();
break;
case 'select':
select();
break;
}
}
function select() {
echo "The select function is called.";
exit;
}
function insert() {
echo "The insert function is called.";
exit;
}
?>
Button clicks are client side whereas PHP is executed server side, but you can achieve this by using Ajax:
$('.button').click(function() {
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
In your PHP file:
<?php
function abc($name){
// Your code here
}
?>
Videos
"Inserting PHP into an HTML button" doesn't make any sense. HTML is for the client's browser, PHP executes on the server.
Take a look at the PHP tutorial to see a very basic example of how to make a form interact with PHP. To crib from there:
action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
It is not possible. You should understand that PHP is purely a scripting language which runs only server side.
HTML is a client side markup language, which is sent from the server to the client. By the time that PHP has ran over your script, it has now left the server and is with the client. At which point no more PHP can execute without loading a new page again through another request.
PHP is serverside script while the HTML code runs on the client.
Serverside code runs before the client can see it, then its sent to the client and rendered by the browser.
So its not possible to call php code on the page without either reloading the page or by using javascript (specifically AJAX or similar).
Hey this one is pretty straight forward. I suggest you use a form to handle that problem. The button will submit the form and call the function. take a look at this
<!DOCTYPE html>
<?php
function pdf() {
echo "yesssss";
}
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
<?php
}
else{
pdf();
}
?>
</body>
</html>
I hope that answers your question and helps you
Having 2 files like you suggested would be the easiest solution.
For instance:
2 files solution:
index.html
(.. your html ..)
<form action="script.php" method="get">
<input type="submit" value="Run me now!">
</form>
(...)
script.php
<?php
echo "Hello world!"; // Your code here
?>
Single file solution:
index.php
<?php
if (!empty($_GET['act'])) {
echo "Hello world!"; //Your code here
} else {
?>
(.. your html ..)
<form action="index.php" method="get">
<input type="hidden" name="act" value="run">
<input type="submit" value="Run me now!">
</form>
<?php
}
?>
I know this question is 5 years old, but for anybody wondering how to do this without re-rendering the main page. This solution uses the dart editor/scripting language.
You could have an <object> tag that contains a data attribute. Make the <object> 1px by 1px and then use something like dart to dynamically change the <object>'s data attribute which re-renders the data in the 1px by 1px object.
HTML Script:
<object id="external_source" type="text/html" data="" width="1px" height="1px">
</object>
<button id="button1" type="button">Start Script</button>
<script async type="application/dart" src="dartScript.dart"></script>
<script async src="packages/browser/dart.js"></script>
someScript.php:
<?php
echo 'hello world';
?>
dartScript.dart:
import 'dart:html';
InputElement button1;
ObjectElement externalSource;
void main() {
button1 = querySelector('#button1')
..onClick.listen(runExternalSource);
externalSource = querySelector('#external_source');
}
void runExternalSource(Event e) {
externalSource.setAttribute('data', 'someScript.php');
}
So long as you aren't posting any information and you are just looking to run a script, this should work just fine.
Just build the dart script using "pub Build(generate JS)" and then upload the package onto your server.
You're missing an HTML tag. That's why you cannot see your button.
?>
<html>
<head>
</head>
<body>
<div id="center_button">
<button onclick="location.href='index.html'">Back to Home</button>
</div>
</body>
</html>
EDIT : remove this line :
header("Content-Type: application/json; charset=UTF-8");
Your test had broken quoting. You need to escape the double quotes rather than replacing them with single quotes, as otherwise your 'index.html' link won't work. As I now have a spare 5 mins...
echo("<button onclick=\"location.href='index.html'\">Back to Home</button>");
A string inside a string inside a string needs care;-)
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
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>