this is my view where there is a form containig a field and a submit button.
<form id="typeheadForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-xs-3 control-label">Movie</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="state" id="movie_name" />
</div>
</div>
<input type='submit' name='submit' value='Search' class="btn btn-squared btn-default">
</form>
and below is my controller code
public function actionMovies_all()
{
$this->layout = "main";
if ( isset( $_POST[ 'movie_name' ] ) )
{
print_r("success");die();
}
if ( Yii::$app->request->post() )
{
print_r(Yii::$app->request->post());die();
}
}
i am not able to POST the form. what am i doing wrong?
i am getting an error " Bad Request (#400)
Unable to verify your data submission."
Replace <form id="typeheadForm" method="post" class="form-horizontal"> with
<?= \yii\helpers\Html::beginForm('', 'post', ['id' => 'typeheadForm', 'class' => 'form-horizontal']);?>
You are getting bad request because when you create your form manually, you did not include csrf token into it. When you create form with Html::beginForm method it takes care about it internaly.
try this:
public function actionMovies_all()
{
$this->layout = 'main';
if ( isset( $_POST[ 'submit' ] ) )
{
print_r('success');die();
}
if ( Yii::$app->request->post() )
{
print_r(Yii::$app->request->post());die();
}
}
Related
I am trying to upload image using the code igniter. But it is giving me the following error
You did not select a file to upload
I have tried, many different sources, but still getting the error.
The PHP Code is below:
public function save_post()
{
$image_path = './uploads/';
$config['upload_path'] = $image_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_size'] = 5000;
// $config['max_width'] = 1024;
// $config['max_height'] = 1024;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('postimage')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
$this->add_post($error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->add_post($data);
}
die;
$data = array(
'page_title' => $this->input->post('ptitle'),
'page_slug' => $this->input->post('pslug'),
'page_body' => $this->input->post('pbody'),
'page_keyword' => $this->input->post('pkeywords'),
'page_description' => $this->input->post('pdesc'),
'page_image' => $this->input->post('postimage'),
);
$this->admin_model->save_post($data);
redirect(base_url() . 'admin/all_posts');
}
My View is below
<form method="post" action="<?php echo base_url(); ?>admin/save_post" enctype ="multipart/form-data ">
<div class="form-group">
<label for="postimage">Feature Image</label>
<input type="file" class="form-control" id="postimage" name="postimage" required>
</div>
<div> <?php if(isset($error)) echo $error; ?></div>
<div class="form-group">
<input type="submit" value="publish" name="submit" id="post-submit" class="btn btn-success waves-effect waves-light">
</div>
</form>
In views replace this code
<form method="post" action="<?php echo base_url(); ?>admin/save_post" enctype ="multipart/form-data ">
from this code
<?php echo form_open_multipart('admin/save_post');?>
The PHP Code is below:
enctype ="multipart/form-data " -> value inside attribute with extra spaces
replace this code
enctype ="multipart/form-data"
Controller : User.php
public function index()
{
log_message('debug', ' Index User');
die("should never get here");
}
public function login()
{
log_message('debug', ' Login Entered');
echo '<pre> Printing Login data:';
print_r($_POST);
echo "</pre>";
$data['pageHeader'] = "Login";
$data['message'] = 'temporary message - This will be the login stuff';
$this->session->set_flashdata('flashInfo', 'Login form will go here');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() === FALSE)
{
log_message('debug', ' validation run FALSE');
$this->load->helper('form');
$this->render_page('user/login', 'public_header', 'footer', $data);
}
else
{
log_message('debug', ' validation run TRUE');
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember))
{
log_message('debug', ' redirect Dashboard');
redirect('dashboard');
}
else
{
$_SESSION['auth_message'] = $this->ion_auth->errors();
$this->session->mark_as_flash('auth_message');
log_message('debug', ' redirect user login');
redirect('user/login');
}
}
}
Known from log:
function login entered,
$_POST always 'empty',
form validation-> always FALSE thus debug message is logged and form redisplayed
Form user/login.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<div class="container">
<?php echo $message;?>
<h1>Login</h1>
<form action="#" method="post">
<div class="imgcontainer">
<img src="http://www.hdkumdo.com/smen/assets/crow2.png" alt="Swordsmen Martial Arts">
</div>
<div class="container">
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" id="username" required autofocus>
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" id="password" required>
<button type="submit" name="login" id="login" value"login" >Login</button>
<label>
<input type="checkbox" checked="checked" name="remember" id="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
</div>
Copied an example from https://www.codeigniter.com/user_guide/libraries/form_validation.html
and form posts data but if I change function index to login it does not work.
Menu link : Login works fine.
Everything seems to be connected...login form displays, Controller function login is entered but it seems like there is no POST data so the form is simply redisplayed. I have 2 simple validation rules so it seems that validation->run should return true if I just enter any data into the fields.
I am sure it's something simple but for the life of me I can't see what.
First of all make sure you have loaded form validation library
$this->load->library('form_validation');
//set field validation
$this->form_validation->set_rules('fieldname','Field Name','required');
if($this->form_validation->run())
{
//then process form
//redirect
}
else {
//load view
}
I created a form and I am trying to submit data, but error:
MethodNotAllowedHttpException in RouteCollection.php line 218:
is coming. What to do?
welcome blade:
#extends('layouts.master')
#section('title')
Welcome!!
#endsection
#section('content')
#include('includes.message-block')
<div class="row">
<div class="col-md-6">
<h2>Enter Your Information</h2>
<form action="" method="post">
<div class="form-group" {{$errors -> has('name') ? 'has-error': ''}}>
<label for="name">Name</label>
<input class="form-control" type="text" name="name" id="name" placeholder="Your name">
</div>
<div class="form-group" {{$errors -> has('email') ? 'has-error': ''}}>
<label for="email">Email-Id</label>
<input class="form-control" type="email" name="email" id="email" placeholder="Enter Your Email Id">
</div>
<div class="form-group">
<label for="branch">Branch</label>
<input class="form-control" type="text" name="branch" id="branch" placeholder="Enter Your Branch">
</div>
<div class="form-group">
<label for="course_name">Course Name</label>
<input class="form-control" type="text" name="course_name" id="course_name" placeholder="Enter Your Course">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
#endsection
Model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
protected $table = 'students';
}
UserController:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
//use Illuminate\Support\Facades\Flash;
use InvalidConfirmationCodeException;
use Flash;
//use Mail;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersstudents;
class UserController extends Controller
{
public function postData(Request $request)
{
$this -> validate($request,[
'email' => 'required|email|unique:students',
'name' => 'required|max:20',
'branch' => 'required|max:10'
'course_name' => 'required|max:10'
]);
$email = $request['email'];
$name = $request['name'];
$branch = $request['branch'];
$course_name = $request['course_name'];
$student = new User();
$student->email =$email;
$student->name = $name;
$student->branch = $branch;
$student->course_name = $course_name;
$student->save();
return redirect()->back();
}
}
Route/web.php:
Route::group(['middleware' => ['web']], function(){
Route::get('/', function () {
return view('welcome');
});
Route::post('/submit_data',[
'uses' => 'UserController#postData',
'as' => 'submit_data'
]);
});
Migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->string('mail')->unique();
$table -> string('name');
$table -> string('branch');
$table -> string('course_name');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('students');
}
}
My migration's name is create_users_table. I don't understnad why this error is coming. It's an easy and simple form.Just a simple form to submit a students data, and save it in database.
You are posting to a get route
In your welcome.blade.php you forgot to set your action, so by default it will try to post to the same page / which is definded as a get route in your routes/web.php. Set the action to /submit_data like so in your welcome.blade.php:
<form action="{{ url('/submit_data') }}" method="post">
<form action="{{ url('submit_data') }}" method="post">
you can also use blade from like this
{{Form::open(array('url'=>'/submit_data','files'=>true,'class'=>'form-horizontal','file'=>'true'))}}
I am trying to edit a picture that I've already uploaded, so when Click on edit i cant not get the name of the image in the input. here is my code in controller:
It means I want to edit the details of an item but i don't want to edit the image of it.
controller
//method to edit category
public function edit_menu($id) {
if($this->isLogin()) {
//object/instance of menu
$menu_list = new Menu();
//calling method of , menu model
$menu_list->load($id);
//print_r($menu_list);
$menu_list_array = (array)$menu_list;
//object/instance of category
$category = new Category();
//calling method of category model
$category_list = $this->Category->get();
//creating an array to hold data
$category_form_options = array();
//iterate through loop
foreach ($category_list as $id => $category) {
$category_form_options[$id] = $category->category_name;
}
//creating an array to hold data for category and menu, to pass data to view
$data = array();
//category key of data array to hold category details
$data['category'] = $category_form_options[$menu_list_array['category_id']];;
//menu key of data array to hold menu details
$data['menu'] = $menu_list;
//CI built-in method to have three params, first is field id, second is human readable lable to show in case of error, rules include required, type.
$this->form_validation->set_rules(array(
array('field' => 'category_id','label' => 'Category', 'rules' => 'required',),
array('field' => 'type','label' => 'Type','rules' => 'required',),
array('field' => 'menu_name', 'label' => 'Menu Name', 'rules' => 'required',),
array('field' => 'description','label' => 'Description','rules' => 'required',),
));
if (empty($_FILES['avatar']['name'])){
$this->form_validation->set_rules('avatar', 'Avatar', 'required');
}
//CI method to error delimiters,
//https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#errordelimiters
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
if (!$this->form_validation->run()) {
$datas['title'] = "";
$this->load->view('header', $datas);
//this will load data
$this->load->view('category_form_edit', array('category_form_options' => $category_form_options, 'data' =>$data, ));
$this->load->view('footer');
}
else {
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->do_upload('avatar');
$data_upload_files = $this->upload->data();
//record from global post variable to model fields.
$menu_data = array();
$menu_data['category_id'] = $menu_list->category_id = $this->input->post('category_id');
$menu_data['type'] = $menu_list->type = $this->input->post('type');
$menu_data['menu_name'] = $menu_list->menu_name = $this->input->post('menu_name');
$menu_data['description'] = $menu_list->description = $this->input->post('description');
$menu_data['per_gram'] = $menu_list->per_gram = $this->input->post('per_gram');
$menu_data['per_quantity'] = $menu_list->per_quantity = $this->input->post('per_quantity');
$menu_data['per_dozen'] = $menu_list->per_dozen = $this->input->post('per_dozen');
$menu_data['avatar'] = $menu_list->avatar = $data_upload_files['file_name'];
$menu_data['updated_by'] = $menu_list->updated_by = $_SESSION['user_id'];
$menu_data['updated_on'] = $menu_list->updated_on = date('Y-m-j H:i:s');
//update model
$id = $menu_list_array['menu_id'];
$where = array('menu_id' => $id);
$menu_list->update('menus', $menu_data, $where);
$this->load_menu();
}
}
}
view
<?php echo form_open_multipart('', 'fscontrol/edit_menu');?>
<!-- <form method="post"> -->
<div class="form-group">
<label for="category_id">Category Name</label>
<select name="category_id" class="form-control" >
<?php
foreach ($category_form_options as $category_id => $category_name) {
$selected = $data_array['category_id'] == $category_id ? 'selected' : '';
echo '<option ' . $selected. ' value="' . html_escape($category_id) . '">' . html_escape($category_name) . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="type">Type</label>
<input type="text" class="form-control" name="type" value="<?php echo $data_array['type']; ?>">
</div>
<div class="form-group">
<label for="menu_name">Menu Name</label>
<input type="text" class="form-control" name="menu_name" value="<?php echo $data_array['menu_name']; ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="<?php echo $data_array['description']; ?>">
</div>
<div class="form-group">
<label for="per_gram">Per gram</label>
<input type="text" class="form-control" name="per_gram" value="<?php echo $data_array['per_gram']; ?>">
</div>
<div class="form-group">
<label for="per_quantity">Per quantity</label>
<input type="text" class="form-control" name="per_quantity" value="<?php echo $data_array['per_quantity']; ?>">
</div>
<div class="form-group">
<label for="per_dozen">Per dozen</label>
<input type="text" class="form-control" name="per_dozen" value="<?php echo $data_array['per_dozen']; ?>">
</div>
<div class="form-group">
<label for="avatar">Photo</label>
<input type="file" class="form-control" name="avatar" size="20" value="<?php echo $data_array['avatar']; ?>">
</div>
<div class="form-group">
<input type="hidden" name="menu_id" value="<?php echo $data_array['menu_id']; ?>">
<input type="submit" class="btn btn-default" value="Edit"/>
</div>
</form>
If you are using bootstrap template you will not get the image name. You'll see only:
no file chosen
So it is better to use:
<img src="">
, and display image as a thump nail.
I have a RESTful API based application with Laravel 4 and Angular.js.
The application's CRUD processes are handled by angularjs $http service.
The Backend Side (Laravel 4):
Routing : app/routes.php
//.....
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
//....
Route::resource('pages', 'PagesController');
//....
});
//.....
Controller : app/controllers/api/PageController.php
<?php
//.....
class PagesController extends BaseController {
//......
public function update($id) {
$page = Page::find($id);
if ( Request::get('title') )
{
$page->title = Request::get('title');
}
if ( Request::get('slug') )
{
$page->slug = Request::get('slug');
}
$page->save();
return Response::json(array(
'error' => false,
'message' => 'Page Updated'),
200
);
}
//......
}
Calling : cURL
This update function can be accessed using cURL method also.
curl -i -X PUT --user admin:admin -d 'title=Updated Title' localhost/laravel/index.php/api/v1/pages/2
Front-end : HTML
<!-- Top Code -->
<!-- From to Add/Edit Pages -->
<form class="form-horizontal" role="form" ng-show="edit" ng-submit="updatePage(entry)">
<!-- Page Title -->
<div class="form-group">
<label class="col-lg-2 control-label">Page Title</label>
<div class="col-lg-4">
<input type="text" class="form-control" value="{{entry.title}}" ng-model="entry.title">
</div>
</div>
<!-- Slug -->
<div class="form-group">
<label class="col-lg-2 control-label">Slug</label>
<div class="col-lg-4">
<input type="text" class="form-control" value="{{entry.slug}}" ng-model="entry.slug">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
<!-- Bottom Code -->
Client-side : angularjs
// ......
function pageCtrl($scope, $http, Data) {
//.........
$scope.updatePage = function(entry) {
$http({method: 'PUT', url: Data.root_path + 'api/v1/pages/'+id}).
success(function(data, status, headers, config) {
//
}).
error(function(data, status, headers, config) {
//
});
}
//.........
}
Question:
How can I pass my form data(more than one values) to the $http.put request
here ?
How can I access the PUT request data in Laravel 4 Controller ? Can
I use Input::get() ?
Need some update in your html to get page id to update. Add the following html inside form.
<input type="hidden" ng-model="entry.id" value="entry.id"/>
Then change angular script to,
$scope.updatePage = function(entry) {
$http.put(Data.root_path + 'api/v1/pages/' + entry.id, entry)
.success(function(data, status, headers, config) {
//
})
.error(function(data, status, headers, config) {
//
});
}
And in your Laravel Controller,
public function update($id) {
$page = Page::find($id);
$input = $input = Input::all();
if ( $input['title'] )
{
$page->title = $input['title'];
}
if ( $input['slug'] )
{
$page->slug = $input['slug'];
}
$page->save();
return Response::json(array(
'error' => false,
'message' => 'Page Updated'),
200
);
}