I am trying to create a data-entry,with controller which inputs the data, displays it and confirm for any edit then uses a method to submit it to the model.
The problem is that in the load the model in the function post_data() the value of $this->input->post() return an empty array.
I am entering the data returning to the get_data function and then displaying it in the data.php in the view.
using
data.php in view post the data to the post_data method.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $this->input->post("xyz") ?>" />
the controller is
protected $arr;
public function index(){
$this->load->view('index/index');
// $this->load->library('Controllerlist');
// print_r($this->controllerlist->getControllers());
}
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
public function post_data(){
$this->load->model("form1","form",TRUE);
print_r($this->input->post());
$blue=$this->form->insert_data($this->arr);
print_r($blue);
if($blue){
echo "Successfully added to database";
}
}}
Why does print_r() method return an empty array?
$this->input->post() in the post_data method return empty array.
if Iam right $this->input->post() should is global to all the method in Controller CI class.
Your input tag in form doesn't have a name attribute.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input name="xyz" type="text" value="<? php echo $this->input->post("xyz") ?>" />
</form>
Edit:
Also the input tag is closed in a wrong way (closing before the value attribute).
<input type="text name="xyz" value="<? php echo $this->input->post("xyz") ?>" />
If you are loading the view data.php in get_data:
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
Then how are you accessing the get_data url? If you are just typing it in the browser, then you are not POSTing you are GETing and so $this->input->post is always going to be an empty array.
It's not entirely clear to me how you are calling your method get_data, but you should probably try to alter it so that it more carefully constructs the data you want to be displayed in any view that it loads
public function get_data(){
$view_data = array(
"xyz" => "here is some value" // you could get this value from anywhere
);
$this->load->view("index/data", $view_data);
}
Then in your view data.php you would want to refer to just $xyz instead of $this->input->post("xyz"):
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $xyz; ?>" />
Note that second parameter to the view loading function. It's an array and each associative key in the array will be expanded into a variable within your view.
Related
I am trying to pass the data from view to controller using php codeigniter. Here is my form view:
<form method="POST" action="<?php echo base_url()?>downloadfiles">
<p>Download file</p>
<p>Browse zip file</p>
<input type="text" name="data" value="<?= $data; ?>" style="display: none">
<input type="submit" name="downloadfiles" value="Download" />
</form>
And in downloadfiles.php - the controller, I try to get the array $data passing by POST method and passing it back to the download_success.php view:
if ($this->input->post('downloadfiles')) {
$data = $_POST['data'];
$this->load->view('upload/download_success', $data);
}
This is my code in download_success.php:
<?php
if(!empty($data))
{
print_r($data);
}
?>
When I run the code in the form view, it returns the error array to string conversion, and in the download_success view, it didn't print anything. Where were I wrong?
This is because you are passing a string variable with the view and CodeIgniter consider it as an array.
MyController.php
if ($this->input->post('downloadfiles')) {
$view_data = array('my_data' => $this->input->post('data') );
$this->load->view('upload/download_success', $view_data);
}
my_view.php
if(!empty($my_data))
{
print_r($my_data);
}
Your form will be like:
//if $my_data is an array then you can print_r($my_data) and show the required values you want to show in your view.
<form method="POST" action="<?php echo base_url()?>downloadfiles">
<p>Download file</p>
<p>Browse zip file</p>
<input type="text" name="data" value="<?=$my_data?>">
<input type="submit" name="downloadfiles" value="Download" />
</form>
Try this and I hope it will help you.
I have a input field
<?php
echo form_open('moneyexchange/borrow_first_page'); ?>
<input id ="Amount" type="text" placeholder="Amount in €" name="writtenamount">
<a type="submit" href="<?php echo base_url();? >index.php/moneyexchange/invest_first_page">invest</a>
</form>
And in codeigniter moneyexchange controller I have a function here
public function invest_first_page(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->view('header');
$userProvidedAmount = $this->input->post("writtenamount");
$data = array(
'userProvidedAmount' => $userProvidedAmount
);
$this->load->view("invest_firstpage", $data);
$this->load->view('footer');
}
But for some reason I cannot get the value from the input field in the view file to the controller, please help.
it shows that my $userPRovidedAmount is a (bool)false value. But I need to get numbers from the input variable
You have a link where your submit button should be. A link wont submit the form (unless there is some unseen javascript) - it will just make a regular GET request to the page (hence why POST is empty in your controller)
Change the action of your form and make the submit button a form element:
<?php echo form_open('moneyexchange/invest_first_page'); ?>
<input id ="Amount" type="text" placeholder="Amount in €" name="writtenamount"/>
<input type="submit" value="invest"/>
<?php echo form_close();?>
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.
hey guys i am new in codeigniter,i have a form like this
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/$id" name="application" method="post" >
//some code
</form>
i have a controller methode
function input_investment($id)
{
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertinvestment($id);
}
i want to get $id from form action to controller methode how can i do that . . pls help me . .
better to pass the value in the hidden field
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name="my_id" value="<?php echo $id; ?>"/>
</form>
in your ci function
function input_investment() {
$id = $this->input->post('my_id');
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertinvestment($id);
}
or if you want (A test)
// Sample view
<?php $id = 1; ?>
<form action="<?php echo base_url('my_class/my_method/' . $id); ?>" method="post" >
<input type="submit" />
</form>
// Controller
class My_class extends CI_Controller {
public function index() {
$this->load->view('my_class');
}
public function my_method($id) {
echo $id; // outputs 1
}
}
You need to use PHP and echo $id in the element if you want the value, right now you're sending '$id' to input_investment($id).
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/<?php echo $id; ?>" name="application" method="post" >
//some code
</form>
Here your form method is post so you cont get the id through the get method ,you can do like
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name='id' value="<?php echo $id;?>">
</form>
and in your controller you can try with post like
$id = $_POST['id'];
or
$id = $this->input->post('id');
it willl better option for you in all cases if you are trying to send single or multiple data to the controller from an form....
$route['ctl_dbcont/input_investment/(:num)'] = "ctl_dbcont/input_investment/$1";
Just add this line at your config/route :) .
This will work with numbers only, if you have other type of IDs you can use (:any)
Other option is to catch the id directly using :
$id = $this->uri->segment(3);
Where segment(3) is the third element after your domain :
http://domain/segment1/segment2/segment3
I want to build an string/text manipulation app that:
An app consists of a form
User inputs text there as a string using
Click a button - which usues $_POST method to manipulate a string using oop method to :
Example method assigned to a button:
public function revers_sentence() {
$this->string = strrev($this->string);
return $this;
}
Then the manipulated string is displayed in the same form.
User can do another manipulation with a converted string
How to assign method to a button to trigger the function and display results in the same form?
Can it be achived in a single php file to send and get a result?
(I want to store my classes in a seperate file)
Any help/idea Appreciated - any wise guy?
edit:
<?php
require_once('mod.php');
$string='';
if (isset($_POST['string']))
$string=$_POST['string'];
if (isset($_POST['DoStuff']))
{
//$string = reverse_1_word($string);
**$string->reverse();**<-------------------------------Fatal error:---------
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
Fatal error: Call to a member function odwroc() on a non-object on line 14 so how to make every new string an object ?
I would do something like that:
<?php
Class MyString{
private $string='';
function __construct(){
if (isset($_POST['string']))
$this->string=$_POST['string'];
}
function doStuffWithThisString(){
$this->string=$this->string.'!';
}
function doOtherStuffWithThisString(){
$this->string=$this->string.'!!!';
}
function getString(){
return $this->string;
}
}
$myString = new MyString();
if (isset($_POST['DoStuff']))
{
$myString->doStuffWithThisString();
}
if (isset($_POST['DoOtherStuff']))
{
$myString->doOtherStuffWithThisString();
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string->getString()?>" name="string" />
<!-- <?=$string->getString()?> is the same as <?php echo $string->getString(); ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
You could have a form like this :
<form action="currentFile.php" method="post">
<input type="text" value="myValue" name="text" />
<input type="submit" value="Reverse !" name="reverse" />
<input type="submit" value="Other manipulation" name="otherManip" />
</form>
When you receive the data, you can do this :
if ( isset($_POST['reverse']) ) {
// User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
// User clicked on the other button
}
to achive the send and get in a single PHP file you have to set the forms action to the actual site you are on. I do this ba writing <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>
To work with the returned values you just have to build an if-statement, that checks for the passed values if (isset($_POST['buttonname'])){ //DO SOMETHING}