Raz*_*zor 138 html php forms button
我是PHP的新手,刚刚开始学习这门语言的基础知识.
我创建了一个名为functioncalling.php的页面,其中包含两个按钮:Submit和Insert.作为PHP的初学者,我想测试单击按钮时执行的功能.我希望输出出现在同一页面上.所以我创建了两个函数,每个按钮一个.functioncalling.php的源代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.";
}
?>
Run Code Online (Sandbox Code Playgroud)
这里的问题是在点击任何按钮后我没有得到任何输出.
任何人都可以告诉我我哪里错了?最早的回复将受到高度赞赏.先感谢您.
Roo*_*dra 77
是的,你需要ajax.有关详细信息,请参阅以下代码.
像这样更改你的标记
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
Run Code Online (Sandbox Code Playgroud)
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");
});
});
});
Run Code Online (Sandbox Code Playgroud)
在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;
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 62
按钮单击是__CODE__PHP而PHP __CODE__,但您可以通过使用来实现此目的__CODE__
$('.button').click(function() {
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
Run Code Online (Sandbox Code Playgroud)
在你的php文件中:
<?php
function abc($name){
// Your code here
}
?>
Run Code Online (Sandbox Code Playgroud)
Dav*_*idS 33
您应该使按钮调用相同的页面,并在PHP部分中检查按钮是否被按下:
HTML:
<form action="theSamePage.php" method="post">
<input type="submit" name="someAction" value="GO" />
</form>
Run Code Online (Sandbox Code Playgroud)
PHP:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
{
func();
}
function func()
{
// do stuff
}
?>
Run Code Online (Sandbox Code Playgroud)
Jas*_*n W 15
你不能调用PHP函数,比如单击HTML中的按钮.因为HTML在客户端,而PHP在服务器端运行.
要么你需要使用一些Ajax,要么像下面的代码片段一样.
<?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.";
}
?>.
Run Code Online (Sandbox Code Playgroud)
您必须发布表单数据,然后检查单击的相应按钮.
小智 12
要在输入中显示$ message:
<?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>
Run Code Online (Sandbox Code Playgroud)
要使用functioncalling.php作为外部文件,您必须以某种方式将其包含在您的html文档中
试试这个:
if($_POST['select'] and $_SERVER['REQUEST_METHOD'] == "POST"){
select();
}
if($_POST['insert'] and $_SERVER['REQUEST_METHOD'] == "POST"){
insert();
}
Run Code Online (Sandbox Code Playgroud)
小智 7
你可以在javascript或jquery ajax中这样写,并调用该文件
$('#btn').click(function(){
$.ajax({
url:'test.php?call=true',
type:'GET',
success:function(data){
body.append(data);
}
});
})Run Code Online (Sandbox Code Playgroud)
<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
}
}
?>Run Code Online (Sandbox Code Playgroud)
我被困在这个问题上,我用一个隐藏的字段解决了它:
<form method="post" action="test.php">
<input type="hidden" name="ID" value"">
</form>
Run Code Online (Sandbox Code Playgroud)
在value你可以添加任何你想添加。
在 test.php 中,您可以通过$_Post[ID].
| 归档时间: |
|
| 查看次数: |
904018 次 |
| 最近记录: |