Using Cjax to POST data in CodeIgniter - php

I'm trying to POST some data using Cjax in CodeIgniter.
My view is:
<?php
require_once(FCPATH . 'ajaxfw.php');
$ajax->click('#subscribesubmit' , $ajax->form('ajax.php?subscriber/add/'));
?>
<div class="col-md-4">
<form class="form-inline subscribe-box" role="form" method="post">
<div class="form-group">
<label class="sr-only" for="subscribemail">Email address</label>
<input type="text" class="form-control" id="subscribemail" name="subscribemail" placeholder="Enter email">
</div>
<button type="submit" class="btn btn-default" id="subscribesubmit">Subscribe</button>
</form>
</div>
This view is loaded in controller index().
My subscriber controller:
class Subscriber extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('email');
$this->load->model('subscriber_model');
require_once(FCPATH.'ajaxfw.php');
}
public function add($subscribemail) {
$ajax = ajax();
//$email = $this->input->post('subscribemail');
echo $subscribemail;
$data['status'] = $this->subscriber_model->new_subscriber($subscribemail);
}
}
}

Try this:
in your code block replace to:
require_once(FCPATH . 'ajax.php');
instead of:
require_once(FCPATH . 'ajaxfw.php');

You should include ajax.php instead of ajaxfw.php (ajax.php would include itself ajaxfw.php if it needs to)

Related

404 not found error, can't redirect to another page/view

I am having a problem with the routing.
Firstly, I am on the page (rate product)/{$id}
Next, when clicking the button, I want to go to another view (addReview/{$productID}/{$clientID}) But it gives me an error.
-web.php file
Route::get('rateProduct/{id}', 'CatalogController#getProduct')
->name('rateProduct');
Route::post('./addReview/{$productID}/{$clientID}','ReviewController#addReview')
->name('rateProduct.addReview');
add_review.blade.php
<div class="mb-3">
<form method="post" action="{{url('rateProduct.addReview', [$product->id, 10]) }} value="{{ csrf_token() }}"">
{{--{{ csrf_field() }}--}}
<label for="exampleFormControlTextarea1" class="form-label">Share your thoughts!</label>
<textarea class="form-control rounded" id="exampleFormControlTextarea1" rows="3"
placeholder="Tell us about your experiecnce in a couple of sentences" name="comment"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
ReviewController.php
class ReviewController extends Controller
{
public function addReview($productID, $clientID)
{
if(isset( $_POST['submit'])) {
$comment = $_POST["comment"];
}
$date = date_create();
$reviewID = DB::table('review')->max('id') + 1;
$data = array(
'id'=>$reviewID,
"comment"=>$comment,
"review_date"=>date_timestamp_get($date),
"rating"=>1,
"client_id"=>$clientID
);
DB::table('review')->insert($data);
$data2 = array('product_id'=>$productID,"review_id"=>$reviewID);
DB::table('product_review')->insert($data2);
return view('pages/product-page');
}
}
Review model
class Review extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'Review';
public function owner() {
return $this->belongsTo('App\Models\Product');
}
}
You have error in routing .Remove dot from the beginning(./addReview/{$productID}/{$clientID}) of string in post method.
Route::post('addReview/{$productID}/{$clientID}','ReviewController#addReview')->name('rateProduct.addReview');
Also you are using url method for named routing in form action it should be
<form method="post" action="{{route('rateProduct.addReview', [$product->id, 10]) }}" >
#csrf
Firstly you have a random . at the start of your route and $ symbols in your parameters, they needs to go.
Route::post('/addReview/{productID}/{clientID}','ReviewController#addReview')
->name('rateProduct.addReview');
Next you need to fix the invalid markup of your add_review blade file, you have mismatched opening/closing elements. You are also using a named route but using the url helper which works with paths rather than route names. You also want to add back in the #csrf token back in otherwise you'll get a 419 error.
<div class="mb-3">
<form method="post"
action="{{ route('rateProduct.addReview', [$product->id, 10]) }}">
#csrf
<label for="exampleFormControlTextarea1" class="form-label">
Share your thoughts!
</label>
<textarea class="form-control rounded"
id="exampleFormControlTextarea1"
rows="3"
placeholder="Tell us about your experiecnce in a couple of sentences"
name="comment">
</textarea>
<button type="submit" class="btn btn-primary">Send</button>
</form>
</div>
You can see a working example here.

how can insert data post with get route in Laravel

