Form is not getting displayed in codeigniter. - php

I am new to code igniter. I've created a form but it is not display properly.
When I put
<?php echo form_open('sms'); ?> instead of <form action="">tag
here in my form and controller, I can't understand why it is not displayed.
<?php echo form_open('sms'); ?>
<p>
<label><strong>Username</strong>
<input type="text" name="textfield" class="inputText" id="textfield" />
</label>
</p>
<p>
<label><strong>Password</strong>
<input type="password" name="textfield2" class="inputText" id="textfield2" />
</label>
</p>
<input type="submit" value="Authentification" name="auth" />
<label>
<input type="checkbox" name="checkbox" id="checkbox" />
Remember me</label>
</form>
and my controller is
<?php
class sms extends CI_Controller{
function school(){
$this->load->view('school/index.php');
if($this->input->post('auth',TRUE)){
$this->load->view('school/dashboard.php');
}
else{
$this->load->view('school/index.php');
}
}
}
?>

If this is your entire script for the most part, it looks like you need to load the helper first from the CodeIgniter Form Helper Page.
If you don't have this line, try adding it before the form_open() function:
<?php $this->load->helper('form'); ?>
While I have used CodeIgniter, it's been a while. Let me know if that changes the result.
Edit: Since you've chosen my answer I'll include this one, credits go out to devo:
You could change </form> to: <?php echo form_close(); ?>. There are pros and cons for this method though, and without using arguments you might be better off sticking with </form>.
I'll explain further:
<div class="registration">
<div class="form-box">
<?php $this->load->helper( 'form' ); ?>
<?php $end = '</div></div>'; ?>
<?php echo form_open( 'register' ); ?>
<!-- Form Inputs Here -->
<?php echo form_close( $end ); ?>
<!-- Echos '</form></div></div>' -->
So for closing the form without arguments, the </form> tag works best, both by performance and simplicity. The example used above is a rather simplistic view of what you can do with it, since what I wrote is not very efficient either.
However, this is still php we're talking about, so perhaps the craftier among us could put it to better use.
End Edit

Have you loaded the form helper? You can use $this->load->helper('form'); in your controller action, her inside function school(). You can then use form helper in the view pages.

Load form helper,
$this->load->helper('form');
And use,
echo form_close()
Instead of,
</form>

First in your Controller put in:
$this->load->helper('form');
And Change </form> to :
<?php echo form_close(); ?>

Related

CodeIgniter URL repetition

I just started CodeIgnitor, that's the first time I use MVC structure though, and I have a problem that I've never seen before... It's mainly in the "form" part, but also in the database display.Also I use Xampp.
I've got a form to create an item to insert in the database, but whenever i click the submit button, things gets wrong in the url section.
My base URL is : localhost/CodeIgniter-3.1.1/ (CodeIgniter-3.1.1 is the directory that contain every php folder).
So the form page URL is : localhost/CodeIgniter-3.1.1/index.php/news/create
And when i submit, it is : localhost/CodeIgniter-3.1.1/index.php/news/localhost/CodeIgniter-3.1.1/index.php/news/create
It just repeat the entire URL after the controller (news).
I don't think it has to be with config.php, my base URL seems good, I just don't know.
Make your base url http://localhost/Codeigniter-3.1.1/index.php/ then in your <form> tag set the url like this <form method="post" action="<?= base_url('news/create') ?>">
In /application/config/config.php set $config['base_url'] like this
$config['base_url'] = http://localhost/Codeigniter-3.1.1/
In your view do either one of the following to create the <form> tag
<form method="post" action="<?= base_url('news/create'); ?>">
of if you have loaded the "Form Helper" (documented here) use this line in the view
<?php echo form_open('news/create'); ?>
It's handle by the framework, as so:
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
Also, the problem occure when I put a link to a view, like :
<a href="<?php echo 'news/'.$news_item['slug']; ?>">
Instead of building the right URL it copy itself along the bar.

How do you correctly pass post variables from the view to the controller in CodeIgniter?

