Jump to content

Trouble collecting multiple textbox data in $_POST


methodman

Recommended Posts

I have been working on this application for 5 days now with no progress on 2 items. I have basically created an application that initially sets all status (project status) values to incomplete. You can then select which steps (project steps) you have completed and make notes for each individual item. Submit the form, and all completed items are updated and saved to the database with the current date and time. 

 

Things that work:

1 –inserting “incomplete” as a status for all items when the form is 1st viewed

2- displaying the form with all current status (complete or incomplete)

3 – collecting new status updates, INSERTing them into the database and displaying them on the refreshed page

4 – timestamp for completion date

 

Things that DON’T work

1  - New form data in the text field doesn’t update to the database.  I can only get my ‘<textfield>’ item to hold / display data after manually submitting information into the database using a mysql tool. So with this, I know that the ‘$updates[]’ array and the SELECT statement I am using works to retrieve the information from the DB. I am assuming the problem lies with the ‘$_POST’ function. If I echo the $update[‘comments’] array after form submission, It returns an error ( this was just a test to see if anything were traveling in that variable. )

 

2 – I think I may be querying the DB too often in this project. I think it will cause the program to run too slow. I don’t know how to fix that though! ALSO,

3 – I am wondering if anyone has an alternative suggestion to the query I have inside the ‘foreach’ statement for the ($update as $updates) array toward the bottom of the form. What you see there was the only working solution I could find that would place the timestamp on each individual item and not overwrite every date stamp in the DB.

ANY help would be appreciated since I am at a dead hault! Thanks

 

CODE:

<?php

// if the client's project is new insert incomplete for all items – WORKS!

$query = "SELECT * FROM status WHERE user_id ='$client_id'";

$data = mysqli_query($dbc, $query) or die('ERROR on QUERY 1');

if(mysqli_num_rows($data) == 0){

//first grab the list of step id's from the step table

$query = "SELECT step_id FROM step ORDER BY phase_id, step_id";

$data = mysqli_query($dbc, $query) or die('ERROR on QUERY 2');

$step_IDs = array();

while($row = mysqli_fetch_array($data)){

array_push($step_IDs, $row['step_id']);

}

 

// insert the "incomplete" value for all steps – WORKS!

foreach($step_IDs as $step_id){

$query = "INSERT INTO status (user_id, step_id, curr_status) VALUES ('$client_id', '$step_id', 2)";

mysqli_query($dbc, $query) or die('ERROR on QUERY 3');

}

}

 

// if the form has been updated, write the updates to the database

if(isset($_POST['submit'])){

// write the updates to the status table – DOES NOT WORK!! WILL NOT UPDATE ‘<textbox>’ data

foreach($_POST as $status_id => $curr_status) {

$query = "UPDATE status SET curr_status = '$curr_status' WHERE status_id = '$status_id' AND user_id='$client_id'";

mysqli_query($dbc, $query)or die('ERROR UPDATING DB, status and notes were NOT updated');

}

echo '<p>Your updates have been saved.</p>';

$comments = mysqli_real_escape_string($dbc, trim($_POST['comments']));

echo $comments; // show items held in array under notes – COMES BACK EMPT

}

 

// grab the status data from the database to generate the form

$query = "SELECT status.user_id AS user_id, status.status_id AS status_id, status.curr_status AS curr_status, " .

"step.name AS step_name, phase.name AS phase_name, status.notes AS notes, step_id " .

"FROM step " .

"INNER JOIN status USING (step_id) " .

"INNER JOIN phase USING (phase_id) " .

"WHERE user_id ='$client_id'";

$data = mysqli_query($dbc, $query) or die('ERROR grabbing the status data from the database to generate the form');

$updates = array();

while($row = mysqli_fetch_array($data)){

array_push($updates, $row);

}

mysqli_close($dbc);

 

//generate the update form by looping through the status array – DOES NOT COLLECT ‘<textbox>’ dataecho '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?user_id=' . $_GET['user_id'] . '">';

echo '<h3>Client update</h3><br />';

