Function name must be a string

I am prety much beginner to php but just wondering why I get error here. Im glad if you helps me out :wink:

<?php
{
$to = 'sulkycsgo@gmail.com';
    $firstname = $_POST["fname"];
    $email= $_POST["email"];
    $text= $_POST["message"];
    $phone= $_POST["phone"];
}


    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "From: " . $email . "\r\n"; // Sender's E-mail
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $message ='<table style="width:100%">
        <tr>
            <td>'.$firstname.'  '.$laststname.'</td>
        </tr>
        <tr><td>email: '.$email.'</td></tr>
        <tr><td>phone: '.$phone.'</td></tr>
        <tr><td>Text: '.$text.'</td></tr>
        
    </table>';

    if (@mail($to, $email, $message, $headers))
    {
        echo 'The message has been sent.';
    }else{
        echo 'failed';
    }

?>

SYNTAX ERROR:
https://gyazo.com/d111f372a09a4144ddb45faaa1743279

You are assigning POST values to variables, but you do not have any code to deal with what happens when there are no POST values to assign.

It could be done more than one way. Typically the assignment of POST values are wrapped with a

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

conditional and the assignment, and indeed the entire “mail” code is not run, if there is no POST.

There are a few other issues with the code, but doing that will stop the undefined errors and allow you to continue developing the script.

So yo want me to change firstname = _POST to if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) { ?

I did now I got this error

https://gyazo.com/24e7b18b79ae2270d1f27f5d8b71a66e?token=5eeaf8c0df65860628eac81977fb383a

No. What I meant by “wrap” is to put the assignments, and probably the entire script (unless for some reason you want to mail() regardless), inside an if conditional. eg. in pseudo code

if ($some_condtion === true) { 
// run this code 
} else { 
// run this code instead 
} 

The “$some_condition === true” in this case being the test for a POST array.

Testing and dealing with the various POST array members will still need to be done within the condional.

1 Like

Lol man If i Just do like this I upload the file here then you maybe can change whats need to change for the php script to work. Because im a completly noob and I so bad at english

https://we.tl/tYhYAP2HW6

I think you forgot to attach the payment?

2 Likes

Sorry, but that is not possible. As I posted previously there are several ways to write the code, and the decisions about how to go about it are up to you.

Maybe a modified version of my previous example will be more helpful?

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // new line added before your existing code 
// ALL of your existing code here 
} // new line added after your existing code
?>

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