1

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
  • You're from an asp.net webforms background, aren't you :-) Commented May 21, 2012 at 16:50
  • Yes how could you tell? It has been a tough adjustment at times Commented Jun 16, 2012 at 15:32
  • I could tell because you're expecting/desiring a tight relationship between your html template and your "codebehind". Commented Jun 16, 2012 at 17:59

3 Answers 3

2

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 
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I am using the .ajax() jQuery function. My intentions create one php file that contains multiple functions for different html forms.
Please see my updated answer for an example based on this info.
0

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.

2 Comments

Does this mean for every html form I have I will need to create a separate PHP file for? I am trying to avoid creating a new .php file for each form in my web site.
You can, but there's other ways to go, look into an MVC design pattern if you want a "single" php file.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.