1

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)

enter image description here

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
    }
  • What does `echo '
    '; var_dump($_POST): die();` tell you if you use it at the top of `insert.php`?
    – MonkeyZeus Apr 01 '19 at 15:24
  • "The problem I am facing is that each input value returns NULL" — How do you know? Your PHP assigns the values to variables and then does nothing more with it. – Quentin Apr 01 '19 at 15:25
  • `echo ''` — A label is useless unless you put a form control inside it or give it a `for` attribute. – Quentin Apr 01 '19 at 15:25
  • @Quentin : I do an insert in the database. And the rows all return NULL. Meaning, NULL values are getting inserted on the table. –  Apr 01 '19 at 15:27
  • Then there is probably something about the insert that is wrong. You need to provide an [mcve]. – Quentin Apr 01 '19 at 15:27
  • Have you [**checked your PHP error logs**](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel)? – Martin Apr 01 '19 at 16:02
  • @Thisisme You should really consider providing the information which experienced programmers request of you. Add the `var_dump();` output which I requested and your life might just get a little easier. – MonkeyZeus Apr 01 '19 at 16:02
  • @MonkeyZeus `output which I requested and your life might just get a little easier` hahah I imagine you saying that while casually slapping an iron bar against your other hand, mafia style `;-)` – Martin Apr 01 '19 at 16:03
  • To verify what you have in php, try to put a phpinfo(); at the start of your insert.php script. Then you'll get a dump of the $_POST array. – F.Madsen Apr 01 '19 at 16:12
  • @F.Madsen that is entirely inappropriate. It's overkill – Martin Apr 01 '19 at 16:15
  • @MonkeyZeus: I'm debugging right now and I have found the error in the var_dump. The issue is being created with empty white space at the end of each variable. This is 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? It should be `["first_name"]` and NOT `["first_name "]`. How does this happen? –  Apr 01 '19 at 16:19
  • Lol at the iron bar imagery. Glad I could help you to solve your own issue. I provided an answer slightly different to yours if you wanted to accept it :) – MonkeyZeus Apr 01 '19 at 17:02

2 Answers2

1

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>';
0

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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77