简体   繁体   中英

Form submit is not posting dynamic created fields by jQuery

Actually I am generating input fields for HTML-Form dynamically. Now my problem is when submit the form nothing will be post to my php.

So when I am using

echo "<pre>"; print_r($_POST) ;  echo "</pre>";

in my php there is an empty array.

Here is my html-form:

<form method="POST" action="../php/saveTraining.php" id="trainingForm">
    <section id="mainCategory" class="hidden">
        <label><input type="checkbox" name="mainCat" id="Krafttraining">Krafttraining</label>
        <label><input type="checkbox" name="mainCat" id="Joggen">Joggen</label>
    </section>
    <section id="categoryKrafttraining" class="hidden">
        <label class="subCategory"><input type="checkbox" class="subCategory">Kurzhantel</label>
        <label class="subCategory"><input type="checkbox" class="subCategory">Bankdrücken</label>
    </section>
    <section id="categoryJoggen" class="hidden">
        <label><input type="number" name="joggingKm" id="joggingKm">Kilometer</label>
        <label><input type="number" name="joggingTime" id="joggingTime">Zeit</label>
    </section>
    <input type="hidden" id="saveTraining" name="sent" value="save">
</form>

you can ignore the 3rd section, because actually I am not working with it.

So here is the .js code where I create the additional fields:

('#'+ category +' :checkbox').off('change').change(function (event) {
        console.log("YUP");
        sectionID = 'sect'+ $(event.target.parentNode).text();
        if (this.checked){
            //append input fields
            $('#'+category).append( '<section id='+sectionID+'><h5>'+$(event.target.parentNode).text()+'</h5><label><input type="number" name="saetze" id="saetze">Sätze</label>' +
                                            '<label><input type="number" name="wiederholungen" id="wiederholungen">Wiederholungen</label></section>');
        }else{
            console.log("HIER");
            //delete not necessary input fields
            $('#'+sectionID).remove();

        }
    });

here is my .php connection :

<?php
$host= "127.0.0.1";
$user= "censored";
$pw= "censored";
$db= "censored";


$mysqli = new mysqli("localhost", "$db", "$user", "$pw", 3306);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") ";
}

echo $mysqli->host_info . "\n";

?>

and here is the .php that get called from form submit:

<?php
include ('datenbankConnect.php');

$saetze = $_POST['saetze'];
$wiederholungen = $_POST['wiederholungen'];
echo "werte:";
echo $saetze;
echo "<pre>"; print_r($_POST) ;  echo "</pre>";
?>

So when submitting the form I also get the following message from my php:

Notice: Undefined index:
Notice: Undefined index:

Maybe you could help me. It would be great!

So there is a problem with method="post" . When I am using method="get" and accordingly changing the php to GET everything works fine.

So could it be possible that POST is not working for PhpStorm's integrated server?


SOLVED

The problem was that PhpStorm's integrated server have some issues with POST method. With GET everything works fine.

And guys, don´t forget to give your input types names ;)

What is output of:

echo "<pre>"; print_r($_POST) ;  echo "</pre>";

If you don't have saetze in your post array you should inspect HTML code with firebug or chrome devTools to see if your javascript code is working properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM