Call PHP Function from Action Form

2019-08-03 03:28发布

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 

}
?>

3条回答
欢心
2楼-- · 2019-08-03 04:15

I am not sure if I uderstand right, but to call function/method you can use hidden input:

<!-- form -->
<input type="hidden" name="action" value="sendEmail" />

// handle file
<?php
$actionObject = new Actions();
if (method_exists($actionObject, $_POST['action'])) {
    $actionObject->{$_POST['action']};
}
?>

AddPerson and sendEmail doesn't sounds they should be method of one class. Try to use classes only with method witch are related. To call method, you can define it as static, or you must make instance of class and then call method.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-03 04:23

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 
}
?>
查看更多
成全新的幸福
4楼-- · 2019-08-03 04:29

You CAN'T. PHP runs on the server only. You can use a javascript AJAX call to invoke a php script on the server, or use a regular form submission, but directly invoking a particular PHP function from client-side html? not possible.

查看更多
登录 后发表回答