可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am a rookie in PHP and have just started learning the basics of this language.
I have created a page called functioncalling.php that contains two buttons, Submit and Insert. As a beginner in PHP, I want to test which function is executed when a button gets clicked. I want the output to come on the same page. So I created two functions, one for each button. The source code for functioncalling.php is as follows:
<html>
<body>
<form action=\"functioncalling.php\">
<input type=\"text\" name=\"txt\" />
<input type=\"submit\" name=\"insert\" value=\"insert\" onclick=\"insert()\" />
<input type=\"submit\" name=\"select\" value=\"select\" onclick=\"select()\" />
</form>
<?php
function select(){
echo \"The select function is called.\";
}
function insert(){
echo \"The insert function is called.\";
}
?>
The problem here is that I don\'t get any output after any of the buttons are clicked.
Can anyone please tell me where exactly am I going wrong? Replies at the earliest will be highly appreciated. Thank you in advance.
回答1:
Button click is client side
whereas PHP is 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
}
?>
回答2:
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;
}
?>
回答3:
You should make the button call the same page and in a PHP section check if the button was pressed:
HTML:
<form action=\"theSamePage.php\" method=\"post\">
<input type=\"submit\" name=\"someAction\" value=\"GO\" />
</form>
PHP:
<?php
if($_SERVER[\'REQUEST_METHOD\'] == \"POST\" and isset($_POST[\'someAction\']))
{
func();
}
function func()
{
// do stuff
}
?>
回答4:
you cannot call php functions like clicking on a button from HTML. because HTML is on client side while PHP runs on server side.
Either you need to use some Ajax or do it like as in code snippet below.
<?php
if($_GET){
if(isset($_GET[\'insert\'])){
insert();
}elseif(isset($_GET[\'select\'])){
select();
}
}
function select()
{
echo \"The select function is called.\";
}
function insert()
{
echo \"The insert function is called.\";
}
?>.
You have to post your form data and then check for appropriate button that is clicked.
回答5:
To show $message in your input:
<?php
if(isset($_POST[\'insert\'])){
$message= \"The insert function is called.\";
}
if(isset($_POST[\'select\'])){
$message=\"The select function is called.\";
}
?>
<form method=\"post\">
<input type=\"text\" name=\"txt\" value=\"<?php if(isset($message)){ echo $message;}?>\" >
<input type=\"submit\" name=\"insert\" value=\"insert\">
<input type=\"submit\" name=\"select\" value=\"select\" >
</form>
To use functioncalling.php as external file you have
to include it somehow in your html document
回答6:
Try this:
if($_POST[\'select\'] and $_SERVER[\'REQUEST_METHOD\'] == \"POST\"){
select();
}
if($_POST[\'insert\'] and $_SERVER[\'REQUEST_METHOD\'] == \"POST\"){
insert();
}
回答7:
You can Write like this in javascript or jquery ajax and call the file
$(\'#btn\').click(function(){
$.ajax({
url:\'test.php?call=true\',
type:\'GET\',
success:function(data){
body.append(data);
}
});
})
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js\"></script>
<form method=\'get\' >
<input type=\"button\" id=\"btn\" value=\"click\">
</form>
<?php
if(isset($_GET[\'call\'])){
function anyfunction(){
echo \"added\";
// your funtion code
}
}
?>
回答8:
The onclick
attribute in HTML calls Javascript functions, not PHP functions.
回答9:
Use a recursive call where the form action calls itself. Then add PHP code in the same form to catch it. In foo.php
your form will call foo.php
on post
<html>
<body>
<form action=\"foo.php\" method=\"post\">
Once the form has been submitted it will call itself (foo.php
) and you can catch it via the PHP predefined variable $_SERVER
as shown in the code below
<?php
if ($_SERVER[\'REQUEST_METHOD\'] == \'POST\') {
echo \"caught post\";
}
?>
</form>
</body>
</html>
回答10:
I was stuck in this and I solved it with Hidden field
<form method=\"post\" action=\"test.php\">
<input type=\"hidden\" name=\"ID\" value\"\">
</form>
in value you can add whatever you want to add
in test.php you can retrieve value through $_Post[ID]
回答11:
Here is an example which you could use:
<html>
<body>
<form action=\"btnclick.php\" method=\"get\">
<input type=\"submit\" name=\"on\" value=\"on\">
<input type=\"submit\" name=\"off\" value=\"off\">
</form>
</body>
</html>
<?php
if(isset($_GET[\'on\'])) {
onFunc();
}
if(isset($_GET[\'off\'])) {
offFunc();
}
function onFunc(){
echo \"Button on Clicked\";
}
function offFunc(){
echo \"Button off clicked\";
}
?>