I haven't used CodeIgniter in nearly a year and I seem to have forgotten a lot of the fundamentals.
I am trying to retrieve the post variables from a form and pass them into a model which inserts them into mysql.
The controller function my form submits to looks like this:
public function validation() {
$this->load->helper("form");
$this->load->model("contact_form");
$data = array(
"name" => $this->input->post("name"),
... etc. etc. ....
);
if ($this->contact_form->new_form($data)) {
$this->load->view("header");
$this->load->view("submitted");
} else echo "Sorry, there was a problem adding the form to the database.";
}
The form in the view is structured like so:
<? echo form_open("form/validation");?>
<div id="one" style="display: block;">
<h1>A Heading</h1>
<p>Some Text</p>
<p class="bold">Name: <input type="text" name="name" class="single" value="<?php echo set_value('name'); ?>"></p>
<p class="bold">Email: <input type="text" name="email" class="single" value="<?php echo set_value('email'); ?>"></p>
<p class="bold">And then some radio buttons</p>
<p> yes <input type="radio" name="registered" value="yes"> no <input type="radio" name="registered" value="no"></p>
<p class="bold">And a textarea...</p>
<textarea name="description" class="fill" value="<?php echo set_value('description'); ?>"></textarea>
next
</div>
<div id="two" style="display:none;">
<h1>Another Heading...</h1>
<p class="bold">And some more textareas</p>
<textarea name="audience" class="fill"></textarea>
... There are four divs in total with further textarea fields ...
<p class="bold"><input type="submit" name="submit" value="submit" class="center"></p>
back
</div>
<? echo form_close();?>
And finally my model is very simple:
class contact_form extends CI_Model {
public function new_form($data) {
$query = $this->db->insert("contact", $data);
if ($query) {
return true;
} else return false;
}
}
The form processes without any errors, but the data just appears as 0's in MySQL. If at any point I attempt to output the value of $_POST it returns BOOL (false), or with $this->input->post('something'); it returns NULL.
You will notice that no actual validation takes place. Initially I was using $this->form_validation->run() and getting the same results. I thought perhaps I was having trouble with the validation so I stripped it out and now I'm fairly certain my problem is that I'm not passing the $_POST variables correctly.
Can anyone explain why I am failing so hard?
I have now resolved this problem.
For some reason <? echo form_open("form/validation");?> was implementing GET and not POST. Replacing that line with <form method="post" accept-charset="utf-8" action="form/validation"/> resolved the issue.
According to the CodeIgniter documentation, by default, form_open should use POST - I have no idea why in my case it decided to use GET.

How to call a value from function set_value in Code Igniter?

