1

I have a form in which once filled in it sends an email to the user and should give a price based on what option they selected within the dropdown.

I have an if statement to see what option has been selected within the drop down. Within the if statement it should set the $Price variable to $Price = '£300';

The $Price variable is then added into the $body variable to be sent. The value of the $Price variable doesn't show within the email but when I take the $Price = '£300'; out of the if statement it is visible within the email.

I have made sure that the problem isn't the options dropdown selection as it works when the variable is placed within the echo 'Sent' , $Price;

PHP:

if ($Property_Value == '1to2') {
    $Price = '£300';
}


$name = $_POST['name'];
$email = $_POST['email'];
$from = 'From: BungeeDesign.com'; 
$to = $email;
$subject = 'Email Inquiry';
$Property_Value = $_POST['Property_Value'];

$body = "From: $name\n E-Mail: $email\n Price:\n $Price\n Selection:\n $Property_Value";



if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) { 
        echo 'Sent'; 
    } else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>

HTML:

 <div class="quoteForm">

  <form action="formAct.php" method="post">
    <li class="liFeilds">Name*:</li>
    <input type="text" name="name" placeholder="e.g. John Appleseed" required="yes" />
    <li class="liFeilds">Email*:</li>
    <input type="email" name="email" placeholder="e.g. email@email.com" required="yes" />
    <li class="liFeilds">Phone:</li>
    <input type="number" name="phone" placeholder="e.g. 07789236519" />
    <li class="liFeilds">Postcode*:</li>
    <input type="text" name="pc" placeholder="e.g. BN2 0AQ" required="yes" />
    <li class="liFeilds">Building Name/Number*:</li>
    <input type="text" name="addr" placeholder="e.g. 24 Lyndhurst Road / Palm Court" required="yes" />
    <li class="liFeilds">Property Value*:</li>
    <select class="propVal" name="Property_Value" required>
    <option value="1to2">£100,000 - £200,000</option>
    <option value="2to3">£200,001 - £300,000</option>
    <option value="3to4">£300,001 - £400,000</option>
    <option value="4to5">£400,001 - £500,000</option>
    <option value="5to6">£500,001 - £600,000</option>
    <option value="6to7">£600,001 - £700,000</option>
    <option value="7to8">£700,001 - £800,000</option>
    <option value="8up">Over £800,000</option>
  </select>
    <button name="submit" type="submit" value="Submit">Get Estimate</button>
  </form>

</div>
J.Rogers
  • 82
  • 1
  • 10
  • 1
    You appear to be checking `$Property_Value` before it's assigned. – Carcigenicate Jan 22 '17 at 15:17
  • You have to `$Property_Value = $_POST['Property_Value'];` before if statement. Otherwise, it will never set `$Price` to 300 because `$Property_Value` is null at that moment. – Manuel Jan 22 '17 at 15:18

1 Answers1

2

You are checking $Property_Value before setting any value to the variable

You have to first set the value, then check for any condition on that variable like:

$Property_Value = $_POST['Property_Value'];

if ($Property_Value == '1to2') {
    $Price = '£300';
}
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30