I'm beginner with code igniter
I want to define site title from user input value
I need create a specific Controller for it ?
I think using this code:
view (for build a new page)
Site Title:<input type="text" size="40" name="title" />
view (generated page )
<title><?php echo $title ?> </title>
controller
$title = $_POST['title'];
From what I understood, you may try something like below: But you should read up more on the codeigniter doc.
View File:
<input type="text" name="title">
Controller:
$data['title'] = $this->input->post('title');
$this->load->view('view-file-name', $data);
View File (view-file-name.php)
<title><?php echo $title; ?></title>
Related
I'm using CodeIgniter on OpenShift.
In my controller I'm using:
$this->session->set_flashdata('message', 'message X');
$this->load->view('viewpage');
In my view I'm using:
print_r ($this->session->userdata);
echo $this->session->flashdata('message');
Here are my observations:
first time through the controller/load view, I see nothing echoed with the
$this->session->flashdata('message');
I see this with the print_r:
[flash:new:message]=>message 1
second time through the controller/load view, I see "message 1" being echoed
I see this with the print_r:
[flash:old:message] =>message 1[flash:new:message]=>message 2
So what appears to be happening is that [flash:old:message] is being displayed instead of [flash:new:message]. If [flash:old:message] isn't set, then nothing is displayed.
Please help.
Cheers,
Mike
when you set a value in a flash data, you need to make a view refresh like:
controller.php
function do_somthing(){
$this->session->set_flashdata('index', 'text message');
redirect('controller/view', 'refresh');
}
controller/view.php
<div>
<?= (isset($this->session->flashdata('index'))) ? $this->session->flashdata('index') : ''?>
</div>
Flashdata is designed to be used moving from 1 page to another (redirects), you typically use it after a post, the return a success/failure message.
the reason for this:
[flash:old:message] =>message 1[flash:new:message]=>message 2
occurring is because flashdata is retained for 1 additional page load (so you can use $this->session->keep_flashdata() if required... as you are triggering flashdata by refreshing the page to generate these results its confusing things and not designed for use this way..
This really seems to be an issue occurring due to the way you are using flashdata than it displaying the incorrect data.
A working example of using flashdata is below (even without a redirect)
controller:
public function index()
{
if (!$this->input->post()) {
$this->load->view('playland/index');
}else{
if ($this->input->post('submit') == "submit") {
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$this->session->set_flashdata('test', 'data posted');
$this->load->view('playland/retrieve', $data);
}
}
}
index view:
<html>
<body>
<?php print_r($this->session->userdata)?>
<form method="post" action="playland">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname"><br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
retrieve view:
<html>
<body>
<?php echo $this->session->flashdata('test') ?><br>
<p>
First Name:<br>
<?php echo isset($firstname) ? $firstname : '';?><br>
Last Name:<br>
<?php echo isset($lastname) ? $lastname : '';?><br>
</p>
Click to refresh the page
Return to original page
</body>
</html>
I am submitting form using form helpers and it's going to blank page and when I see the view source, the action in the view source is like this http://::1/ci1/login_validation I don't understand the ::1 in it shouldn't it be localhost:8080/? But if I use simple form tags like regular html it works fine?
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
View Source
<form action="http://::1/ci1/login_validation" method="post" accept-charset="utf-8">
<p>Email:<input type="text" name="email" value="" /></p>
<p>Password:<input type="password" name="password" value="" /></p>
<p><input type="submit" name="login_submit" value="login" /></p>
</form>
I would think it is a couple of thing like.
If your form action has http://::1/ then does not submit correct because base url is empty
$config['base_url'] = '';
Fill base url example:
$config['base_url'] = 'http://localhost/project_name/';
On codeigniter 3 version not best idea to leave it blank because you will run in to that issue.
Make sure your class and file names of controllers etc have first letter upper case.
Filename: Login_validation.php
Controller:
<?php
class Login_validation extends CI_Controller {
public function index() {
// form submit controller code.
}
}
View
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
This is a common mistake people for get to check.
Why http://::1/??
This mean your base_url() is empty.
Then how this http://::1/ comes??
When your project URL is empty, CI detect your project URL. So this http://::1/ knows your project path.
How to set base_url()??
In config/config.php set $config['base_url'] = ''; the project URL.
Keeping base_url() empty any harm??
When you in local its ok and fine. But when you host that just add your site URL to it.
$config['base_url'] = 'www.stackoverflow.com';
I have form and when i submit that data are passed to next page i can see that in url. but i when i get and print that it shows nothing , here i have attached my coding
<form action="addfilters/add"><input type="hidden" name="id" value=<?php echo $filters->id ?> > <input type="submit" value="Add New"></form>
my controller
<?php
class AddFilters extends Admin_Controller
{
function add(){
echo $this->input->post('id');
}
}
URL
http://localhost/code/index.php/admin/addfilters/add?id=1
but when i print ,it shows nothing , please help me
i have just used
<?php echo form_open_multipart('admin/addfilters/add'); ?>
top of the form. it works fine :-)
I'm having some issues using sessions on my test website (Running it on WAMP server locally, using PHP php5.3.13) I have checked my php.ini to make sure that sessions are actually being saved, which they are:
C:\wamp\tmp
Basically, when the user logs in it shows, Welcome back, .$username so when I log in with the user "John", it shows this accordingly. Now, when I leave the login page and go back to it this sessions is somehow being lost. (And yes, I am using session_start at the top of every page).
Here is my code;
index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Codecall Tutorials - Secured Login with php5</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php include "header.php" ?>
<div id="wrapper">
<form method="post" action="">
<h2>Log In</h2>
<div id="underline"></div>
<ul>
<li>
<label for="usn">Username : </label>
<input type="text" maxlength="30" required autofocus name="username" />
</li>
<li>
<label for="passwd">Password : </label>
<input type="password" maxlength="30" required name="password" />
</li>
<li class="buttons">
<input type="submit" name="login" value="Log me in" class="xbutton" />
<input type="button" name="register" value="Forgot Password?" onclick="location.href='passrecover.php'" class="xbutton" />
</li>
</ul>
</form>
</div>
</body>
</html>
<?php include "login.php" ?>
And my login.php page:
<?php
if($_POST){
if(empty($_POST['username']) && empty($_POST['password'])) {
echo 'Please enter all fields';
}else {
$username = $_POST['username'];
$password = $_POST['password'];
if($password !== $password){
echo 'Your password is wrong';
}else {
$db_name =
$db_user =
$db_pass =
$conn = new PDO('mysql:host=localhost;dbname=XXXXX', 'XXXXX', 'XXXXX', // My bd details have been removed for this post, for security issues obviously
array( PDO::ATTR_PERSISTENT => true )
);
$stmt = $conn->prepare("SELECT username,password from members WHERE username = ? AND password = ?");
$stmt = $conn->prepare("SELECT username,password FROM users WHERE username = ? AND password = ?");
$stmt->execute(array($username, $password));
if($stmt->rowCount() === 1 )
{
$_SESSION['name']= $username;
echo 'Welcome back '. $_SESSION['name'];
//echo '<META HTTP-EQUIV="Refresh" Content="0; URL=usercp.php">';
}else {
echo 'Username or Password incorrect.';
}
}
}
}
?>
So, when I originally log in it shows the $_SESSION['name'];
just fine, but when I move page and go back to it, it no longer shows it. (My other pages also have session_start(); ) My original assumption was that my code was wrong, or that my php.ini file wasn't saving any data. What is going wrong here?
You need to add session_start() on your login.php too
The reason it no longer shows is because when you go to another page, you aren't processing that block of code anymore. Because $_POST is empty on a regular page load, so you aren't echoing anything out. Try adding, var_dump($_SESSION); at the top of your page and then load something.
Try this right after your session_start();,
if(!empty($_SESSION['name'])) {
echo "Hello {$_SESSION['name']}";
}
Perhaps you're destroying the session somewhere in the script.
I have no idea how to do this, this does need to be added to a working store with normal products, I am ok at making an new template to do this.
Create a custom magento module with a controller
Then create a save action in your SavepriceController.php
public function saveAction(){
$websites = Mage::app()->getWebsites();
$product_id = $this->getRequest()->getParam('product_id');
// need to validate price here
$price = $this->getRequest()->getParam('price');
$product = Mage::getModel('catalog/product')->load($product_id)
if($product->getId()){
$product->setPrice($price)->save();
}
}
Then add a text field to your frontend page
<form action='http://site.com/modulename/Saveprice/save' method='post'>
<input type='hidden' name='product_id' value='add product id here' />
<input type='text' name='price' />
<input type='submit' value='submit' />
</form>