Function name must be a string

beginners
html5
mysql

#1

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:


#2

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.


#3

So yo want me to change $firstname = $POST to if ($SERVER['REQUEST_METHOD'] == 'POST') { ?

I did now I got this error


#4

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.


#5

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


#6

I think you forgot to attach the payment?


#7

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
?>