-4

I already created a contact form full of inputs, radio buttons and checkboxes but i cannot connect in via PHP to my email. It brings me to an error page.

I am gonna leave the code of my html contact form and my php code.

error

i am sorry for the badly formulated question, i am new to coding and i mean only good.

 <form action="send_email.php" method="POST">

        <div id="ciao">
            <h1 id="headlineForm">Контактна форма</h1>
            <input type="name" name="name" id="name" style="margin-left: 3cm; height: 1.5cm; width: 15cm; font-size: 25px;" placeholder="Име и Фамилия" required> <br>
            <input type="email" name="email" id="emailBox" style="margin-left: 3cm; height: 1.5cm; width: 15cm; font-size: 25px; margin-top: 1cm;" placeholder="Вашият имейл" required> <br>
            

<div class="container-fluid">


  <div class="form-group col-sm-8">
    <label for="dur" style="font-size: 25px;">Изберете вашия пакет:</label>
    <select id="dur" name="package" class="form-select" style="height: 1.5cm; font-size: 25px; margin-top: 0.5cm; width: 20cm;" >
        <option value="12">Нямам интерес към пакети в момента </option>
      <option value="24" >Нидерландия за студенти </option>
      <option value="36">Стартов пакет: Семейства с деца</option>
      <option value="48">Сам или Двойка: Нидерландия</option>
     
     
    </select>
  </div>

  <div class="form-group col-sm-8">
    <label for="myMultiselect" style="font-size: 25px;">Изберете вашите услуги:</label>
    <div id="myMultiselect" class="multiselect">
      <div id="mySelectLabel" class="selectBox" onclick="toggleCheckboxArea()">
        <select class="form-select"  id="dur2" style="height: 1.5cm; font-size: 25px; margin-top: 0.5cm; width: 20cm;" >
          <option>value</option>
          
        </select>
        <div class="overSelect"></div>
      </div>
      <div id="mySelectOptions">
        <label for="one"><input type="checkbox" id="one" onchange="checkboxStatusChange()" name="selectedServices[]" value="Регистрация и вадене на документи" /> Регистрация и вадене на документи</label>
        <label for="two"><input type="checkbox" id="two" onchange="checkboxStatusChange()" name="selectedServices[]" value="Здравна застраховка" /> Здравна застраховка </label>
        <label for="three"><input type="checkbox" id="three" onchange="checkboxStatusChange()" name="selectedServices[]" value="Детски надбавки" /> Детски надбавки </label>
        <label for="four"><input type="checkbox" id="four" onchange="checkboxStatusChange()"  name="selectedServices[]"value="Регистрация на МПС" /> Регистрация на МПС</label>
        <label for="five"><input type="checkbox" id="five" onchange="checkboxStatusChange()" name="selectedServices[]" value="Данъци и Такси" /> Данъци и Такси</label>
        <label for="six"><input type="checkbox" id="six" onchange="checkboxStatusChange()"  name="selectedServices[]"value="Студентски стипендии" /> Студентски стипендии</label>
        <label for="seven"><input type="checkbox" id="seven" onchange="checkboxStatusChange()" name="selectedServices[]" value="Държавни субсидии и надбавки" /> Държавни субсидии и надбавки</label>
        <label for="eight"><input type="checkbox" id="eight" onchange="checkboxStatusChange()" name="selectedServices[]" value="Данъчна система" /> Данъчна система</label>
        <label for="nine"><input type="checkbox" id="nine" onchange="checkboxStatusChange()" name="selectedServices[]" value="Права на работниците" /> Права на работниците</label>
        <label for="ten"><input type="checkbox" id="ten" onchange="checkboxStatusChange()" name="selectedServices[]" value=" Бизнес" /> Бизнес</label>
        <label for="eleven"><input type="checkbox" id="five" onchange="checkboxStatusChange()" name="selectedServices[]" value="Доставчици на комунални услуги" /> Доставчици на комунални услуги</label>
        <label for="twelve"><input type="checkbox" id="six" onchange="checkboxStatusChange()"  name="selectedServices[]"value="Банкови продукти" /> Банкови продукти</label>
        <label for="thirteen"><input type="checkbox" id="seven" onchange="checkboxStatusChange()" name="selectedServices[]" value="Транспорт" /> Транспорт </label>
        <label for="fourteen"><input type="checkbox" id="eight" onchange="checkboxStatusChange()" name="selectedServices[]" value="Нидерландски уроци и образование" /> Нидерландски уроци и образование</label>
        <label for="fifteen"><input type="checkbox" id="nine" onchange="checkboxStatusChange()" name="selectedServices[]" value="Здравна система " /> Здравна система </label>
        <label for="sixteen"><input type="checkbox" id="six" onchange="checkboxStatusChange()" name="selectedServices[]" value="Оглед на жилище" /> Оглед на жилище</label>
        <label for="seventeen"><input type="checkbox" id="seven" onchange="checkboxStatusChange()" name="selectedServices[]" value="Кариерни съвети и подбор позиции" /> Кариерни съвети и подбор позиции </label>
        <label for="eighteen"><input type="checkbox" id="eight" onchange="checkboxStatusChange()" name="selectedServices[]" value="Трудови данъци и декларации" />Трудови данъци и декларации</label>
        <label for="ninteen"><input type="checkbox" id="nine" onchange="checkboxStatusChange()" name="selectedServices[]" value="Трудови данъци: консултация и обработка" /> Трудови данъци: консултация и обработка</label>
      </div>
    </div>
  </div>

