PHP: isset( $_POST['submit'] ) funktioniert nicht.

B

BloodSteam

Guest
Moin,
Ich verstehe es nicht wieso es nicht funktioniert.

HTML:
Code:
               <section class="box">
                    <form method="POST" action="test.php">
                        <fieldset>
                            <legend>Registration</legend>
                            <div>
                                <span>Username</span>
                                <input type="text" name="username" required>
                            </div>
                            <div>
                                <span>Email</span>
                                <input type="email" name="email" required>
                            </div>
                            <div>
                                <span>Password</span>
                                <input type="password" name="password" required>
                            </div>
                            <div>
                                <button class="cancel">Cancel</button>
                                <input type="submit" name="submit" value="Signup">
                            </div>
                        </fieldset>
                    </form>
                </section>

test.php
Code:
if (isset($_POST['submit']){
   echo "submit";
}else if( empty($_POST['submit'] ){
   echo "empty";
}
^ Gibt mir "empty" aus und nicht "submit" wie gewollt.

Wenn Ich ['email'] anstatt 'submit' benutze, wird mir "submit" angezeigt.
Wieso? Bin Ich wirklich so dumm dass Ich es nicht verstehe?

@edit
Hab ein var_dump(); gemacht und es sieht so aus als würde mein Ajax call den "submit" nicht mitsenden.
Code:
array(3) {
  ["username"]=>
  string(9) "dwqwdqqwd"
  ["email"]=>
  string(17) "awdwda@wdqwdq.com"
  ["password"]=>
  string(6) "qwddwq"
}
 
Zuletzt bearbeitet:
Solltest du in deinem Ajax-Call serialize() (jQuery) für die Formfelder einsetzen, dann werden die type="submit"-Felder davon ignoriert, egal ob input oder button.
Prüfe halt alternativ auf HTTP_METHOD oder gleich, ob es ein Ajax-Call mit POST-Values ist. Oder füge noch ein action-Feld hinzu.

Bsp.:
PHP:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && isset($_POST['irgendwas']) ) {    
// Is ajax with post
}

... oder auf HTTP_X_REQUEST und HTTP_METHOD == 'POST'

usw.
 
Zuletzt bearbeitet:
Danke, Ich nutze pures Javascript, bin nicht so der jQuery fan. Am Anfang ist es gut aber jetzt, meh.


Code:
function ajax(file,method,formData)
{
    fetch(file, {
        method: method,
        body: formData
    }).then(function(res){
        return res.text();
    }).then(function(text){
        router(text);
    }).catch(function(error){
        console.error(error);
    })
};

Code:
let forms = document.getElementsByTagName('form');
for (var i = 0; i < forms.length; i++) {
    forms[i].addEventListener('submit', function(e){
        e.preventDefault();
        const formData = new FormData(this),
                method = this.getAttribute('method'),
                action = this.getAttribute('action');
        ajax(action,method,formData);
    });
}
 
Zurück