Jump to content

Uploading a CSV from a preview window


thenorman138

Recommended Posts

I currently have a site that works in the present state which is an html page showing an upload button where the user can insert a CSV file, then they can either hit Preview or Confirm. The preview button works, as it opens a new window that displays the csv file in an html table as a form (this is so that it's editable.) If they hit the confirm button, the CSV is successfully imported to the database table as it should be.

 

My new task: If the user edits the values of the CSV in the preview window, I need the updated version to submit to the database with the new values entered by the user. 

 

I attempted to get this working by adding the confirm button into the form in the preview window, like so:

 

if(isset($_POST['preview']))
{
ini_set('auto_detect_line_endings', true);
 
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX;  // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;
?><form method="post" action="/confirm" enctype="multipart/form-data"><?
echo '<table>';
 
/*WE WILL NEED TO QA CONDITIONS AND HIGHLIGHT IN RED HERE. ALSO NEED BORDER STYLINGS*/
 
if ($hasHeaderRow) {
   $headerRow = fgetcsv($handle);
   echo '<thead><tr>';
   foreach($headerRow as $value) {
       echo "<th>$value</th>";
   }
   echo '</tr></thead>';
}
 
echo '<tbody>';
 
$rowCount = 0;
while ($row = fgetcsv($handle)) {
   echo '<tr>';
   foreach($row as $value) {
       echo "<td><input type=\"text\" value=\"$value\"></td>";
   }
   echo '</tr>';
 
   if (++$rowCount > $maxPreviewRows) {
       break;
   }
}
echo '</tbody></table>';
 
}
?>
<button type="submit" name="submit" value="Confirm" >Confirm</button>

 

</form>
 
So, when I upload the CSV and hit preview, I get my preview window with the new confirm button but when I press the button the web page freezes and upon checking the database table, I could see that it was just uploading hundreds of entries with nothing in them, so this obviously isn't working. I think my problem is just in my form and file handling.
 
Ideally, I'd like to have my upload page only have the file upload section and the preview button, and the new preview window with the editable form would have the submit button, but I need to make sure it inserts any changes, not just the original CSV.
 
Below I attached screen shots that show the layout currently. The above code corresponds to the preview POST action, and below this I have the working code for the submit button, but I think the problem is that they're in the same file.

post-204074-0-46450700-1493653647_thumb.png

post-204074-0-39759100-1493653648_thumb.png

Link to comment
Share on other sites

If you are trying to debug this, why not simplify?  Stop the page after the preview and confirm steps and just upload the file to the server.  Then you can confirm that it gets to the server the way you want before you try to do the update.

Link to comment
Share on other sites

Well, when I submit from the initial upload page (screenshot 1) the file uploads and goes to the database successfully. Also, the preview window works perfectly from the upload. The only issue is moving the submit process from the upload page to the preview page so that it successfully inserts anything the user edited in the preview form. My form around the preview code currently has the action as "/confirm" which is the name of that page and I think that might be the problem.

 

The html upload form is a page called 'submit' and everything else is in a page called 'confirm' (which has the 'preview' code and the 'submit' code shown above). I think I'm just calling the form action incorrectly, but I don't know how it should be properly used.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.