Ceck if variable exist

Hey, I´m a begginer at php developing. But I have knowledge in java. And it seem almoste the same.

My problem is that i dont know how to check if a variable exist.

I am creating a variable inside a if-statement. And outside the if-statement i want to check if the variable i created inside the if-statement exist.

Here is my code:
[php]<?php
if (isset($_POST[“submit”])) {
$subject = ‘LanCrate beställning!’;
$emaillc = ‘[email protected]’;

    if(empty($_POST['name'])){
        $errName = 'Du missade att fylla i ditt namn.';
    }
    if(empty($_POST['email'])){
        $errEmail = 'Du missade att fylla i din mejladress.';
    }
    if(empty($_POST['message'])){
        $errMessage = 'Du missade att fylla i ditt meddelande.';
    }
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    if(mail($emaillc, $subject, $message, $name)){
        
    }
}

?>[/php]

isset()

The real question is why? The answer is because your error checking logic is wrong. Each error should be an array. Then if the array is not empty, you display the errors to the user.

[php]$error =[];

if (empty($_POST[‘name’]))
{
$error[‘name’] = ‘Name Required’;
}

if (empty($_POST[‘email’]))
{
$error[‘email’] = ‘Email Required’;
}

if ($error)
{
foreach ($error as $value)
{
echo “$value
”;
}
}
else
{
// Send email
}[/php]

Also, checking for the name of a button to be submitted for your code to work will completetly fail in certain circumstances. The correct way is to use if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’)

Sponsor our Newsletter | Privacy Policy | Terms of Service