How to remember last tab used with PHP form submit? - php

I have a form inside one of many tabs, but when it submits it goes back to the first one.
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-control">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tab" aria-expanded="true">Tab1</a></li>
<li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">Tab2</a></li>
</ul>
</div>
<h3 class="panel-title">Text</h3>
</div>
<div class="panel-body">
<div class="tab-content">
<div id="tab" class="tab-pane fade active in">
<form class="form-group" method="post" action="#" enctype="multipart/form-data">
<input class="form-control description" name="description[]" placeholder="Description here">
<input class="btn btn-primary btn-lg" name="update" type="submit" value="Save">
</form>
</div>
<div id="tab-2" class="tab-pane fade">
</div>
</div>
</div>
</div>
PHP fetches form and update database and redirects:
if(isset($_POST['update'])){
//database update
//current url + tab
header("Location: {$url->getCurentUrl()}#tab3");
}
The link is correct, but the tab won't update using the #tab in url, anyone have alternative methods to set active tab to last tab used?

Use this below code ...might be helpful to you as it's for me so...
$('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
localStorage.setItem('activeTab', $(e.target).attr('href'));
});
var activeTab = localStorage.getItem('activeTab');
if(activeTab){
$('#action-tab a[href="' + activeTab + '"]').tab('show');
}

Related

jquery submit form how to make other page

When I query from a page, I submit the form with jquery in sorgu.php. but how do i do it without making it infinite loop?
is there any other way ?
page link
header.php code search mod
<div class="modal fade" id="srcModal" tabindex="-1" role="dialog" aria-labelledby="contacthModal">
<div class="row searchArea modal-dialog modal-content" role="document">
<div class="container ">
<div class="row searchCloserow">
<div class="closeBtn" data-dismiss="modal"> <i class="fas fa-times" aria-hidden="true"></i> Kapat </div>
</div>
<div class="row searchForm d-flex justify-content-center">
<div class="col-md-2 searchText">
<span id="myModalLabel">Sipariş Sorgulama Ekranı</span>
<p>Sorgulamak istediğiniz siparişin kodunu giriniz!</p>
</div>
<div class="col-md-4 searchwell">
<input type="text" name="siparis_key" id="siparis_key" placeholder="Lütfen Sipariş Kodunu Giriniz">
</div>
<div class="col-md-2">
<a href="sorgu" id="siparis_Sorgula" type="submit" class="searchbtn anibut">
Sipariş Sorgula <i class="fas fa-question" aria-hidden="true"></i>
</a>
</div>
</div>
</div>
</div>
and header.php jquery code. here i am changing the href href="sorgu?key=asdasdasd"
$("#siparis_Sorgula").click(function(){
$data = $('#siparis_key').val();
$href = $("#siparis_Sorgula").attr('href');
$href = $href+"?key="+$data;
$("#siparis_Sorgula").attr("href", $href);
});
sorgu.php page code
<form action="" method="post" id="jsSorgula">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="siparis_key" id="siparis_keyId" placeholder="<?php echo $dil['sipariskodu'] ?>" class="form-control" required>
</div>
</div>
<div class="col-lg-12 col-md-12">
<button type="submit" class="btn btn-primary btn-block"><?php echo $dil['sorgula'] ?></button>
</div>
</div>
</form>
sorgu.php jquery code
<script type="text/javascript">
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
$count = 0;
$(document).ready(function(){
if ($count==0) {
$keys = getUrlVars()["key"];
$('#siparis_keyId').val($keys);
$("#jsSorgula").submit();
$count++;
alert($count);
}
break;
});
The infinity loop could came from your $count = 0. Is set to 0 each time you make a request on that page. Instead of make the validation with count == 0, you should use a method like "on click" or "on change".

Livewire component not working properly when call from form hide / show?