$phase = $updates[0]['phase_name'];

echo '<fieldset><legend>' . $updates[0]['phase_name'] . '</legend>';

foreach($updates as $update){

//only start a new fieldset if the phase has changed

if($phase != $update['phase_name']){

$phase = $update['phase_name'];

echo '</fieldset><fieldset><legend>' . $update['phase_name'] . '</legend>';

}

 

//display the step form field – WORKS!

echo '<label ' . ($update['curr_status'] == NULL ? 'class="error"' : '') . ' for="' . $update['status_id'] . '">' .

$update['step_name'] . ':</label>';

echo '<input type="radio" id="' . $update['status_id'] . '" name="' . $update['status_id'] . '" value="2" ' .

($update['curr_status'] == 2 ? 'checked="checked"' : '') . ' /><span id="statusInc">Incomplete</span> ';

echo '<input type="radio" id="' . $update['status_id'] . '" name="' . $update['status_id'] . '" value="1" ' .

($update['curr_status'] == 1 ? 'checked="checked"' : '') . ' /><span id="statusComp">Complete!</span><br />';

// HERES THE PROBLEMATIC <textbox>

echo '<textarea id="comments" name="comments" rows="3" cols="80">' . $update['notes'] . '</textarea><br />';

 

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('ERROR CONNECTING TO DATABASE');

$query = "INSERT INTO status (date) VALUES (NOW())";

$timestamp = mysqli_query($dbc, $query) or die ('ERROR with the timestamp query');         

}

echo '</fieldset>';

echo '<input type="submit" value="Save updates" name="submit" />';

echo '</form>';

?>

???

Hi methodman,

 

I modified the code to test it, though I found no problem. My suggestion would be to comment out the foreach ($_POST as $status_id => $curr_status) section, and put in print_r($_POST) for debugging purposes. This should tell you whether POST is actually picking up the textarea value. For me, I found no problem. Here is the code I used:

 

<?php

if(isset($_POST['submit'])){
   // write the updates to the status table - DOES NOT WORK!! WILL NOT UPDATE '<textbox>' data
   echo '<pre>';
   print_r($_POST);
   echo '</pre>';
}

//generate the update form by looping through the status array - DOES NOT COLLECT '<textbox>'data
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<h3>Client update</h3><br />';

echo '<fieldset><legend>Test</legend>';

   echo '<label>Step 1</label>';
   echo '<input type="radio" id="step1" name="step1" value="2" /><span id="statusInc">Incomplete</span> ';
   echo '<input type="radio" id="step2" name="step2" value="1" /><span id="statusComp">Complete!</span><br />';
   // HERES THE PROBLEM
   echo '<textarea id="comments" name="comments" rows="3" cols="80"></textarea><br />';
   
echo '</fieldset>';
echo '<input type="submit" value="Save updates" name="submit" />';
echo '</form>';
?>

Thanks for the tip on the print_r() function!

 

After using that function i can see everything being passed from the database to the form via the $updates[] array. Now what i am seeing is this:

Array

(

    [404] => 1

    [notes] =>

    [405] => 1

    [406] => 1

    [407] => 1

    [408] => 1

    [409] => 1

    [410] => 1

    [411] => 1

    [412] => 1

    [413] => 2

    [414] => 2

    [415] => 2

    [416] => 2

    [417] => 2

    [418] => 2

    [419] => 1

    [submit] => Save updates

)

all of the regular radio button items are passing through just fine but the outgoing 'notes' array slot is empty. I am also missing the 'notes' data for each consecutive row after the first.

 

This is the  incoming $updates[] array to my form: (there are 15 more items, this is just 1)

[4] => Array

(

            [0] => 2

            [user_id] => 2

            [1] => 408

            [status_id] => 408

            [2] => 1

            [curr_status] => 1

            [3] => CLIENT - Begin Generating website content in word document

            [step_name] => CLIENT - Begin Generating website content in word document

            [4] => Design Foundation

            [phase_name] => Design Foundation

            [5] => this is the 2nd user note

            [notes] => this is the 2nd user note

            [6] => 5

            [step_id] => 5

        )