I have a simple task , and i'm using a MVC methods and CI framework for this task.
I have made a view to input data to database, and it's work, and i make a 2 anchors, those are an [Update] and [Delete], the function delete is working, but the update isn't working.
After user click anchor [Update], it will linked to another view(update_view), and i want to show the content which i've clicked in the (update_view). and i think it use a set_value to set a second parameter, to show a value in my update_view.
This is my code in a view(update_view)
<!-- update_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Update</h2>
<?php echo form_open('site/update'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" value="<?php echo set_value('judul','??')?>"/>
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px" value="<?php echo set_value('konten','??')?>"/>
</p>
<p><input type="submit" value="Ubah" /></p>
<?php echo form_close(); ?>
</body>
</html>
What should i put in input tag in value attributes, i want to show a value in input fields (judul, konten) from page before where i clicked the anchors.
I still can't show a image for a view before click, because i'm still don't have 10 rep to share images. so i'will show the coding where the view(options_view) i've clicked the anchors.
This below is the code in a view(options_view) :
<!-- options_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" />
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px"/>
</p>
<p><input type="submit" value="Simpan" /></p>
<?php echo form_close(); ?>
<hr/>
<h2>Read</h2>
<?php if(isset($records)): foreach($records as $baris) : ?>
<h3><?php echo $baris->judul ?></h3>
<div><?php echo $baris->konten ?></div>
<?php echo anchor("site/view_update/$baris->id","[Update]"); ?>
<?php echo anchor("site/delete/$baris->id","[Delete]"); ?>
<?php endforeach; ?>
<?php else : ?>
<h3>Tidak ada data.</h3>
<?php endif; ?>
</body>
</html>
i'm still doubt, whether i should add code in my controller or my view(set_value).
So anyone can help me to solve this problem.
Thank's for help
set_value is codeigniter form_helper function. That helps u to print previous input value when form_validation fails.
In your case if u want to show your data u need to pass data to the view in controller.
E.G:
Controller:
$data["me"] = $this->model->getData($id);
$this->load->view("update_view",$data);
View:
<input type="text" name="judul" id="judul" value="<?php echo $me->judul;?>"/>

url issues with codeigniter

I have an example at the following url, which sends the user to a basic form.
The code for the form is the following;
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
If I understand correctly, once the user presses "submit", it should go back to the controller named "form". But it does not work as expected.
I would want it to submit to index.php/form, and instead it goes to /index.php/index.php/form
Feel free to test out website at url above and see the issue by yourself.
I just looked at your source code and noticed this:
<form action="http://helios.hud.ac.uk/u0862025/CodeIgniter/index.php/index.php/form" method="post" accept-charset="utf-8">
Which displays two "index.php"s. This likely means you have "index.php" set in your base url AND index page configuration. You should remove the "index.php" from one of those sources, preferably from your base_url.
Check your base_url() in config file. If it is correclty, than you can write the form like this:
<?php echo validation_errors(); ?>
<?php echo form_open('controller_name/function_name');
// controller name write your name of controller
// and function name write your function
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
<?php echo form_close(); // don't forget to close correct ?>
I have faced this situation once and the problem was i have included index.php in my base_url in ./application/config/config.php file please check and remove if present. You can also solve your problem by removing index.php from index_page key but it is not a good practice to include index.php in your base_url.

creating a dynamic sidebar with zend framework

I am trying to create a dynamic sidebar in zend framework. I have googled some articles, browsed even the stackoverflow archive but i can't seem to get it so please help me figure this out.
Here is some code from my layout.phtml file:
<div id="contentWrapper">
<div id="contentArea">
<?php echo $this->layout()->content;
?>
</div>
<div id="sidebar">
<div id="user-authentication">
<?php if (Zend_Auth::getInstance()->hasIdentity()) {
?>Logged In as<br />
<?php
echo Zend_Auth::getInstance()->getIdentity();
} else {
?>
<input type="text" name="login" class="loginInput" /><br/>
<input type="password" name="password" class="loginInput" /><br/>
<input type="submit" name="submit" value="Log In" class="loginButton" />
<?php } ?>
</div>
<div id="sidebar-content">
<? echo $this->layout()->sidebar; ?>
</div>
</div>
</div>
I could use this Best practice creating dynamic sidebar with zend framework but that means I would need to have redundant code for displaying the login box/logged in as.
Are you just worried about the repetition of hasIdentity and getIdentity in the sidebar-content div?
If so, maybe you'd prefer this:
<?php
$auth = Zend_Auth::getInstance();
$user = $auth->hasIdentity() ? $auth->getIdentity : NULL;
?>
<div id="user-authentication">
<?php if ($user) { ?>
User stuff.
<?php } else { ?>
Public stuff.
<?php } ?>
</div>
<div id="user-authentication">
<?php if ($user) { ?>
User stuff.
<?php } else { ?>
Public stuff.
<?php } ?>
</div>
However, that's just a matter of coding style. There's nothing wrong with checking a users logged in state more than once. It's necessary with modular code to do so; that's probably why Zend_Auth is a Singleton.
Looks like you need a widget:
Using Action Helpers To Implement Re-Usable Widgets - phly, boy, phly
For displaying or not some of the content, look for integration of Zend_Acl and Zend_Navigation.
It is as easy as: $container->setAcl($acl)

Categories