I am using the LiveWire Media component.
Here is my component called in blade file.
Below component called on page load.
<div class="form-group">
<livewire:media title="Browse Image" name="media_image_id" type="select" :options="['image']" :selectedMedias="$course->mediaImage ?? null" :unique-key="$unique_key" :table="$table" id="media_image"/>
</div>
After that on button click one form opened called testform.
In that form below code is written.
<div class="form-group">
<livewire:media title="Browse Image" name="lesson_media_image_id" type="select" :options="['image']" :selectedMedias="$lesson->mediaImage ?? null" :unique-key="$unique_key" :table="$tab" id="lesson_media_image"/>
</div>
Media.blade.php
#section('title','Media')
<div>
<div>
#foreach($selectedMedias as $selectedMedia)
<input type="hidden" name="{{$name}}" value="{{$selectedMedia->id}}" id="{{$name}}"/>
<div class="row">
<div class="col-6">
<div class="card">
#switch($selectedMedia->collection_name)
#case('images')
<img src="{{$selectedMedia->getUrl()}}" class="img-fluid rounded float-left"
alt="{{$selectedMedia->name}}"
style="max-height: 150px;"/>
#break
#endswitch
<div class="card-body">
<h6 class="card-title">
<div>{{$selectedMedia->name}}</div>
</h6>
</div>
</div>
</div>
</div>
#endforeach
</div>
<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mediaModal-{{$name}}">
{{$title}}
</button>
<!-- The Modal -->
<div class="modal fade" id="mediaModal-{{$name}}">
<div class="modal-dialog modal-xl modal-dialog-scrollable">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h5 class="modal-title">Media Library</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<!-- Nav pills -->
<ul class="nav nav-pills mb-2">
#if(in_array('image',$options))
<li class="nav-item">
<a class="nav-link {{$options[0]==='image'?'active':''}}" data-toggle="pill"
href="#image-show">
All Images
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#image-upload"
data-url="{{route('media.upload',['uniqueKey' => $uniqueKey,'table'=>$table,'tenant'=>tenantName()])}}">
Upload Image
</a>
</li>
#endif
</ul>
<!-- Tab panes -->
<div class="tab-content">
#if(in_array('image',$options))
<div class="tab-pane container {{$options[0]==='image'?'active':''}}" id="image-show">
<livewire:media.show-images :type="$type" :select="$select" :name="$name"/>
</div>
<div class="tab-pane container fade" id="image-upload">
<livewire:media.upload-images/>
</div>
#endif
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
Now problem is when on page load component is called modal is open and can select and upload image.
When I comment on page load component at that time I able to active upload button tab, but unable to open or drag and drop image as well as strore in database.
when form is show above component is called modal is also open but when click on upload tab it will show selected but upload form is not become active also image is also not able to select from all images.

How to submit my form without reloading with ajax?

