John Davidson

How to check if html select option was not selected by user - php

0 comments
Message:


I have a contact form where I'm trying to catch if the user does not select an option "OR" forgets to select one from the select drop down list and is still set to the first option "Choose Option". In this case it should give the user an Alert "Please select option" which is called by flash('contact');.


But when I try to do if statements in the controller.php to check this, it is not giving the Alert error - rather is giving me my "Message Submitted(also flash('contact');)". And then I check my mailing test account and it is sending the email but shouldn't.


I have other if statements that check the inputs if(empty) and proper email type if(!filter_var) that are working, but I cannot figure out what I'm doing with the if to validate this.


Yes done some research as well. I also have few more options - just tried limiting the amount of code that wasn't necessary.


contactus.php


flash('contact');

<form method="post" action="./controllers/Contacts.php" class="messageForm">
<input type="hidden" name="type" value="contact">
<input type="text" name="yourName" onclick="showName()" placeholder="YOUR NAME">
<input type="text" name="yourEmail" onclick="showEmail()" onchange="showEmailAgain()" placeholder="EMAIL">
<select name="contactOptions">
<option value="Default" disabled selected>Choose Option</option>
<option value="Account Help">Account Help</option>
<option value="Other">Other</option>
</select>
<textarea id="yourMessage" type="text" name="yourMessage" onclick="showText()" placeholder="YOUR MESSAGE"></textarea>
</form>

./controllers/Contacts.php


 class Contacts {
public function sendMessage() {
//Sanitize POST Data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

//Grab info from user
$data = ['yourName' => trim($_POST['yourName']),
'yourEmail' => trim($_POST['yourEmail']),
'contactOptions' => trim($_POST['contactOptions']),
'yourMessage' => trim($_POST['yourMessage'])];

//Have tried

//if (isset($_POST['contactOptions'] == "Default")) {
// flash();
// redirect();
//}
//if ($_POST['contactOptions'] == "Default")) {
// flash();
// redirect();
//}
//if ("Default") {
// flash();
// redirect();
//}



//this "if" checks what option user selected that gets sent with
//email, is separate from my issue. There are some $this->mails before this if
if (isset(_POST['submit'])) {
$selected = $_POST['contactOptions'];
$this->mail->Subject = $selected;
}

$this->mail->Body = $subjectMessage;
$this->mail->send();

flash("contact", "Message Submitted", 'form-message form-message-green');
redirect("../contactus.php");
}
}

$init = new Contacts;

//Ensure that user is sending a post request
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
switch ($_POST['type']) {
case 'contact':
$init->sendMessage();
break;
default:
redirect("../index.php");
}
}else{
redirect("../index.php");
}


My Answer:
To check if the user did not select an option from the select dropdown list in your PHP code, you can use the following approach:

php
if ($_POST['contactOptions'] == 'Default') {
flash('contact', 'Please select an option', 'form-message form-message-red');
redirect('../contactus.php');
}


This code snippet checks if the value of `$_POST['contactOptions']` is equal to 'Default', which is the initial value of the dropdown list. If it is, it means the user did not select any option and the code will display the alert message and redirect back to the contact form.

Make sure to place this code before sending the email and displaying the success message to ensure that the validation is checked before proceeding with the form submission.

Rate this post

5 of 5 based on 8482 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us