</div>


<textarea rows="4" cols="50" name="message" id="message" form="usrform" style="height: 8cm; width: 20cm; margin-left: 3cm; margin-top: 1.5cm; font-size: 25px; margin-bottom: 2cm; " placeholder="Съобщение"></textarea>
<input type="hidden" name="services" id="services" value="">

<button type="submit" class="button" id="btn" style="margin-left: -20cm; width: 10cm;">
    Изпрати
    </button>
</form>


    <?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $package = $_POST["package"];
    $selectedServices = isset($_POST["selectedServices"]) ? implode(', ', $_POST["selectedServices"]) : "No services selected";
    $message = $_POST["message"];

    $to = "livinginthenetherlands.nl@gmail.com"; 
    $subject = "New Contact Form Submission";
    $headers = "From: $email\r\n";

    $emailContent = "Name: $name\n";
    $emailContent .= "Email: $email\n";
    $emailContent .= "Package: $package\n";
    $emailContent .= "Selected Services: $selectedServices\n";
    $emailContent .= "Message:\n$message";

    if (mail($to, $subject, $emailContent, $headers)) {
        echo "Email sent successfully!";
    } else {
        echo "Failed to send email.";
    }
}
?>

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • There is no chance we can help you unless you tell us what the error is. Is it a PHP error? In which case we will need the exact error message, and a [mre] of the code which caused it. We cannot simply guess the problem, unfortunately. See also [ask]. You can [edit] your post to provide more details. Thanks. – ADyson Sep 01 '23 at 12:34
  • 1
    P.S. The HTML seems to be incomplete as well, e.g. the ` – ADyson Sep 01 '23 at 12:36
  • i reformulated my question , i am sorry i am new to stack overflow and new to coding in general – giorgio stringari Sep 01 '23 at 12:50
  • Thanks. A 405 error means your webserver did not accept the HTTP method used for the request. Since your form sends a POST, it means it did not accept the POST request specifically. I would expect this means that you are not running your code on a proper webserver which will accept POST requests, and therefore is likely cannot run PHP code either. To run PHP code locally for a web application, you need a server such as Apache or IIS. If you are (for example) using the built-in webserver provided by Visual Studio Code, this is only intended for simple static sites, it does not support PHP. – ADyson Sep 01 '23 at 12:57
  • so i could only check if it works once i upload it meaning find hosting and connect it to a domain? I there another way to check the result ? I am actually using VS Code Live server built in option to check the website i am building so i guess that was the problem. – giorgio stringari Sep 01 '23 at 13:01
  • No, I didn't say that. I said you need a webserver such as IIS or Apache. Both of those can be installed on your own computer (although IIS is Windows-only, of course). For PHP development, people commonly use a package such as XAMPP or Laragon which provides all the commonly-used tools - PHP, Apache, MySQL etc. and installs and configures it all for you automatically in one go. I don't know if you're following any kind of book or course about PHP, but really if it's aimed at beginners it should have covered some basic setup steps to get your environment ready. – ADyson Sep 01 '23 at 13:05
  • You can get localhost servers which are install, click and play, such as wamp and xampp for windows – Can O' Spam Sep 01 '23 at 13:05
  • I understand , i am just trying to see if the form works , my main goal is that , just t connect the form to the email. – giorgio stringari Sep 01 '23 at 13:07
  • Thanks for the further explanation about the webservers , i will more look into that but again i am trying to make the whole form work mainly because is the only php code i am using in the whole website. And i am using it strictly to connect responses to my email. – giorgio stringari Sep 01 '23 at 13:09
  • Ok. But you won't be able to make any PHP work until you have set that up properly. (The same would be true if you used some other server-side language such as nodeJS or ASP.NET/C# to send the email - you need a webserver which is configured to handle that language and respond to all the possible requests). – ADyson Sep 01 '23 at 13:11
  • Ok ok , thanks for letting me know , do you know any other way I could connect my contact form to my email instead? – giorgio stringari Sep 01 '23 at 13:14
  • P.S. If you want to test only if the HTML form itself is working, you can actually do that already. It doesn't matter if the data never reaches the server, you can still test if it sent the data you were expecting it to. Load your HTML form into the browser, then open the browser's Developer Tools (press F12 in most browsers), and go to the Network tool. Then, complete the form and submit it. In the Network tool you will see an entry appear for POST request. Click on the request to open it. Then go to the "payload" tab and you can see what data the form tried to submit. – ADyson Sep 01 '23 at 13:14
  • Thank you so much for the important information – giorgio stringari Sep 01 '23 at 13:14
  • Did that just now thanks that helps a lot – giorgio stringari Sep 01 '23 at 13:17
  • `do you know any other way I could connect my contact form to my email instead`...well, you always need some kind of back-end code - you cannot send email directly from a browser or a HTML document. So either you have your own server-side code which receives the form data and submits the email, or you pay to subscribe to some 3rd party mail service which has an API you could call from your page using JavaScript. (In that case, you are simply sending the POST request to someone else's backend code, and paying for the privilege). Not all such providers support client-side API requests, though. – ADyson Sep 01 '23 at 13:17
  • Last question, what would be your suggestion for me that i need only this contact form to send to my email ? I really only need this to be functional , i checked my html form and it works i really need a back end code that supports me in sending it to my email. What would be the best / easier solution? btw Thank you so much for the help – giorgio stringari Sep 01 '23 at 13:23
  • Sorry I don't understand the question really. If you want the form to work, and you have already written some PHP code to work with it, then the simplest thing would be to get your local PHP server working, and then test your code. What service are you using to actually send the email? I mean, PHP can generate the email, but then it must pass it to a mailserver for delivery. Having your own local mailserver is not very practical, so I guess you are using some 3rd party SMTP server - maybe Gmail or something like that? – ADyson Sep 01 '23 at 13:26
  • Unfortunately i really have only the 2 codes that are on the question above i don't have anything set up. I am new to the backend world, i think i should look into contact forms better and get a better knowledge because i am not using PHPmailer only my gmail. – giorgio stringari Sep 01 '23 at 13:30
  • Here's a good general guide to debugging email problems in PHP, which you really need to read I think: https://stackoverflow.com/a/24644450/5947043 – ADyson Sep 01 '23 at 13:32
  • If it seems too complex, you could also consider signing up for a service like SendGrid, or something similar. You can send small numbers of emails through them for free, normally. – ADyson Sep 01 '23 at 13:33
  • Thank you , i am really sorry for my low level of knowledge, thanks for the help – giorgio stringari Sep 01 '23 at 13:41

0 Answers0