0

I've recently been redesigning my contact form on my website. I will be using php to generate two separate emails--one will be a confirmation email sent to the customer, and the second email will be to me. This way, I can include the form details in the customer's confirmation, allowing them to verify that they entered the correct info. There's one relatively minor detail I'm trying to iron out.

My html form has a couple drop down selectors and the labels I've always used, which post to the php end, are shorthand that are okay for me, but not necessarily great for a customer facing email. I could edit the labels on the html side to be more customer friendly, but it's not ideal. I'd like to convert each shorthand string into two new strings, one for the customer email and one for when the form is sent to me.

For example, one of these drop downs asks whether the customer wants my all day availability, or only partial day availability. Currently they post "fullday" or "partial" to the php.

I'm thinking that a simple if/else function would be effective to define a new string. If you can excuse my lack of sytnax here, I'm thinking something along the lines of

if($availability = "fullday") {
    $lead_av = "You would prefer my exclusive availability for your event date.";
} else {
    $lead_av = "You don't mind if I have another event on the same day as long as there is plenty of time between them.";
}

I don't need these long statements when I receive the form, and would prefer my own copy of the form be something that I can consume in less than 2 seconds (which the shorthand allows me to do). Down the road, I want to reduce my copy of the form even more to render simple "1, 2, 3" codes, hopefully allowing me to extract the form data easily in a way that will let me import many forms elsewhere, all at once.

As you may be able to tell from this example, my understanding of php (or coding in general) is not exactly sophisticated. Appreciate any recommendations.

1
  • 2
    Note this is an assignment, not a comparison: if($availability = "fullday") You want to use the comparison operator == here instead. Commented Jul 27, 2018 at 18:09

1 Answer 1

1

This sort of thing gets messy when you have more than two or three values:

if ($availability == "fullday") {
    $lead_av = "You would prefer...";
} else {
    $lead_av = "You don't mind...";
}

You're generally better off using associative arrays for these sorts of equivalency lookups:

$availabilityStrings = [
    'partial' => "You don't mind...",
    'fullday' => "You would prefer...",
];
$lead_av = $availabilityStrings[$availability];

This way, you can very easily add/remove values without significantly changing code, and things don't get unwieldy when you have a dozen values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the recommendation. The specific instance I was considering would always have only two possible values, but it occurred to me that this suggestion could be very useful elsewhere on my form. This has been most helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.