I would like to submit my form without reloading the page after pressing the submit button.But everything I have tried doesn't worked.Can you give me some help with ajax code please?
My view:
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" style="border-bottom: none;padding: 0px 0px;right: 15px;top: 10px;position:relative">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<section>
<div class="wizard col-md-6" >
<div class="wizard-inner">
<div class="connecting-line"></div>
<ul style="border:none !important" class="nav nav-tabs" role="tablist" style="margin: 0px auto">
<li role="presentation" class="active">
<a href="#step1" data-toggle="tab" aria-controls="step1" role="tab" title="Step 1">
<span class="round-tab">
<i class="icon-pencil"></i>
</span>
</a>
</li>
<li role="presentation" class="disabled">
<a href="#step2" data-toggle="tab" aria-controls="step2" role="tab" title="Step 2">
<span class="round-tab">
<i class="icon-note"></i>
</span>
</a>
</li>
<li role="presentation" class="disabled">
<a href="#step3" data-toggle="tab" aria-controls="step3" role="tab" title="Step 3">
<span class="round-tab">
<i class="icon-check"></i>
</span>
</a>
</li>
</ul>
</div>
<form role="form" action="/career_report" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#if(Sentinel::check())
<input type="hidden" name="user_id" value="{{ Sentinel::check()->id }}">
<input type="hidden" name="username" value="{{ Sentinel::check()->username }}">
#endif
<input type="hidden" name="subject" value="{{url()->current()}}">
<input type="hidden" name="user_id_posted" value="{{ $event->user->id }}">
<input type="hidden" name="username_posted" value="{{ $event->user->username }}">
<input type="hidden" name="career_solution_id" value="{{ $event->id}} ">
<div class="tab-content">
<div class="tab-pane active" role="tabpanel" id="step1">
<h3 style="text-align: center">Why are you reporting this content :</h3>
<ul style="list-style: none">
<li>
<input type="radio" id="box-9" name="why_reporting" value="Spam">
<label for="box-9">Spam</label>
<div class="check"></div>
<small id="box-9-s" style="display: none">I consider this content irrelevant and annoying.</small>
</li>
<li>
<input type="radio" id="box-10" name="why_reporting" value="Fake Profile">
<label for="box-10">Fake Profile</label>
<div class="check"><div class="inside"></div></div>
<small id="box-10-s" style="display: none">This content was posted by way of a profile that clearly doesn't represent a real person.
</small>
</li>
<li>
<input type="radio" id="box-11" name="why_reporting" value="Advertising">
<label for="box-11">Advertising</label>
<div class="check"><div class="inside"></div></div>
<small id="box-11-s" style="display: none">This content is or contains advertising.
</small>
</li>
<li>
<input type="radio" id="box-12" name="why_reporting" value="Untrustworthy source">
<label for="box-12">Untrustworthy source</label>
<div class="check"><div class="inside"></div></div>
<small id="box-12-s" style="display: none">This content is from an untrustworthy source and contains unverifiable statements.
</small>
</li>
<li>
<input type="radio" id="box-13" name="why_reporting" value="Defamatory">
<label for="box-13">Defamatory</label>
<div class="check"><div class="inside"></div></div>
<small id="box-13-s" style="display: none">The reported content is insulting or defamatory to me or other people.
</small>
</li>
<li>
<input type="radio" id="box-14" name="why_reporting" value="Violence or pornography">
<label for="box-14">Violence or pornography</label>
<div class="check"><div class="inside"></div></div>
<small id="box-14-s" style="display: none">This content contains violence or pornography.
</small>
</li>
<li>
<input type="radio" id="box-15" name="why_reporting" value="Violates IP rights">
<label for="box-15">Violates IP rights</label>
<div class="check"><div class="inside"></div></div>
<small id="box-15-s" style="display: none">This content includes third-party content (e.g. an image) posted under their own name.
</small>
</li>
<li>
<input type="radio" id="box-16" name="why_reporting" value="Promotes structural distribution measures">
<label for="box-16">Promotes structural distribution measures</label>
<div class="check"><div class="inside"></div></div>
<small id="box-16-s" style="display: none">This content promotes a chain distribution system, multilevel selling or pyramid sales.
</small>
</li>
<li >
<input type="radio" id="box-17" name="why_reporting" value="Other">
<label for="box-17" id="other">Other</label>
<div class="check"><div class="inside"></div></div>
<div class="form-group" id="mydiv" style="display: none">
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Please let us know why you're reporting this content:
" style="resize: none" name="why_reporting_message"></textarea>
</div>
</li>
</ul>
<ul class="list-inline pull-right">
<li><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></li>
<li><button style="background-color: #18ba9b;border-color: white" type="button" class="btn btn-primary next-step">Save and continue</button></li>
</ul>
</div>
<div class="tab-pane" role="tabpanel" id="step2">
<h3>Why would you like to report this?</h3>
<div class="form-group">
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" style="resize: none" placeholder="
Please let us know why you're reporting this content:" name="additional_message"></textarea>
</div>
<ul class="list-inline pull-right">
<li><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></li>
<li><button type="button" class="btn btn-default prev-step">Previous</button></li>
<li><button style="background-color: #18ba9b;border-color: white" type="submit" class="btn btn-primary btn-info-full next-step" id="save">Submit your report</button></li>
</ul>
</div>
<div class="tab-pane" role="tabpanel" id="step3">
<h2 class="title-box-v2" style="font-size: 17px !important;line-height: 35px">We'll look into this as soon as possible. Thanks for helping us improve the quality of content on Workstickers.</h2>
<p style="text-align: center">You have successfully completed all steps.Flagged content and users are reviewed by Workstickers staff 24 hours a day, seven days a week to determine whether they violate Community Guidelines. Accounts are penalized for Community Guidelines violations, and serious or repeated violations can lead to account termination.
</p>
<div style="text-align: center;margin-top: 50px">If you've changed your mind - Cancel report</div>
</div>
<div class="clearfix"></div>
</div>
</form>
</div>
</section>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- END MY MODAL -->
<script>
$(document).on("click",".save",function(){
e.preventDefault(); //Prevent page from submiting
$(document).on("click", ".save", function() {
$.ajax({
type: "post",
url: 'career_report',
data: $(".why_reporting").serialize(),
success: function(store) {
},
error: function() {
}
});
});
});
</script>
My route
Route::post('career_report', 'CareerSolutionController#careerReport');
My controller
public function careerReport(requ $request)
{
$report = \App\Reports::create([
'user_id' => $request['user_id'],
'username' => $request['username'],
'user_id_posted' => $request['user_id_posted'],
'username_posted' => $request['username_posted'],
'career_solution_id' =>$request['career_solution_id'],
'subject' =>$request['subject'],
'why_reporting' =>$request['why_reporting'],
'why_reporting_message' =>$request['why_reporting_message'],
'additional_message' =>$request['additional_message'],
]);
if($report != ""){
flash('Career solution report submited', 'success');
}else{
flash('Career solution report', 'warning');
}
return Redirect::back();
}
What I have tried:
I tried to adapt this to my code, but without success. Trying to submit my laravel form without the page refreshing
you can use Jquery Ajax .
$('#yourFormId').submit(function(e){
e.preventDefault();
var data = $(this).serialize();
$.ajax({
method:'POST',
url:'your process file URL',
data:data,
success: function (result) {
// do something with result
}
});
});
in this example i have used $.Ajax method
when you use e.preventDefault(); your form will not be submitted . so you can manually submit it in Javascript . in the submit event , you use Ajax and it sends your form data to your process file and then returns the result . without refreshing page or redirecting .

