Reputation: 23
I am a very beginner at php and I am having troubles. I need to call a php function, which prints a sentence on the screen, when an html button is clicked. I have writen the code below so far, but when I click the button the sentence does not appear. What's wrong with the code? Please help me! Thank you!
<?php
if (isset($_REQUEST['pdf'])) {
pdf();
}
?>
<input type="button" name="pdf" value="pdf" />
<?php
function pdf(){
echo "yesssssssss";
}
?>
Upvotes: 1
Views: 4809
Reputation: 26
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
Upvotes: 0
Reputation: 1788
Try something like this.
html
<form method="post" action="yourFile.php">
<input type="submit" name="someName">
</form>
yourFile.php
if(isset($_POST['someName']){
pdf();
}
Upvotes: 0
Reputation: 5335
You could try the following in your html code
<form name="pdf-form" method="post">
<button type="submit" name="pdf">Submit</button>
</form>
Upvotes: 0
Reputation: 5847
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).
Upvotes: 1