• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a form

#1
[eluser]ealonw[/eluser]
Hello all! Im an admin, Linux geek turned coder.. I took to PHP because I work with Mysql on Linux all the time. What Im trying to do is create a simple form in codeigniter that includes: couple input boxes checkboxes and radio button. Can someone give me a good guide on how thats done. I want to write the results to a file and echo a message after the form is complete. I can do this with just PHP scripting but not the codeigniter way. Can anyone help me along. This would be my first time coding using CI and Im really interested in learning.

Functions:
1. Fill out the form
2. Write the results to text file
3. Echo message after form complete

Attached is the HTML form in TEXT version

#2
[eluser]Pascal Kriete[/eluser]
I don't see an attached file. I highly suggest you watch the video tutorials to get a real feeling of how CI works.

That said, most programs use a database instead of a file, but we'll make do. If you don't understand something make sure to check the User Guide first. You'll be in there a lot, so might as well get some practice using it.

As always, absolutely untested. Let me know if you have trouble.

1. Make a new file in the Controllers folder and call it test.php.
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function Test()
    {
        parent::Controller();
    }
    
    // --------------------------------------------------------------------

    /**
     * Default Controller Function
     *
     * @access    public
     */
    function index()
    {      
        //Load url helper
        $this->load->helper('url');

        $this->load->view('testform');
    }
    
    // --------------------------------------------------------------------

    /**
     * Handle Form Submittion
     *
     * @access    public
     */
    function submit()
    {
        // Get the name field
        $name = $this->input->post('name');
        
        // Load the file helper
        $this->load->helper('file');
        
        // Write to the file

        if ( ! write_file('./path/to/file.php', $name))  // Change this - obviously
        {
              $data['message'] = "An error occurred!";
        }
        else
        {
             $data['message'] = "Successfully saved your name.";
        }
         $this->load->view('feedback', $data);
    }
}

2. Now we need views. Create a file called testform.php in your views folder:
Code:
<html>

<head>
    <title>Whois</title>
</head>

<body>

<form action="<?=site_url('test/submit');?>" method="post" accept-charset="utf-8">
    <p>Name: &lt;input type="text" name="name" value="" id="name"&gt;</p>
    <p>&lt;input type="submit" value="Hit it!"&gt;</p>
&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;

3. And another view called feedback.php :
Code:
&lt;html&gt;

&lt;head&gt;
    &lt;title&gt;Feedback&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<h3>&lt;?= $message ?&gt;</h3>

&lt;/body&gt;

&lt;/html&gt;

4. In your config/config.php file change the base_path variable to fit your setup.

Now point your browser at: /cidirectory/index.php/test

Here's some references to help you understand what I did:
Controllers
Views
Url Helper
File Helper

For more advanced forms you will also want to take a look at:
Validation


I know it seems like a lot at first, but just watch those tutorials, play around a bit and you'll be up and running in no time. Remember the forum search and userguide - you'll usually find something.

Welcome to CodeIgniter.

#3
[eluser]ealonw[/eluser]
Hmmm that works! Good test! But it wont append to the file using
if ( ! write_file('/var/www/html/write/savefile.php', $name, FILE_APPEND)Wink

#4
[eluser]Pascal Kriete[/eluser]
That's because that parameter doesn't exist (form helper). I believe you're used to the file_put_contents function. That's cool, you can use any php function. In this case though, if you want to remain php4 compatible you should load the compat helper first.

Code:
// PHP 4 support for portability
$this->load->helper('compatibility');

// Drop that stuff in
if( ! file_put_contents('path/to/file', $name, FILE_APPEND) )

#5
[eluser]ealonw[/eluser]
Nvermind! I used this instead..
if (file_put_contents('/var/www/html/write/savefile.php', $name."\n\r", FILE_APPEND))

#6
[eluser]ealonw[/eluser]
Geez you beat me to it....

#7
[eluser]Pascal Kriete[/eluser]
:-) Glad it's working for you.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.