How to properly structure this multi step form?

I have a page create_conference.blade.php to create new confereces. In this page there is a multi step form:
in the 1st step asks the user for general conference information
in the 2nd asks for a description for the conference creator
in the 3rd aks the user for the registration types for the conference
The way the form should work is: the user enter the information for the first step then click “go to step 2”, then in step 2, the user enters that step 2 specific information and click “go to step 3”. Finally, in step 3, the user enter that step 3 specific information and also clicks in “Store” to submit the information and so the conference is created.
Do you know how to properly handle this multi step form logic with laravel? Im not having success in structuring this.
HTML:
<!-- So, the page has 3 step: General Info, Conference Creator Info and Registration Types) -->
#extends('app')
#section('content')
<div class="container nopadding py-4">
<h1 class="h5 text-center">Create Conference</h1>
<div class="row">
<div class="col-12">
<ul class="nav nav-pills registration_form_list" role="tablist">
<li class="">
<a class="nav-link active" href="#step1" data-toggle="tab" role="tab">
Step1<br><small>General Information</small></a>
</li>
<li class="disabled">
<a class="nav-link" href="#step2" data-toggle="tab" role="tab">
Step 2<br><small>Conference Creator Info</small></a>
</li>
<li class="disabled">
<a class="nav-link" href="#step3" data-toggle="tab" role="tab">
Step 3<br><small>Registration Types</small></a>
</li>
</ul>
<!-- STEP 1 - General Confenrece Information-->
<form method="post" class="clearfix" action="conference/store">
{{csrc_field()}}
<div class="tab-content registration_body bg-white" id="myTabContent">
<div class="tab-pane fade show active clearfix" id="step1" role="tabpanel" aria-labelledby="home-tab">
<form method="post" class="clearfix">
<div class="form-group">
<label for="conference_name" class="text-heading h6 font-weight-semi-bold">Conference Name </label>
<input type="text" name="conference_name" class="form-control" id="conference_name">
</div>
<div class="form-row">
<div class="form-group col-lg-6">
<label for="conference_categories" class="text-heading h6 font-weight-semi-bold">Conference Categories</label>
<select id="tag_list" name="conference_categories" multiple class="form-control" id="conference_categories">
<option>IT</option>
</select>
</div>
</div>
<div class="form-group">
<label for="address" class="text-heading h6 font-weight-semi-bold">Address</label>
<input type="text" name="conference_address" class="form-control" >
</div>
<div>
<button type="button" href="#step2" data-toggle="tab" role="tab"
class="btn mr-2 btn-primary btn next-step">
Go To Step 2
</button>
</div>
</form>
</div>
<!-- STEP 2 - Conference creator Information-->
<div class="tab-pane fade clearfix" id="step2" role="tabpanel" aria-labelledby="profile-tab">
<form method="post" class="clearfix">
<div class="form-row">
<div class="form-group">
<label for="conference_creator_description" class="text-heading h6 font-weight-semi-bold">Description</label>
<textarea name="conference_creator_description" id="conference_creator_description" class="form-control" rows="3"></textarea>
</div>
<button type="button" href="#step1" data-toggle="tab" role="tab"
class="btn mr-2 btn-primary btn next-step">
Go Back To Step 1
</button>
<button type="button" href="#step3" data-toggle="tab" role="tab"
class="btn mr-2 btn-primary btn next-step">
Go To Step 3
</button>
</form>
</div>
<!-- STEP 3 - Registration Types-->
<div class="tab-pane clearfix fade" id="step3" role="tabpanel" aria-labelledby="contact-tab">
<form method="post" class="clearfix">
<div class="form-group">
<label for="registration_type_name" class="text-heading h6 font-weight-semi-bold">Registration Type Name</label>
<input type="text" name="registration_type_name" class="form-control" id="registration_type_name">
</div>
<div class="form-group col-md-6">
<label for="registration_type_capacity" class="text-heading h6 font-weight-semi-bold">Capacity</label>
<input type="text" class="form-control" name="registration_type_name" id="registration_type_capacity">
</div>
<div class="form-group">
<button type="button" href="#step2" data-toggle="tab" role="tab"
class="btn mr-2 btn-primary btn next-step">
Go Back To Step 2
</button>
<button type="submit" data-toggle="tab" role="tab"
class="btn mr-2 btn-primary btn next-step">
Store
</button>
</div>
</form>
</div>
</div>
</form>
</div>
</div>
</div>
Laravel:
I created a ConferenceController
Then add to the form an action (action="conference/store")
Then I also created a route:
Route::post(‘/createConference, [
‘uses’ => ‘ConferenceController#store’
‘as’ => conference.store’
]
But when I enter some info and submit the form it appears:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message.
Wrong action url
Route::post(‘/createConference,[‘uses’=>‘ConferenceController#store’,‘as’=>conference.store’]);
Set form's action to route's name and add crsf field
<form action="{{route('conference.store')}}" method="post">
{{ csrf_field() }}

Unable to post value from bootstrap-wysiwyg text editor in codeigniter

I am using bootstrap-wysiwyg text editor in my form, and I am not able to post value in controller
View :
<form id="demo-form2" action="<?php echo base_url();?>admin/post/add" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Post Details<span style="color:red"> <?php echo form_error('details'); ?></span></label>
<div class="col-md-8 col-sm-8 col-xs-12">
<div id="alerts"></div>
<div class="btn-toolbar editor" data-role="editor-toolbar" data-target="#editor">
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" title="Font"><i class="fa fa-font"></i><b class="caret"></b></a>
<ul class="dropdown-menu">
</ul>
</div>
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" title="Font Size"><i class="fa fa-text-height"></i> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>
<a data-edit="fontSize 5">
<p style="font-size:17px">Huge</p>
</a>
</li>
<li>
<a data-edit="fontSize 3">
<p style="font-size:14px">Normal</p>
</a>
</li>
<li>
<a data-edit="fontSize 1">
<p style="font-size:11px">Small</p>
</a>
</li>
</ul>
</div>
</div>
<div id="editor" class="editor-wrapper"></div>
<textarea id="descr" name="descr" style="display:none;"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-sm-8 col-xs-12 col-md-offset-3"> <button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
And my controller
print_r($_POST); exit;
On searching, I have founded answers like
Solution for this issue
But I don't know how to implement this in my condition. I am getting all the other values expect value in text-area.
Thanks in advance
This will get the editor contents and replace the value of editor_contents on the submit.
<form name="your_form" method="post" onSubmit="document.your_form.editor_contents.value = $('#editor').html()">
<textarea name="editor_contents" style="display:none;"></textarea>
<input type="submit" name="submit">
</form>
And you can access it on PHP like $_POST['editor_contents']
This like passing your data on hidden fields.
Try this
<script type="text/javascript">
$(document).on('submit','#demo-form2',function(){
var editor = CKEDITOR.instances['descr'];
document.getElementById('descr').value = editor.getData();
})
</script>

Categories