user10021033
user10021033

Reputation:

How to post an extra value when submitting a form in PHP?

As the title says, I need to post an extra value when submitting a form in PHP, but I get this value from a GET method.

index.php

<!DOCTYPE html>
<html>
    <head>
        <style> 
            //Form styles
            input[type=text] {
                width: 10%;
                padding: 12px 20px;
                margin: 8px 0;
                box-sizing: border-box;
                border: 1px solid #555;
                outline: none;
            }

            input[type=text]:focus {
                background-color: lightblue;
            }
            input[type=submit] {
                padding: 12px 20px;
                box-sizing: border-box;
                border: 1px solid #555;
                outline: none;
            }

            input[type=submit]:hover {
                background-color: lightblue;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        //Form
        <form action="sendMail.php" method="post">
            <label for="fname">4 last numbers:</label>
            <br>
            <input type="text" name="dgCard">
            <br>
            <label for="lname">Card name:</label>
            <br>
            <input type="text" name="nameCard">
            <br>
            <input type="submit">
        </form>
    <?php
       $id = $_GET["idPayment"]; //I need to send this variable when posting the form
    ?>
    </body>
</html>

sendMail.php

<?php
    $email_to = "...";
    $email_from = "...";
    $email_headers = "MIME-Version: 1.0\r\n";
    $email_headers.= "From: FROM NAME <...>" . "\r\n";
    $email_headers.= "Content-Type: text/html; charset=UTF-8" . "\r\n";
    $email_headers.= "Reply-To: ..." . "\r\n";
    $email_subject = "...";
    $email_body = "4 last numbers: ".$_POST["dgCard"]."<br>Card name:".$_POST["nameCard"].
    "<br><br><a href='localhost:3001/payment?id= //I need to access to id variable here'>Payment details'</a> ";

    // Send Email
    $mailerResult = @mail($email_to, "$email_subject", $email_body, $email_headers, '-f ' . $email_from);

    // Check For Errors
    if($mailerResult) {

        echo "Mail Sent!";

    } else {

        echo "Error Sending Email!" . "<br><br>";

        print_r(error_get_last());
    }
?>

Is there any way to achieve this?

Upvotes: 0

Views: 57

Answers (1)

Alberto
Alberto

Reputation: 12939

Yes you can, using a hidden input:

<body>
    <form action="sendMail.php" method="post">
        <input type="hidden" name="the-name-you-need" value="<?php echo $id = $_GET["idPayment"]; ?>">
        ...
    </form>

</body>

Upvotes: 3

Related Questions