简体   繁体   中英

How to POST form data from dynamically created inputs?

I'm generating several dynamic inputs with a foreach. Each input gets its name and id from looping through an array from a text file.

The form data is then sent to another PHP page to perform some database queries with POST.

The problem I am facing is that each input value returns NULL.

I don't know what is going on, because when I look in the Web Console on the Network tab, I can see the Parameters are being collected.

array.txt

first_name
last_name
occupation
company_name
industry
city
country
countryCode
phone
email
address
stateProvince
postalZipeCode

form.php

//get array of names and ids for inputs
$array = explode("\n", file_get_contents('array.txt'));


//loop through the array
echo '<form method="POST" action="insert.php">';
foreach($array as $input) {
    echo '<label>'.$input.'</label>'
       . '<input type="text" id="'.$input.'" name="'.$input.'" required="required" class="form-control" placeholder="'.$input.'">';  
    echo '<br>';
}
echo '<input type="submit" value="Submit">';
echo '</form>';

Network Tab (web console)

在此处输入图片说明

insert.php

if (!empty($_POST)) {

    //get variables
    if (isset($_POST['first_name'])) {
        $first_name= $_POST['first_name']; //1.
    }

    if (isset($_POST['last_name'])){
        $last_name=$_POST['last_name']; //2.
    }

    if (isset($_POST['occupation'])) {
        $occupation=$_POST['occupation']; //3.
    }

    if (isset($_POST['company_name'])) {
        $company_name=$_POST['company_name']; //4 
    }

    if (isset($_POST['industry'])){
        $industry = $_POST['industry']; //5
    }

    if (isset($_POST['city'])) {
        $city = $_POST['city']; //6
    }

    if(isset($_POST['country'])){
        $country=$_POST['country']; //7
    }

    if (isset($_POST['countryCode'])) {
        $countryCode = $_POST['countryCode']; //8
    }

    if (isset($_POST['phone'])) {
        $phone = $_POST['phone']; //9
    }

    if (isset($_POST['email'])) {
        $email = $_POST['email']; //10
    }

    if (isset($_POST['address'])) {
        $address = $_POST['address']; //11
    }

    if (isset($_POST['stateProvince'])) {
        $stateProvince = $_POST['stateProvince']; //12
    }

    if (isset($_POST['postalZipeCode'])) {
        $postalZipeCode = $_POST['postalZipeCode']; //13
    }

    // insert into table 
    $insertProspectQuery = $conn->prepare("INSERT INTO users (first_name, last_name, occupation, company,industry,city,country,countryCode,phone,email,address,stateProvince,postalZipeCode) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
    $insertProspectQuery->bind_param('sssssssssssss',$first_name,$last_name,$occupation,$company_name,$industry,$city,$country,$countryCode,$phone,$email,$address,$stateProvince,$postalZipeCode);        
    $insertProspectQuery->execute();
    $insertProspectQuery->close();
    $ok = 1;

    } else {
        //handle error
    }

By outputing the variables with var_dump as suggested by MonkeyZeus, I have found that the issue was being created with empty white space at the end of each name variable.

This is a short summary of the var_dump : array(19) { ["first_name "]=> string(6) "John" ["last_name "]=> string(8) "Doe"[.......] .

I dont know what creates the white space in the name variables, probably the fact that the variables are being created from a text file and text file adds white space at the end of each line.

So since it should be ["first_name"] and NOT ["first_name "], $_POST was not able to recognize the variables being posted on insert.php .

This would never run in this wrong use case :

if(isset($_POST["first_name"])){
$first_name = $_POST["first_name"];
}

Because ["first_name"] != ["first_name "]

I solved the problem by trimming the names variable before using them in the foreach.

Solution quick trim :

form.php

//get array of names and ids for inputs
$array = explode("\n", file_get_contents('array.txt'));

//trim post variables
$trimmed_array=array_map('trim',$array);

//loop through the array

    echo '<form method="POST" action="insert.php">';
    foreach($trimmed_array as $input) {//pass the trimmed version of name variables
        echo '<label>'.$input.'</label>'
           . '<input type="text" id="'.$input.'" name="'.$input.'" required="required" class="form-control" placeholder="'.$input.'">';  
        echo '<br>';
    }
    echo '<input type="submit" value="Submit">';
    echo '</form>';

The issue is that $input in your foreach loop contains a space which it probably got from the array.txt file.

To avoid future issues you should just trim $input :

foreach($array as $input) {
    $input = trim($input);

    // Proceed as normal
}

You can choose to try and fix the data in array.txt but if the mistake presents itself in the future then your app will stop working again so it's best to just apply trim() per item retrieved from your file.

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