0

I have a simple registration form in html which is designed to "step by step" form. For that I am using javascript with ajax to call php which should send an email with values typed into inputs. But yesterday it sends email without that values and update to directory did not work. What happened today is that the same codes do not work anymore (and nothing was changed). Ajax only return error message which is put in alert.

Here's the codes:

HTML form:

<form id="regForm" action="doneregister.php" method="post" enctype="multipart/form-data">
            <div class="tab">
                <h1 class="reg">Kapitán tímu</h1><br>
                <p><input type="text" name="meno" placeholder="Meno a priezvisko" oninput="this.className = ''"></p>
                <p><input type="text" name="nick" placeholder="Nick v hre" oninput="this.className = ''"></p>
                <p><input type="email" name="email" placeholder="Email" oninput="this.className = ''"></p>
                <p><input type="text" name="steamID" placeholder="Steam ID" oninput="this.className = ''"></p>
            </div>
            <div class="tab">
                <h1 class="reg">Hráč 2</h1><br>
                <p><input type="text" name="meno2" placeholder="Meno a priezvisko" oninput="this.className = ''"></p>
                <p><input type="text" name="nick2" placeholder="Nick v hre" oninput="this.className = ''"></p>
                <p><input type="text" name="steamID2" placeholder="Steam ID" oninput="this.className = ''"></p>
            </div>
            <div class="tab">
                <h1 class="reg">Hráč 3</h1><br>
                <p><input type="text" name="meno3" placeholder="Meno a priezvisko" oninput="this.className = ''"></p>
                <p><input type="text" name="nick3" placeholder="Nick v hre" oninput="this.className = ''"></p>
                <p><input type="text" name="steamID3" placeholder="Steam ID" oninput="this.className = ''"></p>
            </div>
            <div class="tab">
                <h1 class="reg">Hráč 4</h1><br>
                <p><input type="text" name="meno4" placeholder="Meno a priezvisko" oninput="this.className = ''"></p>
                <p><input type="text" name="nick4" placeholder="Nick v hre" oninput="this.className = ''"></p>
                <p><input type="text" name="steamID4" placeholder="Steam ID" oninput="this.className = ''"></p>
            </div>
            <div class="tab">
                <h1 class="reg">Hráč 5</h1><br>
                <p><input type="text" name="meno5" placeholder="Meno a priezvisko" oninput="this.className = ''"></p>
                <p><input type="text" name="nick5" placeholder="Nick v hre" oninput="this.className = ''"></p>
                <p><input type="text" name="steamID5" placeholder="Steam ID" oninput="this.className = ''"></p>
            </div>
            <div class="tab">
                <h1 class="reg">Tím</h1><br>
                <p><input type="text" name="nazov" placeholder="Názov tímu" oninput="this.className = ''"></p>
                <p><input type="text" name="skratka" placeholder="Skratka tímu" oninput="this.className = ''"></p>
                <input type="file" name="file" required>
                <p>S odoslaním registrácie súhlasíte s <a href="9wk4l81Gmo2PYYVvGa1c.php" target="_blank">Pravidlami a podmienkami</a></p>
            </div>
            <div style="text-align:center;margin-top:40px;">
                <div style="float:right;">
                    <button type="button" id="firstBtn" onclick="redirect();">Späť na WindCup</button>
                    <button type="button" id="prevBtn" onclick="nextPrev(-1)">Späť</button>
                    <button type="button" id="nextBtn" onclick="nextPrev(1)">Ďalej</button>
                </div>
            </div>
            <div style="text-align:left;margin-top:50px;">
                <span class="step"></span>
                <span class="step"></span>
                <span class="step"></span>
                <span class="step"></span>
                <span class="step"></span>
                <span class="step"></span>
            </div>
        </form>

JS/jQuery and Ajax to call php:

function nextPrev(n) {
                var x = document.getElementsByClassName("tab");
                if (n == 1 && !validateForm()) return false;
                x[currentTab].style.display = "none";
                currentTab = currentTab + n;
                if (currentTab >= x.length) {
                    jQuery.ajax({
                        type: "POST",
                        url: "doneregister.php",
                        data: jQuery("#regForm").serialize(),
                        success: function(response) {
                            if (response) {
                                jQuery(location).attr('href', "www.windcup.sk/?register=1");
                            } else {
                                alert("Error");
                            }
                        }
                    });
                    return false;
                }
                showTab(currentTab);
            }

And php code:

if(isset($_POST)) {
    //kapitan
    $meno = $_POST["meno"];
    $nick = $_POST["nick"];
    $email = $_POST["email"];
    $steamID =  $_POST["steamID"];

    //hrac 2
    $meno2 = $_POST["meno2"];
    $nick2 = $_POST["nick2"];
    $steamID2 =  $_POST["steamID2"];

    //hrac 3
    $meno3 = $_POST["meno3"];
    $nick3 = $_POST["nick3"];
    $steamID3 =  $_POST["steamID3"];

    //hrac 4
    $meno4 = $_POST["meno4"];
    $nick4 = $_POST["nick4"];
    $steamID4 =  $_POST["steamID4"];

    //hrac 5
    $meno5 = $_POST["meno5"];
    $nick5 = $_POST["nick5"];
    $steamID5 =  $_POST["steamID5"];

    //Tim
    $nazov = $_POST["nazov"];
    $skratka = $_POST["skratka"];

    //poslanie emailu
    $destination = 'info@windcup.sk';
    $predmet = 'Registrácia tímu';
    $obrazok = basename($_FILES["file"]["name"]);
    $sprava = "Kapitán tímu: $meno Nick: $nick Email: $email SteamID: $steamID\n Hráč2: $meno2 Nick: $nick2 SteamID: $steamID2\n Hráč3: $meno3 Nick: $nick3 SteamID: $steamID3\n Hráč4: $meno4 Nick: $nick4 SteamID: $steamID4\n Hráč5: $meno5 Nick: $nick5 SteamID: $steamID5\n Tím: $nazov Skratka: $skratka Názov obrázku: $obrazok";
    $mail = mail($destination, $predmet, $sprava);

    //obrazok
    $target_dir = "style/img/teams/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $upload_ok = 1;
    $imageType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    $check = getimagesize($_FILES["file"]["tmp_name"]);

    if($check !== false) {
        //je to obrazok
        $upload_ok = 1;
    } else {
        //nie je to obrazok
        $upload_ok = 0;
    }

    if($_FILES["file"]["size"] > 500000) {
        $upload_ok = 0;
    }

    if($imageType != "jpg" && $imageType != "jpeg" && $imageType != "png" && $imageType != "gif") {
        $upload_ok = 0;
    }

    if($upload_ok == 0) {
        echo "Error";
    } else {
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            //nic
        } else {
            echo "Error";
        }
    }
}

Thanks for help!

  • Can you please provide the error message that the AJAX returns? – Johan Sep 25 '18 at 18:56
  • Sorry, Wrong information. It is returning error in alert. – Dávid Peprik Sep 25 '18 at 19:10
  • Ah, I noticed your code. It would help greatly if you could check what status code or error message you receive from the server is. Please check out [this example](https://stackoverflow.com/a/2800941/10400050) for inspiration. – Johan Sep 25 '18 at 19:16
  • I don't know what is happening with that but again I try that same code and now it's returning succes but without sending emails and uploading images... – Dávid Peprik Sep 28 '18 at 18:32

1 Answers1

0

First you need to define currentTab: var currentTab = 0; //might do the trick

Second, you didn't list entire JS, as I don't see showTab() function or initialization of nextPrev()