从操作表单调用PHP函数 [英] Call PHP Function from Action Form

查看:109
本文介绍了从操作表单调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的PHP文件整合到一个普通的类文件中,但我不确定如何调用该函数而不是.php页面。如何在HTML页面的表单部分调用sendEmail()?






  HMTL 
< form action =form_class.phpid =frmAirportmethod =post>
全名:< span style =white-space:pre;>
< / span>< input name =nametype =text/>< br />< br />
电子邮件地址:< input name =emailtype =text/>< br />< br />
主题:< span style =white-space:pre;>
< / span>< input name =subjecttype =text/>< br />< br />
< textarea id =txtCommentscols =30rows =10>评论< / textarea>< br />
< input type =submitvalue =Send/>
< / form>






 < ;?php 
function addPerson(){
//在这里做东西
}

函数sendEmail(){
//在这里做一些东西

}
?>


解决方案

您无法直接调用PHP函数。但是,如果php文件被格式化为如何在这里显示只有两个函数,您可以通过一个标志作为POST字段,并有一个逻辑块根据该标志确定哪个函数调用。这并不理想,但它适用于您的目的。但是,这会强制页面刷新,并且您可能必须在函数返回后加载其他页面,因此您可能只需将其实现为ajax调用,以避免页面刷新。



根据您的评论编辑(使用jQuery Ajax):


我使用.ajax()jQuery函数。我的意图创建一个包含不同html表单的多个函数的php文件。 - tmhenson


在这种情况下,您可以让文件包含逻辑块,以将请求路由到正确的功能,最后返回结果。



我会尝试根据一些简单的jQuery为你想要做的事提供一个选项。假设你有这样的事情:



Java Script:

  $ (#button1)。click(function(e){
e.preventDefault(); //如果button是提交
ajax_route('ap');
} );
$(#button2)。click(function(e){
e.preventDefault(); //防止提交事件如果按钮是提交
ajax_route('se');
});

函数ajax_route(action_requested){
$ .post(ajax_handler.php,{action:action_requested},function(data){
if(data.length> 0 ){
alert(Hoorah!Completed the action requested:+ action_requested);
}
})
}
$ b

PHP(在ajax_handler.php中)

 < php 
//确保你有一个动作取
if(isset($ _ POST ['action'])
switch($ _ POST ['action']){
'ap':addPerson(); break;
case'se':sendEmail(); break;
default:break;
}
}

function addPerson(){
// do stuff here
}

function sendEmail(){
//在这里做一些事
}
?>


I want to consolidate my PHP files into a common class file, but I am not sure how to call the function instead of the .php page. How can I call sendEmail() in the form section of my HTML page?


HMTL
<form action="form_class.php" id="frmAirport" method="post">
Full Name: <span style="white-space: pre;"> 
</span><input name="name" type="text" /><br /><br />
Email Address: <input name="email" type="text" /><br /><br />
Subject: <span style="white-space: pre;">       
</span><input name="subject" type="text" /><br /><br />
<textarea id="txtComments" cols="30" rows="10">Comments</textarea><br />
<input type="submit" value="Send" />
</form>


<?php
function addPerson() {
//do stuff here 
}

function sendEmail() {
//do some stuff here 

}
?>

解决方案

You can't call PHP functions directly. However, if the php file is formatted how you displayed here with only two functions you could pass a flag in as a POST field and have a logic block determine which function call based on that flag. It's not ideal but it would work for your purposes. However, this would force the page to refresh and you would probably have to load a different page after the function returns so you may want to simply implement this as an ajax call to avoid the page refresh.

Edit based on your comment (Using jQuery Ajax):

I am using the .ajax() jQuery function. My intentions create one php file that contains multiple functions for different html forms. – tmhenson

Well in this case, you can have your file contain the logic block to "route" the request to the right function and finally return the result.

I'll try to provide an option for what you want to do based on some simple jQuery. Say you have something like this:

Java Script:

$("#button1").click(function(e){ 
    e.preventDefault(); // prevents submit event if button is a submit
    ajax_route('ap'); 
}); 
$("#button2").click(function(e){ 
    e.preventDefault(); // prevents submit event if button is a submit
    ajax_route('se'); 
});

function ajax_route(action_requested){ 
    $.post("ajax_handler.php", {action : action_requested}, function(data){
        if (data.length>0){ 
            alert("Hoorah! Completed the action requested: "+action_requested); 
        } 
    })
}

PHP (inside ajax_handler.php)

<?php
// make sure u have an action to take
if(isset($_POST['action']) 
    switch($_POST['action']){
        case 'ap': addPerson(); break;
        case 'se': sendEmail(); break;
        default: break;
    }
}

function addPerson() {
    //do stuff here 
}

function sendEmail() {
    //do some stuff here 
}
?>

这篇关于从操作表单调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