Passing array data from view to controller and reverse php codeigniter - php

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.

Related

codeigniter input post method returns empty array?

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.

How do we send php page attribute values to a php class method

How do we send php page attribute values to a php class method?. I want to get php attribute values into a php class method. following is the way i tried.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<form id="test" method="post" action="testPhp.php">
<label>Name</label>
<input type="text" name="n1" id="name" />
<input type="submit" value="ADD NAME">
</form>
</body>
</html>
Now I want to get this name value in method in separate php file
testPhp.php
<?php
class getParam{
public function getFormParm(){
$name = $_POST['n1'];
}
}
?>
You have no valid action declared as the forms action. Try $_SERVER['self'] as the form action, like so:
<body>
<form id="test" method="post" action="<?php echo $_SERVER['self']?>">
<label>Name</label>
<input type="text" name="n1" id="name" />
<input type="submit" name='submit' value="ADD NAME">
</form>
</body>
And to get the result of this, do the following:
<?php
echo $_POST['n1'];
?>
The way you have the class set up, you aren't actually doing anything with the class. Nothing is being executed. You can do something like this:
<?php
class getParam{
public function getFormParm(){
$name = $_POST['n1'];
return $name;
}
}
// This is the part that you are missing
$getParam = new getParam();
echo $getParam->getFormParm();

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.

Unable to insert data into database (PHP MVC)

I want to allow people to submit their contacts by entering it into a field and it would eventually send it to the database but been trying it for quite some time and cant find out whats wrong with it, if possible do let me know how to return a echo saying "SENT" when its in database. Redirecting to the same page, if possible , dont even need a refresh to get that SENT shown to people.
This is my Controller
$this->config->db_config_fetch();
$this->load->model('skills_model');
//Get Form Data
$this->input->post('submitsms') ;
//GEt phone number from field
$type = $this->input->post('phonenumber');
//Call model
$this->skills_model->addsms($type);
}
This is my View # home page
<form method="post" action="<?php echo site_url(''); ?>" name="form" enctype="multipart/form-data">
<label>SMS Subscription</label>
<input type="text" name="phonenumber" placeholder="Phone Number here">
<button class="btn btn-info" name="submitsms" type="submit">Subscribe</button>
</form>
This is my Model
function addsms($type){
$this->db->set('number', $type);
$this->db->insert('subscribers');
}
I also tried the following
function addsms($type){
$sql = "INSERT INTO 'subscribers' ('number') VALUES ($type)";
$this->db->query($sql);
echo $this->db->affected_rows();
}
You advice would be of a great help! thank you!
A small example ...........
View file
<form action="<?php echo ROOT_FOLDER ?>/add_price" method="post">
<input type="text" class="text" name="amount" value="<?php echo set_value('amount'); ?>" />
<input type="submit" class="submit" value="Approve" />
</form>
Controller...........
public function post_add_price()
{
$data = array(
'amount'=>$this->input->post('amount'),
);
$this->model->add_amount($data); //sending amount to model to insert in dataabse
echo "Amount added to database";
}
Model................
public function add_amount($data)
{
$this->insert_helper('order_amount_table',$data);
}
public function insert_helper($table_name, $data_array){
$this->db->insert($table_name,$data_array);
return $this->db->insert_id();
}
I hope this example will help you .........if you have any doubts ask

Any idea how to build a php string manipulation app using forms?

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}

Categories