skip to Main Content

I want to have a php file that executes the function instead of submitting to a separate file.

I know I can name it:
function sibmitForm() {
…. code below …
}

But that doesn’t help me execute the function.

This is what I have now:

<form method="post" action="forms/comment.php">
<?php
$webmaster_email = "[email protected]";
$thankyou_page = "http://antarctica-ufo.com/forms/thank_you.html";
$error_page = "http://antarctica-ufo.com/forms/error.html";
$name = $_REQUEST['name'] ;
$comment = $_REQUEST['comment'] ;

if (empty($name)) { $name='Anonymous';  }
$msg = "rnName: " . $name . "rnrnComment: " . $comment . "rn";  

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    mail( "$webmaster_email", "Comment on Antarctica-UFO.com", $msg );
    header( "Location: $thankyou_page" );
}
else 
    header ( "Location: $error_page" );
?>

2

Answers


  1. To execute the function within the PHP file instead of submitting to a separate file, you can modify your code as follows:

    $webmaster_email = "[email protected]";
    $thankyou_page = "http://antarctica-ufo.com/forms/thank_you.html";
    $error_page = "http://antarctica-ufo.com/forms/error.html";
    
    function submitForm() {
        $name = $_REQUEST['name'] ;
        $comment = $_REQUEST['comment'] ;
    
        if (empty($name)) {
            $name = 'Anonymous';
        }
    
        $msg = "rnName: " . $name . "rnrnComment: " . $comment . "rn";
    
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            mail("$webmaster_email", "Comment on Antarctica-UFO.com", $msg);
            header("Location: $thankyou_page");
            exit(); // Exit the script after redirecting to prevent further execution
        } else {
            header("Location: $error_page");
            exit(); // Exit the script after redirecting to prevent further execution
        }
    }
    
    // Call the function
    submitForm();
    ?>
    

    In this modified code, the submitForm() function encapsulates the logic for processing the form submission. The function is defined at the beginning of the PHP file, and then it is called at the end of the file. This way, the function is executed immediately when the PHP file is loaded.

    Note the addition of exit() after the header() function calls. This is important to prevent any further execution of the script after the redirection, as headers need to be sent before any output.

    Login or Signup to reply.
  2. Too long for a comment but I believe what you are trying to ask is:

    How do I execute a function from the browser or client-side code?

    PHP is a server-side language, it doesn’t run in the browser. Research client-side versus server-side for a better understanding. In order to execute the server-side code to send an email, you would need to invoke a web request to the server hosting the PHP files. This server will execute the code and provide an HTTP response. This response could contain headers (used for redirects) and a response body (HTML rendered in the user’s browser).

    What you probably want to research is AJAX requests which allow you to invoke web requests from Javascript without changing the page that your user is on. This involves orchestration between HTML, Javascript, and PHP. When you click the button, you execute a Javascript function that sends an HTTP request to your web server and manipulates the HTML rendered in the user’s browser.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search