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):
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:
PHP (inside ajax_handler.php)