Global variable $_POST empty

Website URL

carlosalbertoalegre.com.ar

Error Message

I have a contact form with phpmailer. Everything seems to work fine, except that the form values ​​can’t be fetched, so an empty email is sent to my account. When i test it locally it works but when i upload the site the email it sends is empty.

The code is this:

<?php

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require '../vendor/autoload.php';

$CONFIG = require("./config.php");

$to = $CONFIG["TO"];
$from = $CONFIG["FROM"];
$pass = $CONFIG["PASSAPP"];
$port = $CONFIG["PORT"];
$host = $CONFIG["HOST"];

$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);

$message = str_replace("\n.", "\n..", $message);

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();
    $mail->Mailer = "smtp";
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = "tls";
    $mail->Host       = $host;
    $mail->Username   = $from;
    $mail->Password   = $pass;
    $mail->Port       = $port;

    // DATOS MENSAJE
    $mail->setFrom($from);
    $mail->addAddress($to);
    $mail->isHTML(true);
    $mail->CharSet = "UTF-8";
    $mail->Subject = $subject;
    $mail->Body = "<strong>From:</strong> $name<br><strong>Email:</strong> $email:<br><hr>$message";

    $mail->send();
    $res = ["status" => true, "message" => "El mensaje ha sido enviado."];
    die(json_encode($res));
} catch (Exception $e) {
    $res = ["status" => false, "message" => "Error de servidor. Intentar más tarde."];
    die(json_encode($res));
}

I went to see what was happening
I see that it retrieves data from the form properly

and then it has redirection because it assumes that this is file, but it is not, it is dir
so that’s why it redirects and adds a slash / to the end

but you deleted the contact form and I don’t see anything anymore

3 Likes

Please note that POST requests, and any data submitted to it, do not persist across redirects. So the redirect adding the slash by default will cause the POST request to turn into a GET request.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.