'notes' this shows the data that was manually inserted in the DB.

 

I am thinking i may need another "dimension" on the array?? I'll keep working any additional help is much appreciated.

 

SO i have narrowed the problem down to the INSERT statement: When i print_r() the array data, i can now see the notes[] array i created to hold the various data from the textboxes.

 

// if the form has been updated, write the updates to the database
if(isset($_POST['submit']))
{
        foreach($_POST as $status_id => $curr_status)
   {
      $query = "UPDATE status SET curr_status = '$curr_status' " .
             "WHERE status_id = '$status_id' AND user_id='$client_id'";
      mysqli_query($dbc, $query)or die('ERROR UPDATING DB, status and notes were NOT updated');
   }
//----- BROKEN ITEM -----------------------------------------   
   foreach($_POST['notes'] as $notes ) // this statement works
   {
      echo '<p>' . $notes . '</p>'; // this echo will also display each item from the textboxes - all separately
      $query = "UPDATE status SET notes = '$notes' WHERE user_id='$client_id'"; // $client_id works, SET notes works, 
      mysqli_query($dbc, $query)or die('ERROR UPDATING DB, status and notes were NOT updated - 2'); 
   }
   print_r($_POST); // this will show every line of data collected from the textbox, so the array is carrying data and posting it here... so WHAT GIVES??  
   echo '<p>Your updates have been saved.</p>';
}
//---------- END OF BROKEN ITEM ----------------------------------   

// grab the status data from the database to generate the form - NEW 6/29
$query = "SELECT status.user_id AS user_id, status.status_id AS status_id, status.curr_status AS curr_status, " .
       "step.name AS step_name, phase.name AS phase_name, status.notes AS notes, step_id " .
       "FROM step " .
       "INNER JOIN status USING (step_id) " .
       "INNER JOIN phase USING (phase_id) " .
       "WHERE user_id ='$client_id'"; 

$data = mysqli_query($dbc, $query) or die('ERROR grabbing the status data from the database to generate the form');
$updates = array();
while($row = mysqli_fetch_array($data))
{
   array_push($updates, $row);
}
   
mysqli_close($dbc);

//generate the update form by looping through the status array
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '?user_id=' . $_GET['user_id'] . '">';
echo '<h3>Client update</h3><br />';
$phase = $updates[0]['phase_name'];
echo '<fieldset><legend>' . $updates[0]['phase_name'] . '</legend>';

//print_r($updates);

foreach($updates as $update)
{
   //only start a new fieldset if the phase has changed
   if($phase != $update['phase_name'])
   {
      $phase = $update['phase_name'];
      echo '</fieldset><fieldset><legend>' . $update['phase_name'] . '</legend>';
   }
   
   //display the step form field
   echo '<label ' . ($update['curr_status'] == NULL ? 'class="error"' : '') . ' for="' . $update['status_id'] . '">' . 
   $update['step_name'] . ':</label>';
   
   echo '<input type="radio" id="' . $update['status_id'] . '" name="' . $update['status_id'] . '" value="2" ' .
   ($update['curr_status'] == 2 ? 'checked="checked"' : '') . ' /><span id="statusInc">Incomplete</span> ';
   
   echo '<input type="radio" id="' . $update['status_id'] . '" name="' . $update['status_id'] . '" value="1" ' .
   ($update['curr_status'] == 1 ? 'checked="checked"' : '') . ' /><span id="statusComp">Complete!</span><br />';
   
   echo '<textarea id="notes" name="notes[]" rows="3" cols="80">' . $update['notes'] . '</textarea><br />'; //this is my new array to hold each textbox's data
   
   $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('ERROR CONNECTING TO DATABASE');
   $query = "INSERT INTO status (date) VALUES (NOW())";
   $timestamp = mysqli_query($dbc, $query) or die ('ERROR with the timestamp query');           

}
echo '</fieldset>';
echo '<input type="submit" value="Save updates" name="submit" />';
echo '</form>';
?>

 

Again, any help is appreciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.