Printing form data with values in CodeIgniter

Recently, I tackled the challenge of creating a page with multiple forms in view using codeigniter. My goal is to implement a popup window for printing the data from these forms, complete with a print button. However, I am struggling with the process of transferring the form data into this popup window for printing within codeigniter. As a beginner, I would greatly appreciate any assistance or guidance on how to code for this particular issue.

Answer №1

Are you new to this? That's great! Here are some suggestions for you to experiment with. This method utilizes jQuery to fetch data from the server, place it in a dynamically generated iframe, and then print it out (perfect for CI, WP, or any other platform). Is that what interests you?

1.- Working with jQuery

// <input type="button" id="print_me" data-form_id="101">
$("#print_me").live("click", function() {
    var form_id = $(this).data("form_id"); // obtaining value 101
    $.post(
            // URL of the server where data is retrieved for printing
            window.location.href,
            {
                // variables sent to the server to retrieve data for printing
                form_id : form_id // sending form_id=101
                // etc
            },
            // output.contents contains the printable content
            function(output) {
                // creating an iframe
                var ifrm = document.createElement("iframe");
                ifrm.style.display = "none";
                document.body.appendChild(ifrm);
                setTimeout(function() {
                    // inserting content into the iframe
                    $(ifrm).contents().find("body").html(output.contents);
                }, 1);
                // printing when the iframe has loaded
                ifrm.onload = function() {
                    ifrm.contentWindow.print();
                }
            }, 'html');
});

2.- Processing on the server side

$form_id = $_REQUEST['form_id']; // receiving data here
ob_start();
// echoing HTML formatted data to print
$contents            = ob_get_contents(); // $contents holds all echoed HTML
ob_end_clean();
$output['contents']  = $contents; // wrapping it in $output
echo json_encode($output); // echoing JSON encoded data
exit; // exiting at this point is crucial!

You need to thoroughly investigate each of these steps. Once you reach your goal, you will have gained valuable knowledge. Best of luck!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason behind the perpetual hiding of the button?

<div class="app__land-bottom" v-if="isVisible"> <a href="#projects"> <img ref="arrowRef" id="arrow" src="./../assets/down.png" alt srcset /> </a> </div> When ...

What is the reason for the excessive width of the final column in this table?

I am currently working with a dataset that I am displaying using the handsontable library in the form of a table. One issue I am facing is that the last column of my table appears too wide even though I did not specify any width for it. Below you can see t ...

Currently, I'm attempting to figure out a way to create a CSS string in my MVC ASP.NET project. Any ideas or suggestions are greatly appreciated

I am currently exploring solutions to generate a CSS string in my ASP.NET MVC Web Application. Specifically, I am interested in creating this at the selector level. For instance, I might have a class named "TableFormat" with the following CSS properties: ...

Invoke the function when the user inputs text into the textbox

<textarea name="" id="" #text cols="30" (keydown)="WordCounter()" (change)="WordCounter()" rows="8" [(ngModel)]="user_text" placeholder="Type something here"></textare ...

Problem with inserting multiple rows in Codeigniter

I have been attempting to insert multiple values into a database using the insert_batch method, but I am encountering an error. Controller: public function add() { $data['title'] = $this->input->post('title'); ...

The value of ng-repeat list in AngularJS does not update when its value is changed by an ajax call

I am completely perplexed. Why doesn't my ng-repeat update when there is an ajax call that changes its value? I have searched through many questions and answers here, but none of them address the issue with the ajax call. Here is the HTML: <div c ...

Can one conceal the JavaScript code that has been written?

Can JavaScript (jQuery) codes be hidden from view? I have a program with several load() functions and I'm worried that everyone can see the page addresses. Is this a risk? Example code snippet: load('account/module/message/index.php'); ...

How can data on the server be fetched using an AJAX request in Express.JS and displayed on the client side?

I have been given the task of displaying this code on the client side. I've successfully loaded data/content on the page but it lacks a certain level of elegance. var express = require('express'); router = express.Router(), ...

Pass data from a JavaScript function to Objective-C in Xcode by returning an array

How can I retrieve an array of strings from Javascript in my iPhone app code? I am calling a Javascript function, and once it completes, I need to send the array of strings back to the Objective-C code. What is the best way to return the array and how can ...

Why is the Express validator failing to work with the posted value?

I have come across an issue with my current code. Everything is working fine, but I need to access req.body.type in the createValidationFor function. However, when I try to access it, the validation stops working and I can't figure out why. router.po ...

Verify the input in text fields and checkboxes using JavaScript

I'm in the process of creating a complete "validate-form" function, but there are still a couple of things missing: Ensuring that the Name field only allows alphabetical characters and white spaces Validating that at least one checkbox is selected, ...

What is the best way to update a deeply nested array of objects?

I have an array of objects with nested data that includes product, task, instrument details, and assets. I am attempting to locate a specific instrument by supplier ID and modify its asset values based on a given number. const data = [ { // Data for ...

Attach onClick event when employing a higher order component

Just getting started with React and I have a question. After following some blog posts, I was able to create a page using higher order components and componentDidMount to fetch data from an API and display it. Everything works smoothly and the code looks ...

What is the most effective method for achieving a desired outcome?

Is it a valid approach to get an action result, and if so, how can this be achieved? For instance, if there is a page with a form for creating entities, after successfully creating an entity, the user should be redirected to the entity's detail view. ...

Can we pass a search term parameter to getServerSideProps in Next.js?

I've been working on a project to organize all my notes and summaries in one place. The notes are stored as markdown files, converted to HTML dynamically in the backend (it's functioning well). Now, I want to add a search feature on the notes p ...

Utilize the JavaScript .send function to pass an array

Can an array be passed to a PHP page using the code below? If not, what modifications are needed in the code? function ajax_post(){ var hr = new XMLHttpRequest(); hr.open("POST", "get_numbers.php", true); hr.setRequestHeader("Content-type", "a ...

Tips on customizing the MSAL login commands for successful integration with numerous users

Successfully implemented the code to login using a username and password combination with Cypress for a webapp integrated with MSAL. In the end-to-end Testfile: describe('Login with MSAL as xxUsername', () => { beforeEach(() => { cy.Lo ...

Exploring TypeScript: Understanding how to define Object types with variable properties names

Having an issue with a React + TypeScript challenge that isn't causing my app to crash, but I still want to resolve this lingering doubt! It's more of a query than a problem! My goal is to set the property names of an object dynamically using va ...

Is there a way to dynamically alter the content depending on the index of the current slide using swiper.js?

Hi, I am new to utilizing the Swiper framework and so far, it has been one of the best sliders I have ever experienced. Currently, I am trying to establish a connection between 2 div tags - one tag holds the content of each slide while the other tag contro ...

Is there a way to extract a particular value from a JSON URL and transfer it to a JavaScript variable?

I am looking to extract current exchange rates from a JSON URL for implementation in a webpage. Specifically, I want to retrieve a particular exchange rate (such as the US Dollar) and store it in a variable for use within a JavaScript function. Below is a ...