how do i send a form POST method with GET route in laravel?
Route
Route::get('domain_detail/{domain_name}','domain_detailController#index');
View domain_detail folder
<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
<div class="form-group">
<label for="namefamily">namefamily</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
</div>
<div class="form-group">
<label for="mobile">mobile</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
</div>
<div class="form-group">
<label for="myprice">myprice</label>
<input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
</div>
<div class="form-group">
<input type="submit" name="send_price" class="btn btn-success" value="submit">
</div>
</form>
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class domain_detailController extends Controller
{
public function index($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
public function create()
{
return view('domain_detail.index');
}
}
At the controller i didn't put any codes in the create function, but when i click on submit button in form i get this error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Use the index function in your domain_detailController just so return the view.
like this:
public function index($domain_name)
{
return view('domain_detail.index');
}
create a route to return the view:
Route::get('domain_detail/','domain_detailController#index');
Then use the create function to store the domain detail like this:
public function create($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
make a POST route like this:
Route::post('domain_detail/','domain_detailController#create');
Also take a look at the laravel best practices when it comes to naming conventions:
https://www.laravelbestpractices.com/

Laravel: Send an array through route

I was trying to send an array through a route to another view, but when i used the function get_defined_vars(), i realized that i was sending a string with the information. Is it possible to do that?
this form from my view should send the array to my route
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value="{{$cooperado}}">
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
then this route should send the array to the other view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with(['j'=>$j]);
});
this is the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create()
{
//
return view('movs.create');
}
}
routes.php
Route::post('/trans', 'MovimentacoesController#create');
controller
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create(Request $request)
{
$j = $request->request->get('r');
return view('movs.create')->with(['j' => $j]);
}
}
Code like this In the form tag:
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
submit this form
then the Input::get('r') will be Array!
I hope it helps you.

Edit page show blank in laravel

I have news details inserted and i need to show it on the edit page but when i try to edit and delete it shows blanks page insert and show is working properly. i have been stucked on this from morning. id is getting from the database but it shows a blank page,Not using any Form helper
1.what's problem,is it on route file
2.is it on Controller file
route.php
Route::get('/', function () {
return view('welcome');
});
Route::resource('books','BookController');
Route::resource('news','NewsController');
Auth::routes();
Route::get('/news','NewsController#index')->name('news');
//Route::get('/news/create','NewsController#create');
//Route::get('/news/edit','NewsController#edit');
Edit.blade.php
#extends('theme.default')
#section('content')
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
NEW BEE NEWS DETAILS
</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
<form method="post" action="{{route('news.update',[$news->id])}}"
enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label>NEWS TITLE</label>
<input type="text" name="atitle" id="atitle" class="form-control"
placeholder="PLEASE ADD TITLE OF NEWS" value="{{$news->name}}">
<p class="help-block">Example: SELFY PLAYSHARE </p>
</div>
<div class="form-group">
<label>NEWS</label>
<textarea name="news" id="news" class="form-control" {{$news->news}}></textarea>
<p class="help-block">DETAILED NEWS HERE</p>
</div>
<div class="form-group">
<label>NEWS LINK</label>
<input type="text" name="alink" id="alink" class="form-control"
placeholder="PLEASE ADD LINK OF NEWS" value="{{$news->alink}}">
<p class="help-block">Example: https://play.google.com/store/apps/selfyplusure</p>
</div>
<div class="form-group">
<label>NEWS IMAGE</label>
<input type="file" name="addimage" id="addimage" value="{{$news->imagename}}">
</div>
<button type="submit" class="btn btn-default">ADD NEWS</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
NewsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
public function index()
{
$news = News::all();
return view('news.index', ['news' => $news]);
}
public function create()
{
return view('news.create');
}
public function store(Request $request)
{
$news=new News();
if($request->hasFile('addimage')){
$request->file('addimage');
$imagename=$request->addimage->store('public\newsimage');
$news->name = $request->input('atitle');
$news->alink = $request->input('alink');
$news->news = $request->input('news');
$news->imagename = $imagename;
$news->save();
if($news) {
return $this->index();
} }
else{
return back()->withInput()->with('error', 'Error Creating News ');
}
}
public function show(News $news)
{
//
}
public function edit(News $news)
{
$news=News::findOrFail($news->id);
return view('news.edit',['News'=>$news]);
}
public function update(Request $request, $id)
{
$news = News::findOrFail($id);
// update status as 1
$news->status = '1';
$news->save();
if ($news) {
// insert datas as new records
$newss = new News();
//On left field name in DB and on right field name in Form/view
$newss->name = $request->input('atitle');
$newss->alink = $request->input('alink');
$newss->news = $request->input('news');
$newss->imagename = $request->input('addimage');
$newss->save();
if ($newss) {
return $this->index();
}
}
}
public function destroy($id)
{
$news = News::findOrFail($id);
$news->status = '-1';
$news->save();
if ($news) {
return $this->index();
}
else{
return $this->index();
}
}
}
Link to delete and Edit
<td><input type="button" name="edit" value="EDIT">
<td><input type="button" name="delete" value="DELETE"></td>
This is the link to edit
<td><input type="button" name="edit" value="EDIT">
For delete please go through
Delete
In your controller try to use this.
return view('news.edit',compact('news'));

how to create search function in codeigniter

I want to create simple search function. I follow some example. but I was unable to get results. please help me.
//view page
<form class="navbar-form" role="search" action=" {{ base_url }}search/search_keyword" method = "post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name = "keyword"size="15px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
//controller
function search_keyword()
{
$keyword = $this->input->post('keyword');
$data['results'] = $this->mymodel->search($keyword);
$this->twig->display('result_view.php',$this->data);
//$this->load->view('result_view.php',$data);
}
}
//model
function search($keyword)
{
$this->db->like('item_name',$keyword);
$query = $this->db->get('bracelets');
return $query->result();
}
Change
$this->twig->display('result_view.php',$this->data);
TO
$this->load->view('result_view.php',$data);
Use This Function
function search_keyword()
{
$keyword=$this->input->post('keyword');
$data['results']=$this->mymodel->search($keyword);
$this->twig->display('result_view.php',$data);
}

Categories