I have a very small app, it requires an id, then updates a database table based on that id. The id is an input from the view.
Basically my DB (model) has getAllProjects() and approveProject($id).
Controller:
<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class ApproveProject extends CI_Controller {
public function index () {
$this->loadView();
}
public function getData () {
$this->load->model("db_Projects");
$this->db_Projects->getAllProjects();
}
public function updateDB () {
// how can I get this variable?
$this->db_Projects->approveProject($toApprove);
}
public function loadView() {
$this->load->view("ViewProjectApproval");
}
}
?>
View:
<html language="en">
<head>
<title>Aprobare Proiect</title>
</head>
<h1> Aprobare Proiect </h1>
<body>
<div id="container">
<?php
if (isset($_POST['projectSubmit']) && ($_POST['projectSubmit'] == "Submit"))
{
$toApprove= $_POST['projectId'];
}
?>
<form action ="updateDB" method="post">
<input type="text" name="projectId">
<input type="submit" name="projectSubmit" value="Submit">
</form>
</div>
</body>
I have no idea if my view is good, or if it should be otherwise ... how can I send the ID to my controller ?
You are submitting the form using POST method so you can retrieve the variable from the POST inside your controller's method, like
$toApprove = $this->input->post('projectId');
So your method/function will look like something like this
public function updateDB () {
if($this->input->post('projectId'))
{
$toApprove = $this->input->post('projectId');
// load the model and call the method, like
$this->load->model('model_name');
$model_name->method_name($toApprove);
}
}
Update:
Your form's action doean't seem right, try this (controller_name/method_name)
<form action ="<?php echo site_url('ApproveProject/updateDB') ?>" method="post">
Related
I am new to CodeIgniter 3, I am trying to create a static page from this tutorial https://codeigniter.com/userguide3/tutorial/static_pages.html
http://localhost/index.php/pages/view It is work.
but my actual is like this C:\xampp\htdocs\application\views\pages
? (and not work)
why the folder is not /views/pages ?
I am getting confuse.
Also need help ! i am new to codeIgniter 3, I want to create a simple form, but they not work in the views
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
welcome.php
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
both added the controllers
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome');
}
}
class Form extends CI_Controller {
public function index()
{
$this->load->view('form');
}
}
I tried these, but they not work.
You have not mentioned form action correctly. The form action should be look like this
action="<?php echo base_url(); ?>form"
and in the route.php add route for your action.
Example:
$route['form'] = 'form/index';
Then you can have $_POST values in your Form controller.
<?php
class IndexController extends \Phalcon\Mvc\Controller {
public function indexAction(){
}
}
?>
<?php
class SignupController extends \Phalcon\Mvc\Controller {
public function indexAction(){
}
}
?>
<?php
echo "<h1>Hello!</h1>";
echo Phalcon\Tag::linkTo( "signup", "Sign Up Here!");
?>
<?php use Phalcon\Tag; ?>
<h2>Sign up using this form</h2>
<?php echo Tag::form( "signup/register" ); ?>
<p>
<label for="name">Name</label>
<?php echo Tag::textfield( "name" ); ?>
</p>
<p>
<label for="email">E-Mail</label>
<?php Tag::textfield( "email" ); ?>
</p>
<p>
<?php echo Tag::submitButton( "Register" ); ?>
</p>
</form>
I was following a tutorial on phalcon framework here but it can't get it to work. I have created the controllers for the index page and the signup page. I also created the views for the index controller and the view for the signup controller.
What happens is that when I click on the link to go to the signup page it shows the url correct which means we should be on the signup page but it shows the index view not the signup view. Basically when i click on the signup link the only thing that changes in the browser is the url but not the page.
anyone know what is going on here?
Ok,
I got this working in the end.
Make sure that (in my case Nginx) you confirm that it has been set up correctly.
The second thing to look at is the code to handle the request. I have used this:
$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
if (!empty($_SERVER['REQUEST_URI'])) {
$pathInfo = $_SERVER['REQUEST_URI'];
} else {
$pathInfo = '/';
}
echo $application->handle($pathInfo)->getContent();
This isn't exactly what I wanted, but for some reason my PATH_INFO was coming out as empty, even when I have set cgi.fix_pathinfo to 1 in the php.ini
Hope this helps.
below is my 'mainview.php' view. from here iam attempting to submit and just open the next view which is called 'carerview.php'.
<form action="<?php echo base_url()?>login" method="post">
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" id="" name="" placeholder="your#email.com"></br></br>
<div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span>
<input type="password" id="" name="" placeholder="Password"></br></br>
<button type="submit" class="btn btn-primary"><i class="icon-user icon-white"></i>Sign in</button>
</div>
</div>
</form>
Iam trying to submit this is giving me issues.The Index page loads which contains the above view. but when i submit . i get requested URL not found on this server
. then if i use the full url action="application/controllers/user/login" i get a forbidden, dont have permission to access it.
my method in my controller class is just to load the next view on submit so i dont think there is an issue there . below is the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
if(!$this->isLoggedIn())
{
$this->load->view('mainview');
}
else
{
//do something
}
}
public function login()
{
$this->load->view('carerview');
}
public function isLoggedIn()
{
return false;
}
}
any help would be appreciated thanks.
if you didn't remove index.php from your URL and didn't set anything to base_url in configuration,try this
<?php echo base_url();?>index.php/user/login
localhost/your_app_folder/index.php/controller/action
Your form action is base_url(), which means is the application index route.
Try using form_open() (in the form_helper), which takes care of building the correct url:
<?php echo form_open('user/login');?>
... your form here
<?php echo form_close();?> // since I didn't see a close form tag in your form
Be careful of any routes that might intercept the request.
Alternatively, you could use site_url():
<form method="POST" action="<?php echo site_url('user/login');?>">
I just started learning CI and PHP and wanted to make a simple CRUD application.
I created a function to add a record, but when i submit the data in the form, Chrome downloads a file with no extension.
Controller
<?php
class Options extends CI_Controller{
function index()
{
$this->load->view('options_view');
}
function create()
{
$data = array(
'name' => $this->input->post('name'),
'price' => $this->input->post('price'));
$this->data_model->addItem($data);
$this->index();
}
}
Model
<?php
class Data_model extends CI_Model {
function getAll() {
$q = $this->db->query("SELECT * FROM items");
if($q->num_rows() >0){
foreach($q->result() as $row){
$data[]=$row;
}
}
return $data;
}
function addItem($data){
$this->db->insert('items', $data);
return;
}
}
?>
View
<html><head></head><body>
<style type="text/css">
label {display: block;}
</style>
<h2>Create</h2>
<?php echo form_open('options/create'); ?>
<p>
<label for="name">Name</label><input type="text" name="name" id="name" />
</p>
<p>
<label for="name">Price</label><input type="text" name="price" id="price" />
</p>
<p><input type="submit" value="Submit" /></p>
<?php echo form_close(); ?>
</body>
</html>
Is there something that i did wrong?
No errors pop out. The form is created, but when i add data in the textboxes and click submit, the browser dowloands a "Create" file.
Could it be that you don't need the last slash in that line?
<?php echo form_open('options/create/'); ?>
i think you need to load form helper like
$this->load->helper(array('form', 'url'));
please let me know if you face any problem.
Add $this->load->model('data_model'); to method before $this->data_model->addItem($data);
Im a newbie in CodeIgniter and I just followed a tutorial for CRUD operations within framework but my data is not getting inserted in my database. Here is my code:
Controller: site.php
<?php
class Site extends CI_Controller
{
function index()
{
$this->load->view("home");
}
function create()
{
$data=array('title'=>$this->input->post("title"),
'song'=>$this->input->post("song")
);
$this->site_model->add_record($data);
$this->index();
}
}
View: home.php
<html>
<head>
<title>my page</title>
<style type="text/css">
input,label
{
display:block;
}
</style>
</head>
<body>
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Song:</label>
<input type="text" name="song" id="song" />
</p>
<p>
<input type="submit" value="submit"/>
</p>
<?php echo form_close(); ?>
</body>
</html>
Model: site_model.php
<?php
class site_model extends CI_Model
{
function getAll()
{
$q=$this->db->get('name');
return $q->result();
}
function add_record($data)
{
$this->db->insert("name",$data);
return;
}
function update_record($data)
{
$this->db->where("id",14);
$this->db->update('name',$data);
}
function delete_row($data)
{
$this->db->where("id",$this->uri->segment(3));
$this->db->delete('name',$data);
}
}
My table name is name and I have 3 fields: id(auto_increment), title, and song. Help would be much appreciated.
Add this line before you calling model function.
$this->load->model('site_model');
after that call your model function
$this->site_model->add_record($data);
I think this will solve your problem
Regards
iijb