1

I'm working on a site where you have a form that can add multiple persons, you start with one person to subscribe and can add more if you like.

I have written a PHP document that's made for 1 person, but I like it to change if I use multiple persons.

Is it possible to change my mail.php with the use of some jQuery code?

SCRIPT

var x = 0;

$('#addPlayer').on('click', function() {
            x++;
        });

if(x > 0) {
    for(i = x; i>=0; i--) {
        // add extra persoon to mail.php
    }
}
else {
    mail.php
}

if (i == 0) {
    use new mail.php
}

For every time a new player has been added (click on button) their is a count.

mail.php

<?php
$to = "example@mail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$voornaam = $_POST['first_name'];
$geboortedatum = $_POST['date'];
$telefoon = $_POST['phone_number'];
$type = $_POST['r0'];
$adres = $_POST['address'];
$postcode = $_POST['postcode'];
$huisnr = $_POST['number'];
$gemeente = $_POST['municipality'];
$prijs = $_POST['price'];

//getting the new inputs for a new player
//extra persoon
$naam1 = $_POST['spname1'];
$voornaam1 =$_POST['Firstname1'];
$mail1 = $_POST['spemail1'];
$geboortedatum1 = $_POST['datepicker1'];
$telefoon1 = $_POST['sptele1'];
$type1 = $_POST['r1'];



//getting the new inputs for a new player
$naam2 = $_POST['spname2'];
$voornaam2 =$_POST['Firstname2'];
$mail2 = $_POST['spemail2'];
$geboortedatum2 = $_POST['datepicker2'];
$telefoon2 = $_POST['sptele2'];
$type2 = $_POST['r2'];

$myArray = array( "nul", "volwassene", "student", "kind met 8 lessen", "kind zonder 8 lessen");
//array that selects the right value for type

$bericht = "Beste " . $voornaam . " " . $name . ",\n" . "We hebben uw inschrijving goed ontvangen. \nHier hebt u nog eens de inschrijving. Als er iets niet correct zou zijn laat het ons weten (info@kltc.be). \n\r\n\r" . "NAAM: " . $voornaam ." ". $name . "\n E-MAIL: " . $from . "\n GEBOORTEDATUM: " . $geboortedatum . "\n TELEFOON: " . $telefoon . "\n TYPE: " . $myArray[$type] . "\n ADRES: " . $adres . "\n POSTCODE: " . $postcode . "\n HUISNR: " . $huisnr . "\n GEMEENTE: " . $gemeente . "\n PRIJS: " . $prijs . "\n\r\n\r Gelieve dit bedrag te storten op het rekeningnummer: IBAN: BE59 0017 9225 2226 van KLTC HERENT vzw of BIC: GEBABEBB \n\r met de vermelding : 'lidmaatschap 2019 '-' X aantal personen / gezin / lessen, ... enz.' \n Uw inschrijving is pas definitief als zowel het lidgeld betaald is en we het inschrijvingsformulier ontvangen hebben. \n !Deelname aan tennislessen kan alleen, nadat je het lesgeld betaald hebt. \n\r\n\r Wij hier bij KLTC HERENT heten u alvast welkom! \n\r\n\r !Dit is mogelijk gemaakt door Tobias Van Rickstal!";

$subject = "Inschrijving";
$subject2 = "Inschrijving bij KLTC";

$headers = "From: $from";
$headers2 = "From: $to";

mail($to,$subject,$bericht,$headers) or die("Error!");

            echo "Bedankt," . $voornaam . $myArray[$type];
?> 

Should I use an extra message(bericht)? or how should I do this the best way possible?

I also had some issues with the mailing, I would like to send a confirmation to the one who has filled the form. so one to the receiver and one to the sender, but I had some problems with it. Or I get only one in my inbox or I get an empty mail (no information of my inputs were published in the mail.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • *"I also had some issues with the mailing.... no information of my inputs were published in the mail."* - About that. Reason being is that you didn't use the 2nd subject/headers to send mail. Now, if another issue is that you're unable to add more than one recipient, then the problem may lie in the HTML form, which hasn't been included in the original post. Have you seen the answer given below? Can you elaborate on what isn't working the way you'd like it to? – Funk Forty Niner Jan 21 '20 at 20:56
  • @TobiasvanRickstal Were any of the below answers helpful to you? If so, please select a "correct" answer *(by clicking the checkmark beside an answer)* to close the question, or provide an answer yourself and choose that as the correct answer. Otherwise, please add comments below one of the answers or edit your original question to add more detail so that additional answers can be provided. *Thanks!* – cssyphus Jul 14 '20 at 01:25

2 Answers2

4

PHP is a server-side language and runs on the webserver.

jQuery/javascript is a client-side language and runs on each user's computer, in the browser.

Think for a second where these two computers are - they could be miles apart.

In order for jQuery to communicate with PHP files, you must either use a FORM (which sends the user-typed data to the webserver PHP file), or use AJAX.

The FORM method is very old and can only SEND data. The AJAX method is much newer and can both SEND and RECEIVE. AJAX is how we talk to the server and receive a response back (e.g. look something up in a database, like username/password) and update the page without refreshing the entire page. (Forms always leave the current page and go to the processing (PHP) file.)

Mail.php will work by receiving data from an HTML form, filling the data into the variables, and sending the email. All the data must be filled-in by the user in the HTML form. When user presses SEND (or whatever you call your submit button) the user-supplied data will be sent to the back-end PHP file, plugged into the right PHP variables, and only then will PHP generate the email and send it.

So... adding a new user to your mail.php file is something that is done manually (by the user filling out the form again) and clicking Send. The form is erased after each Send.

See below for a simple introduction to forms and AJAX:

HTML Forms

AJAX Basics

jQuery AJAX Introduction - w3schools

cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

Re-reading your question, I think you could benefit by viewing these other StackOverflow answers that describe how to use jQuery AJAX to send data to back-end PHP file.

Should we use Forms or AJAX or both?
Do I still need a Form element when using a AJAX request?

An example much like what you are doing:
How to make an Ajax Contact Form

If you later want to store (database) information received:
update list with jquery & ajax

cssyphus
  • 37,875
  • 18
  • 96
  • 111