I am using codeigniter for developing my website.When submitting form Iam variables separated with ? and &. I want variables separated with slashes like below.
Now getting like this
http://example.com/controller/method?id=22&sd=31
I want an url like below
http://example.com/controller/method/22/31
Html Code is given below
<form action="<?php echo base_url();?>controller/method" method="get">
<div class="form-group">
<select id="id" name="id" class="selectcl form-control" data-live-search="true" title="Pick Your place" >
<option value="22" >22</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="sd" id="sd" placeholder="Enter details" title="please choose">//getting values here by autocomplete
</div>
<div class="form-group">
<button type="submit" class="btn find" value="submit" ></i>Start </button>
</div>
</form>
Tried URI Routing and below
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
Please help me to find a solution.
change your form method get to post
<form id="form" action="<?php echo base_url('controller/method');?>" method="post">
add following jquery in the page.
$(function(){
$('button[type="submit"]').on('click', function(e){
e.preventDefault();
var id = $.trim($('#id').val());
var sd = $.trim($('#sd').val());
if(id.length > 0 && sd.length > 0){
var url_string = new URL($('#form').attr('action'));
window.location.href = url_string.href.concat(((url_string.href.endsWith('/')) ? '' : '/'),id,'/',encodeURIComponent(sd));
}
});
});
If you want to pass the value through URL, Try the below link:
Send form input value to URL
Related
I am trying to attach a file to a blog type post. For this i have an file field and 2 buttons, one saves the fields to the database and the other uploads the file. standalone the file upload works as intended. However in the form i get Call to a member function store() on null. I have changed the menthod from put to post, but that doesnt seem have any effect.
Below my post forms and the function in the controllers.
The Form:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">New/Edit Blog</div>
<div class="card-body">
#if($data)
<form … action = "{{Route ('post.update', $data->_id)}}" method="post", enctype = 'multipart/data'>
#csrf
#method('POST')
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title" value = "{{$data->title}}" >
</div>
<div class="form-group">
<label for="shorttext">Shorttext:</label>
<input type="text" class="form-control" name="shorttext" value = "{{$data->shorttext}}" >
</div>
<div>
<input id="x" type="hidden" name="content" value ="{{$data->content}}">
<trix-editor input="x" ></trix-editor>
</div>
<div class="form-group">
<label for="created_by">Created By:</label>
<input type="text" class="form-control" name="created_by" value = "{{$data->created_by}}" readonly>
</div>
<input type="file" name="attachment" enctype="multipart/form-data"/>
<p align="center">
<input type="submit" btn btn-primary class="btn btn-primary" id="save" name="action" value="save">
<input type="submit" btn btn-primary class="btn btn-primary" id="upload" name="action" value="upload">
The functions in the controller:
Store:( this one is the function that determines which button is pressed)
public function store(Request $request, $_id = false, $attachment = false){
//check which submit was clicked on
if($request->action == 'upload'){
//
$this->upload($request);
return redirect()->route('home');
} elseif($request->action == 'save') {
//echo 'save pressed';
//run function save all form fields
$this->update($request, $_id);
return redirect()->route('home');
} else {echo "error";}
}
Upload function:
function upload(Request $request){
$path = $request->file('attachment');
// $original = $request->file('attachment')->getClientOriginalName();
$path->store('/public');
Update function:
public function update (Request $request, $_id){
//$this->upload($request);
/*
$path = $request->file('attachment');
$path->storeas('/public','123'); */
$data = post::findOrFail($_id);
$data->title = $request->title;
$data->content = $request->content;
$data->shorttext = $request->shorttext;
$data->created_by = $request->created_by;
$data->text3 = $request->text3;
$data->attachment = $request->attachment;
$data->save();
if($data){
return redirect()->route('home');
}else{
return back();
}
}
The route is:
Route::post('/post/update/{_id}', 'PostController#store')->name('post.update');
As mentioned, the save function in the complete form works. In a standalone form ( and a standalone controller) the upload works (and i can, if i wish, manipulate the filename), but how do i bring the 2 together? At first i thought it was because the update form had a PUT method, but changing everything to post doesnt seem to have any effect and i still get the Null error.
For completeness the standalone solution:
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Te7aHoudini\LaravelTrix\Pipes\AttachmentInput;
class UploadController extends Controller
{
//
function upload(Request $req){
$path = $req->file('attachment');
$original = $req->file('attachment')->getClientOriginalName();
$path->storeas('/public',$original);
echo $original;
}
}
The standalone form:
<html>
<head>
<title>Upload</title>
</head>
</html>
<form action="upload" method="POST" enctype="multipart/form-data">
<input type="file" name="attachment" />
#csrf
<button type="submit">file upload</button>
</form>
In your form element Change enctype = 'multipart/data' to enctype="multipart/form-data"
<form … action = "{{ Route ('post.update', $data->_id) }}" method="post", enctype="multipart/form-data">
Hope this solves your problem.
I'm trying to pass ajax data to a "settings/addsection/{class_id}" route with parameter so as to call a controller method to log on laravel log file. but logging does not work. So the ajax didn't send data to the route. How do I fix this?.. Note that the $class_id which i passed to the hidden input has value as i have queried it from the database.
JAVASCRIPT
<script>
document.addEventListener('DOMContentLoaded' , function(){
var add = document.getElementById('addSection');
add.addEventListener('submit' , function(e){
e.preventDefault();
var class_id = document.getElementById('class_id').value;
var section = document.getElementById('name').value;
var token = document.getElementsByName('_token')[0].value;
var data = {
name:section,
_token:token
}
var xhr = new XMLHttpRequest();
xhr.open('POST', 'settings/addsection/' + class_id, true);
xhr.setRequestHeader('Content-Type' , 'application/json');
xhr.send(JSON.stringify(data));
})
})
</script>
HTML
form id = "addSection" method="POST" action=" {{ url('settings/addsection/' .$class_id)}}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('add section') }}</label>
<div class="col-md-6">
<input id="name" name="name" type="text" class="form-control #error('name') is-invalid #enderror" value="{{ old('name') }}" autocomplete="name" autofocus placeholder="A,B,C...Z">
<input type="hidden" id = "class_id" value = "{{$class_id}}">
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary btn-block">
{{ __('Add add section') }}
</button>
</div>
</div>
</form>
this is the web.php where the routes are
Route::middleware(['auth' , 'admin'])->group(function(){
Route::prefix('settings')->group(function(){
Route::post('addsection/{class_id}' , 'SectionController#store');
});
});
SectionController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Section;
class SectionController extends Controller
{
public function store(Request $request , $class_id){
//do stuff here
Log::info(' it works');
}
}
I worked around this problem by changing this
xhr.open('POST', 'settings/addsection/' + class_id, true);
to
xhr.open('POST', "{{url('settings/addsection/')}}" + '/' +class_id, true);
my input for example :
<div class="form-group">
<label for="name">New Name Offers :</label>
<input name="newNameOffers" class="form-control" id="newNameOffers">
</div>
<div class="form-group">
<label for="">From Email :</label>
<input name="fromEmail" placeholder="Email" class="form-control" id="fromEmail">
</div>
How can i put it on text area ....
That my textArea :
<textarea class="form-control" name="HEADER" id="HEADER" rows="11">i need to dispay my values here <textarea>
(i need to put the values not the name of variable)
I have tried to make a simple form for you. You can modify it as you wish. For this, you need to take help of javascript or other similar front end technologies. However, if your requirement is just to take the value to back end then you can get both value $request->newNameOffers and $request->fromEmail then you can concatenate and store in your desired way.
function myFunction() {
var val1 = document.getElementById("newNameOffers").value;
var val2 = document.getElementById("fromEmail").value;
var res = val1 +' '+ val2;
document.getElementById("HEADER").innerHTML = res;
}
<!DOCTYPE html>
<html>
<body>
newNameOffers:<br>
<input name="newNameOffers" onChange="myFunction()" class="form-control" id="newNameOffers">
<br>
fromEmail:<br>
<input name="fromEmail" placeholder="Email" class="form-control" onChange="myFunction()" id="fromEmail">
<p>Click the button to alert the contents of the text area.</p>
<!-- <button type="button" onclick="myFunction()">Try it</button> -->
<br>
<textarea id="HEADER"></textarea>
<script>
</script>
</body>
</html>
Why do you want to put it using Laravel, You can do it in the front-end(JavaScript, JQuery, Vue.js, React) itself.
I am using ajax but it behaves different. I just want code that I should
in Model , VIEW,Controller.
This while contain two operation insertion and fetching the data from that db.
![In image their is subject and textarea as input.When i click submit the input
dispaly in comment which is just above the Subject input]1
View
<form action="" method="POST">
<input type="hidden" name="myId" id="myId" value="" />
<div class="form-group px-4">
<label for="subject">Subject</label>
<input type="text" id="js_subject" name="js_subject">
</div>
<div class="form-group px-4">
<label for="exampleFormControlTextarea1">Example textarea</label>
<textarea class="form-control" name = "js_text" id="js_text" rows="3"></textarea>
</div>
<input type="submit" id="comment-info" value="submit" name="submit" class="btn btn-primary">
</form>
Use an jQuery Ajax request,and on somepage.php use if and else for insert and select and
echo some message for both and die();
$('form').on('submit', function(event){
event.preventDefault();
$.ajax({
type:'post',
url:'somepage.php',
data:$(this).serialize(),
success:function(data){
var tmp = data;
if(data = "message1"){
//do whatever
}else if(data = "message2"){
//do whatever
}
})
});
I am trying to get a dynamic script for nested comments to work.
My first problem is that I don't know how I can do endless nesting. For now I planned to do 3 layers, cause I don't know how to make it work dynamicly.
The second problem is that when i submit the form, the values of the models is not submitted to the JS-script. The values are just undefined.
I guess my approach is just wrong - The ng-model elements are not bound inside of the ng-repeat, also the values of all forms would be bound to the same element... Maybe someone has some tips. If it is important, my backend runs with Laravel 4. Here is my code
var commentsApp = angular.module('commentsApp', []);
function CommentsCtrl($scope, $http, $compile) {
var url_segments = window.location.host.split('.');
var section = url_segments[0];
$http.get('/api/' + section + window.location.pathname + '/comments').success(function (comments) {
$scope.comments = comments;
});
$scope.toggleForm = function (id) {
var container = document.getElementById(id);
var html = '<br/><input name="category" type="text" ng-model="person.category" placeholder="Category" required/><span class="alert alert-error ng-show="add-bro.input.$error.required">Required</span>';
var elem = $compile(html)($scope);
angular.element(container).append(elem);
}
$scope.addComment = function () {
var comment = {
body: $scope.body,
commentable_id: $scope.commentable_id,
commentable_type: $scope.commentable_type
};
$scope.comments.push(comment);
};
}
commentsApp.controller('CommentsCtrl', CommentsCtrl);
<div class="content-row basic" ng-controller="CommentsCtrl" class="comments">
<form ng-submit="addComment()">
<input type="text" placeholder="Add Comment" ng-model="body">
<input type="hidden" value="#{{c.id}}" ng-model="commentable_id">
<input type="hidden" value="Player" ng-model="commentable_type">
<button type="submit">Add Comment</button>
</form>
<div ng-repeat="c in comments" class="comment">
<span>#{{c.author.username}}</span>
<p>#{{c.body}}</p>
<a href class="reply-link" ng-click="showForm = !showForm">Answer</a>
<div class="reply-container" ng-show="showForm">
<form ng-submit="addComment()">
<input type="text" placeholder="Add Comment" ng-model="body">
<input type="hidden" value="#{{c.id}}" ng-model="commentable_id">
<input type="hidden" value="Comment" ng-model="commentable_type">
<button type="submit">Add Comment</button>
</form>
</div>
<div ng-repeat="cc in c.comments" class="s-comment">
<span>#{{cc.author.username}}</span>
<p>#{{cc.body}}</p>
<a href class="reply-link" ng-click="showForm = !showForm">Answer</a>
<div class="reply-container" ng-show="showForm">
<form ng-submit="addComment()">
<input type="text" placeholder="Add Comment" ng-model="body">
<input type="hidden" value="#{{c.id}}" ng-model="commentable_id">
<input type="hidden" value="Comment" ng-model="commentable_type">
<button type="submit">Add Comment</button>
</form>
</div>
<div ng-repeat="ccc in cc.comments" class="ss-comment">
<span>#{{ccc.author.username}}</span>
<p>#{{ccc.body}}</p>
<a href class="reply-link" ng-click="showForm = !showForm">Answer</a>
<div class="reply-container" ng-show="showForm">
<form ng-submit="addComment()">
<input type="text" placeholder="Add Comment" ng-model="body">
<input type="hidden" value="#{{c.id}}" ng-model="commentable_id">
<input type="hidden" value="Comment" ng-model="commentable_type">
<button type="submit">Add Comment</button>
</form>
</div>
</div>
</div>
</div>
</div>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
//Comments object having reply oject
$scope.comments = [{ comment: 'hi', reply: [{ comment: 'hi inside commnet' }, { comment: 'hi inside commnet' }] }];
//push reply
$scope.insertReply = function (index, reply) {
$scope.comments[index].reply.push({ comment: reply });
}
//push commnet
$scope.newComment = function (comment) {
$scope.comments.push({ comment:comment, reply: [] });
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<!--Comment section-->
<ul ng-repeat="comment in comments track by $index" style="background: skyblue; padding: 10px;">
<li>
<b>Comment {{$index}} : </b>
<br>
{{comment.comment}}
<!--Reply section-->
<ul ng-repeat="reply in comment.reply track by $index">
<li><i>Reply {{$index}} :</i><br>
{{reply.comment}}</li>
</ul>
<!--End reply section-->
<input type="text" ng-model="reply" placeholder=" Write your reply." />Reply
</li>
</ul>
<!--End comment section -->
<!--Post your comment-->
<b>New comment</b>
<input type="text" placeholder="Your comment" ng-model="comment" />
Post
</div>