Form with checkboxes pass to ajax - php

I'm using Laravel 4 on this. I have a form with a list of departments in which there are members per department. I used an accordion to place all members under one checkbox of department. Now when I check the checkbox and submit it I want it to be posted on my ajax.
Here's what I can do but found an error.
Syntax error, unrecognized expression: input[name=dept_id:checked
HTML
<div class="col-md-6 col-sm-6">
<div class="blue-steel box portlet">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-calendar"></i>Bulk select Department off day
</div>
</div><!-- /.portlet-title -->
<div class="portlet-body form">
<div class="form-body">
{{Form::open(['url'=>url('admin/schedule'), 'id' => 'department_bulk_off_day', 'class'=>'form-horizontal', 'method'=>'GET'])}}
<div class="form-group">
<label class="col-md-3 control-label" style="padding-left: 0px;">Select Department</label>
<div class="col-md-9">
<!-- <div id="accordion">
<h3><span id="id"><input type="checkbox"/></span>Text More text </h3>
<div>content etc</div>
</div> -->
<div class="employee_checkbox_wrapper" id="accordion">
<?php $department = DB::table('department')->get(); ?>
#foreach($department as $key => $val)
<label><span id="id"><input type="checkbox" name="dept_id[]" value="{{ $val->id }}"/></span>{{ $val->deptName }} </label>
<div>
<?php
$dept_id = $val->id;
$schedule = '';
$desig_ids = DB::table('designation')->where('deptID', $dept_id)->get();
foreach ($desig_ids as $desig_id) {
$emp_des = $desig_id->id;
$employee_desigs = DB::table('employees')->where('designation', $emp_des)->get();
foreach ($employee_desigs as $employee_desig) {
?>
<label style="margin-left: 15px;"><!-- <input type="checkbox" name="employee_id[]" value="{{ $employee_desig->id }}"/> -->
{{ $employee_desig->fullName }}
</label>
<a data-toggle="modal" data-id="{{ $employee_desig->id }}" data-target="#myModal" title="Add this item" class="open-AddBookDialog btn " href="#addBookDialog">
Change Department
</a>
</br>
<?php
}
}
?>
</div>
#endforeach
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" style="margin-top: 20px;">Select Date</label>
<div class="col-md-3">
<div class="input-group input-medium date date-picker" data-date-format="dd-mm-yyyy" data-date-viewmode="years" style="margin-top: 20px;">
<input type="text" class="form-control" name="off_day" value="{{ date('d-m-Y') }}">
<span class="input-group-btn">
<button class="btn default" type="button"><i class="fa fa-calendar"></i></button>
</span>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-md-12 text-center">
{{Form::token()}}
<input type="hidden" name="department_bulk_off_day" value="2">
<button class="btn btn-button button-tip-form-submit">Update</button>
</div><!-- /.col-md-12 text-center -->
</div>
</form><!-- /.form-horizontal -->
</div><!-- /.form-body -->
{{ Form::close() }}
</div><!-- /.portlet-body form -->
</div><!-- /.blue-steel box portlet -->
</div><!-- /.col-md-6 col-sm-6 -->
And here's my JS
<script>
$('#department_bulk_off_day').on('submit', function(e){
e.preventDefault();
var checkValues = jQuery('input[name=dept_id:checked').map(function()
{
return $(this).val();
}).get();
alert(checkValues);
</script>

you can try the following code
var checkValues = jQuery('input[name="dept_id"]').map(function()
{
var this_var = $(this);
if($(this):checked){
return this_var.val();
}
}).get();

Try This. You have the selectors all messed up.
$('#department_bulk_off_day').on('submit', function(e){
e.preventDefault();
var checkValues = $('input[name="dept_id"]:checked').map(function()
{
return $(this).val();
}).get();
alert(checkValues);

Related

How do I get two modals to work on the same page?

I have a food ordering web app in PHP laravel 5.8. The items modal is working fine with the following code but when I am trying to display the deals modal in the same manner, it does not work at all. I am sharing my blade files. The "show.blade.php" file displays the entire front page where these modals have to be shown to the customer. The "modal.blade.php" file is where the modals are defined and their data is coming from the "show.blade.php" page. The first div in the modals file is the one that I want to work and the second is the one that is currently working fine. You can ignore the last modal div.
show.blade.php
#extends('layouts.front', ['class' => ''])
<?php
use Carbon\Carbon;
use App\User;
$name = new User();
?>
#section('content')
#include('restorants.partials.modals')
<section class="section pt-lg-0" id="restaurant-content" style="padding-top: 0px">
<input type="hidden" id="rid" value="{{ $restorant->id }}" />
<div class="container container-restorant">
<hr>
#if ($deals)
<div class="owl-carousel owl-theme owl-loaded">
<div class="owl-stage-outer">
<div class="owl-stage">
<h1>Deals</h1>
#foreach ($deals as $deal)
<div class="col-xl-3 col-lg-6 col-md-6 col-sm-6">
<div class="owl-item">
<div id="addToCart1">
<div class="strip">
<figure style="border-radius: 3rem">
<a onClick="setCurrentDeal({{ $deal->id }})" href="javascript:void(0)"><img
src="{{ $deal->image_url }}_thumbnail.jpg" loading="lazy"
data-src="{{ config('global.restorant_details_image') }}"
class="img-fluid lazy" alt=""></a>
</figure>
<span class="res_title"><b><a onClick="setCurrentDeal({{ $deal->id }})"
href="javascript:void(0)">{{ $deal->name }}</a></b></span><br />
<span class="res_description">{{ $deal->description }}</span><br />
<div class="d-flex justify-content-between">
<div>
#php
$dealItems = [];
$dealItems = App\DealItem::where('deal_id', $deal->id)->get();
#endphp
#foreach ($dealItems as $it)
<h6><strong>{{ $it->item->name }}</strong></h6>
#endforeach
</div>
<h6 class="text-success"><strong>Price:{{ $deal->price }}</strong></h6>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="owl-dots">
<div class="owl-dot active"><span></span></div>
<div class="owl-dot"><span></span></div>
<div class="owl-dot"><span></span></div>
</div>
</div>
#endforeach
#endif
</div>
#section('js')
<script>
$(function() {
$('.owl-carousel').owlCarousel({
margin: 50,
autoplay: true,
loop: true
});
});
var deals = [];
var currentDeal = null;
var currentDealSelectedPrice = null;
var lastAdded = null;
var CASHIER_CURRENCY = "<?php echo env('CASHIER_CURRENCY', 'usd'); ?>";
var LOCALE = "<?php echo App::getLocale(); ?>";
/*
* Price formater
* #param {Nummber} price
*/
function formatPrice(price) {
var formatter = new Intl.NumberFormat(LOCALE, {
style: 'currency',
currency: CASHIER_CURRENCY,
});
var formated = formatter.format(price);
return formated;
}
function setCurrentDeal(id) {
var deal = deals[id];
currentDeal = deal;
$('#modalDealTitle').text(deal.name);
$('#modalDealName').text(deal.name);
$('#modalDealPrice').html(deal.price);
$('#modalDealID').text(deal.id);
$("#modalDealImg").attr("src", deal.image);
$('#modalDealDescription').html(deal.description);
//Normal
currentDealSelectedPrice = deal.priceNotFormated;
// $('#variants-area').hide();
$('.quantity-area').show();
$('#dealModal').modal('show');
}
<?php
$deals =[];
foreach ($deals as $deal){
$theArray = [
'name' => $deal->name,
'id' => $deal->id,
'priceNotFormated' => $deal->price,
'price' => #money($deal->price, env('CASHIER_CURRENCY', 'usd'), true) . '',
'image' => $deal->logom,
'description' => $deal->description,
];
echo 'deals[' . $deal->id . ']=' . json_encode($theArray) . ';';
}
?>
<script>
$(document).ready(function() {
$("#addToCart1").show();
// $('#exrtas-area').show();
$('.quantity-area').show();
});
</script>
#endsection
modals.blade.php
<div class="modal fade" id="dealModal" z-index="9999" tabindex="-1" role="dialog" aria-labelledby="modal-form" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 id="modalDealTitle" class="modal-title" id="modal-title-new-item"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body p-0">
<div class="card bg-secondary shadow border-0">
<div class="card-body px-lg-5 py-lg-5">
<div class="row">
<div class="col-sm col-md col-lg col-lg text-center">
<img id="modalDealImg" src="" width="295px" height="200px">
</div>
<div class="col-sm col-md col-lg col-lg">
<input id="modalDealID" type="hidden"></input>
<span id="modalDealPrice" class="new-price"></span>
<p id="modalDealDescription"></p>
</div>
<div class="offset-md-6 col-md-6">
#if(!config('app.isqrsaas'))
<div class="quantity-area">
<div class="form-group">
<br />
<label class="form-control-label" for="quantity">{{ __('Quantity') }}</label>
<input type="number" name="quantity" id="quantity" class="form-control form-control-alternative" placeholder="{{ __('1') }}" value="1" required autofocus>
</div>
<div class="quantity-btn float-right">
<div id="addToCart1">
<button class="btn btn-primary" v-on:click='addToCartAct'
<?php
if(auth()->user()){
if(auth()->user()->hasRole('client')) {echo "";} else {echo "disabled";}
}else if(auth()->guest()) {echo "";}
?>
>{{ __('Add To Cart') }}</button>
</div>
</div>
</div>
#endif
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="productModal" z-index="9999" tabindex="-1" role="dialog" aria-labelledby="modal-form" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 id="modalTitle" class="modal-title" id="modal-title-new-item"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body p-0">
<div class="card bg-secondary shadow border-0">
<div class="card-body px-lg-5 py-lg-5">
<div class="row">
<div class="col-sm col-md col-lg col-lg text-center">
<img id="modalImg" src="" width="295px" height="200px">
</div>
<div class="col-sm col-md col-lg col-lg">
<input id="modalID" type="hidden"></input>
<span id="modalPrice" class="new-price"></span>
<p id="modalDescription"></p>
<div id="variants-area">
<label class="form-control-label">{{ __('Select your options') }}</label>
<div id="variants-area-inside">
</div>
</div>
</div>
<div class="col-md-12">
<div id="exrtas-area">
<br />
<label class="form-control-label h4" for="quantity">{{ __('Extras') }}</label>
<div class="row" id="exrtas-area-inside"></div>
</div>
</div>
<div class="offset-md-6 col-md-6">
#if(!config('app.isqrsaas'))
<div class="quantity-area">
<div class="form-group">
<br />
<label class="form-control-label" for="quantity">{{ __('Quantity') }}</label>
<input type="number" name="quantity" id="quantity" class="form-control form-control-alternative" placeholder="{{ __('1') }}" value="1" required autofocus>
</div>
<div class="quantity-btn float-right">
<div id="addToCart1">
<button class="btn btn-primary" v-on:click='addToCartAct'
<?php
if(auth()->user()){
if(auth()->user()->hasRole('client')) {echo "";} else {echo "disabled";}
}else if(auth()->guest()) {echo "";}
?>
>{{ __('Add To Cart') }}</button>
</div>
</div>
</div>
#endif
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

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".

Laravel 6 persistent undefined variable error

I am facing undefined variable error when trying to access a variable in my view. I have gone through several solutions out here but none of them has worked for me. I have thoroughly examined everything that could cause this error in mu code and cannot find anything at all, please assist
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Category;
use App\Depot;
use Illuminate\Support\Facades\DB;
use Gloudemans\Shoppingcart\Facades\Cart;
use Auth;
class CheckoutController extends Controller
{
public function index()
{
$allcategories=Category::get();
$delivery_method = DB::table('deliverymethods')
->get();
$cartItems=Cart::content();
return view('checkout.index',['delivery_method'=>$delivery_method],['allcategories'=>$allcategories]);
}
public function deliverymethod()
{
$cartItems=Cart::content();
$allcategories=Category::get();
return view('delivery.index',['cartItems'=>$cartItems],['allcategories'=>$allcategories]);
}
public function billinginfo()
{
$depots=Depot::get();
$cartItems=Cart::content();
$allcategories=Category::get();
return view('billing.index',compact('cartItems'),['allcategories'=>$allcategories],['depots'=>$depots]);
}
public function shipping(Request $request)
{
$user_id=Auth::user()->id;
$shippingaddress=DB::table('shippingaddresses')
->select('shippingaddresses.*')
->where('user_id',$user_id)
->get();
$cartItems=Cart::content();
$allcategories=Category::get();
return view('shipping.index',['cartItems'=>$cartItems],['allcategories'=>$allcategories],['shippingaddress'=>$shippingaddress]);
}
}
My View
#extends('home.base')
#section('action-content')
<div class="container">
<div class="container" id="cart-window">
<div class="row">
<div class="col-lg-12" id="cart-header">
<h3>Billing & Shipping Details <span class="cart-return pull-right"><i class="ionicons ion-ios-cart"></i> Back to Cart</h3>
</div>
</div>
<div class="row">
<div class="col-lg-9">
#if(Cart::Count()==0)
<div class="row">
<div class="col-lg-12" id="empty-cart">
<p>Your cart is currently empty</p>
</div>
</div>
<div class="row">
<div class="col-lg-12">
Return to Shop
</div>
</div>
#else
<div class="card detail" id="delivery-card">
<div class="card-header">
</div>
<div class="card-body">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-lg-6">
<h5><strong>Existing Addresses</strong></h5>
</div>
<div class="col-lg-6">
<select class="select2 form-control" id="newAddress">
<option selected disabled>Please Select Delivery Address</option>
<option value="0">Add New Address</option>
#foreach($shippingaddress as $depot)
<option value="{{$depot->depot_name}}">{{$depot->depot_name}}</option>
#endforeach
<option>LIGHt</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
#endif
</div>
<div class="col-lg-3">
#if(Cart::Count()>0)
<div class="card detail">
<div class="card-header">
</div>
<div class="card-body">
<p class="card-text" id="cart-summary">
Order Summary
</p>
<hr/>
<div class="row">
<div class="col-sm-6">
{{Cart::Count()}}
</div>
<div class="col-sm-6">
#if(Cart::Count()==1)
Item
#else
Items
#endif
</div>
</div>
<hr/>
<div class="row">
<div class="col-sm-6">
<p>
Subtotal
</p>
<p>
VAT (15%)
</p>
<p>
Total to Pay
</p>
</div>
<div class="col-sm-6" id="cart-totals">
<p>
R{{Cart::subtotal()}}
</p>
<p>
R{{Cart::tax()}}
</p>
<p>
R{{Cart::total()}}
</p>
</div>
</div>
<hr/>
<div class="row">
<div class="col-12 continue-btn">
Continue
</div>
</div>
{{-- <hr>
<div class="row" id="delivery-method">
<select class="form-control select2" id="delivery_method" name="del">
<option value="0" selected disabled>Select Delivery Method</option>
#foreach ($delivery_method as $item)
<option value="{{$item['delivery_method']}}">{{$item['delivery_method']}} R{{$item['price']}}</option>
#endforeach
</select>
</div> --}}
</div>
{{-- <div class="card-footer" id="cart-footer">
<div class="delivery_type">
<strong>Checkout</strong>
</div>
{{-- <div class="delivery_type2">
<strong>Checkout 2</strong>
</div> --}}
{{-- </div> --}}
</div>
<div class="card order-review">
<div class="card-body">
<p class="card-text" id="cart-summary">
Order Review
</p>
<div class="row">
<div class="col-sm-12">
<p><strong>Delivery Method</strong> <span class="change-del pull-right">Change</span></p>
<p>Delivery</p>
</div>
</div>
</div>
</div>
#endif
</div>
</div>
</div>
</div>
<!-- NewAddressModal -->
<!-- Modal -->
<div class="modal fade show" id="newAddressModal" style="display: none;" aria-modal="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Default Modal</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
#push('custom_scripts')
<script type="text/javascript">
$(document).ready(function() {
{{-- $("#newAddress").change(function() {
var curVal = $("#newAddress option:selected").val();
if (curVal.indexOf('#newAddressModal') == 0){
$('#newAddressModal').modal('show');
}
}); --}}
$("#newAddress").on("change", function () {
$modal = $('#newAddressModal');
if($(this).val() === '0'){
$modal.modal('show');
}
});
});
</script>
#endpush
#endsection
Error Image
Error Image
Try grouping Multiple collection into one array, Then send it to the view like this:
$viewData = [
'cartItems' => $cartItems,
'allcategories' => $allcategories,
'shippingaddress' => $shippingaddress
];
return View::make('shipping.index')->with($viewData);

Form is not being posted back

I am currently working on create-post view of dashboard of my blogging site. I have used Aero template.
#extends('Dashboard.Layout.master')
#section('content')
<section class="content blog-page">
<div class="body_scroll">
<div class="block-header">
<div class="row">
<div class="col-lg-7 col-md-6 col-sm-12">
<ul class="breadcrumb">
<li class="breadcrumb-item"><i class="zmdi zmdi-home"></i>Computer Bitches</li>
<li class="breadcrumb-item">Blog</li>
<li class="breadcrumb-item active"> New Post</li>
</ul>
<button class="btn btn-primary btn-icon mobile_menu" type="button"><i class="zmdi zmdi-sort-amount-desc"></i></button>
</div>
<div class="col-lg-5 col-md-6 col-sm-12">
<button class="btn btn-primary btn-icon float-right right_icon_toggle_btn" type="button"><i class="zmdi zmdi-arrow-right"></i></button>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<form method="post" action="{{route('dashboard.store.post')}}">
#method('post')
#csrf
<div class="card">
<div class="body">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Blog title" />
</div>
<div class="form-group">
<select class="mdb-select md-form form-control" multiple searchable="Search here..">
<option>LARAVEL</option>
<option>GIT</option>
<option>AWS</option>
<option>REACT</option>
<option>PHP</option>
<option>JAVASCRIPT</option>
<option>CSS3</option>
<option>HTML5</option>
<option>WEB TECHNOLOGIES</option>
<option>JQUERY</option>
</select>
</div>
<div class="form-group">
<textarea id="editor" name="editor " class="form-control" rows="10" placeholder="Enter your Content">
</textarea>
</div>
</div>
</div>
<input type="submit" class="btn btn-success" value="Save"/>
</form>
</div>
</div>
</div>
</div>
</section>
#endsection
#section('scripts')
ClassicEditor
.create( document.querySelector( '#editor' ) )
.then(editor => {
document.getElementById('editor').innerHTML = editor.getData();
})
.catch( error => {
console.error( error );
} );
#endsection
I have tried all the possible ways, from posting back throuh jquery to removing elements and putting them back but no lock yet. It's been 10 hours still trying but I don't think I will find the solution. Thank you guys in advance for any leads.

Using AJAX for passing form inputs in CodeIgniter

I'm a newbie in CodeIgniter and I'm trying to pass my form inputs from the view to the controller. I was able to pass it, and was able to insert it on the database. However, I want to use Ajax for this so the whole page won't reload when I press the submit button. I have watched and tried this youtube tutorial: https://www.youtube.com/watch?v=IURutxKvukA, but it doesn't work for me. :( Thank you in advance!
This is my controller, Teacher.php
public function insert_quiz($quiztitle, $quizdescription, $value="")
{
$data = $this->input->post();
$numData = count($data);
$response = array();
$stringChoices = "";
if($numData > 1)
{
for ($i=4; $i < $numData; $i++)
{
$num = $i-3;
$offset = "inputChoice$num";
if ($i == 4)
{
$stringChoices = $data[$offset];
}
else if ($i != 4)
{
$stringChoices = $stringChoices . "," . $data[$offset];
}
}
}
$quizitems = array('Question' => $data['inputQuestion'], 'Choices' => $stringChoices, 'Correct' => $data['correctanswer'], 'NumSequence' => $data['questionnumber']);
$quizitemid = $this->Teacher_model->insertQuizItemOnQuizItems($quizitems);
if (count($quizitemid) == 1)
{
$quizdeedid = $this->Teacher_model->getQuizDeedId($quiztitle);
$quizdatasetitem = array('QuizSet' => $data['quizsetnumber'], 'QuizItemID' => $quizitemid, 'QuizDeedID' => $quizdeedid);
$quizdatasetid = $this->Teacher_model->insertQuizOnQuizDataSet($quizdatasetitem);
if(count($quizdatasetid) == 1)
{
$response = array(
'status' => "success"
);
echo json_encode($response);
}
}
}
and this is my quiz form, in quizView.php
<div class="container-fluid container-cards-pf container-pf-nav-pf-vertical
nav-pf-persistent-secondary">
<div class="row row-cards-pf">
<!-- Important: if you need to nest additional .row within a .row.row-cards-pf, do *not* use .row-cards-pf on the nested .row -->
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="card-pf">
<div class="card-pf-heading">
<h1 align="center">
<strong><?php echo str_replace("%20", " ", $quizTitle); ?></strong>
</h1>
<h2 class="card-pf-title" align="center">
<label><?php echo str_replace("%20", " ", $quizDescription); ?></label>
</h2>
</div>
<div class="card-pf-body" id="divquizset" style="height: 50px">
<div class="col-sm-2">
</div>
<div class="col-sm-9" id="divquizset">
<div class="col-sm-3">
<p align="right"> QUIZ SET: </p>
</div>
<div class="col-sm-4">
<input align="center" type="text" id="quizset" class="form-control"></input>
</div>
<button class="btn btn-default col-sm-2" align="left" onclick="saveQuizSet()">Save</button>
</div>
<div class="col-sm-1">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="container">
<div class="col-sm-12">
<div id="status">
</div>
</div>
</div>
</div>
<div class="container-fluid container-cards-pf container-pf-nav-pf-vertical
nav-pf-persistent-secondary" id="list1" style="padding-top: 0px">
<!-- LIST VIEW PART 1 -->
<div id = "div1" style="padding-top: 0px">
<?php echo form_open("Teacher/insert_quiz/$quizTitle/$quizDescription", "id='myForm'", $hidden= array()); ?>
<div>
<div class="list-group list-view-pf list-view-pf-view">
<div class="list-group-item">
<div class="list-group-item-header" id="div2">
<div class="list-view-pf-expand">
<span class="fa fa-angle-right"></span>
</div>
<div class="list-view-pf-checkbox">
<input type="text" name="questionnumber" id="inputquestionnumber1" class="form-control">
<!-- <b id="questionnumber" name="questionlabel" for="inputQuestion">Question 1: </b> -->
<input type="hidden" name="quizsetnumber" id="quizsetnum" class="form-control" value="">
</div>
<div class="list-view-pf-actions">
<?php echo form_submit('submit', 'Save', $attributes=array("class" => "btn btn-default")); ?>
<!-- <button class="btn btn-default" style="width: 100px">Save</button> -->
<button class="btn btn-default" style="width: 100px">Edit</button>
<button class="btn btn-default" style="width: 100px">Delete</button>
</div>
<div class="list-view-pf-main-info">
<div class="list-view-pf-body">
<div class="list-view-pf-description">
<div class="list-group-item-text form-group">
<input type="text" name="inputQuestion" id="input1" class="form-control">
</div>
</div>
</div>
</div>
</div>
<div class="list-group-item-container container-fluid hidden" id="containerDiv">
<div class="close">
<span class="pficon pficon-close"></span>
</div>
<div class="row">
<!-- LIST VIEW SUB VIEW -->
<div class="col-md-12">
<form class="form-horizontal" style="padding-left: 100px">
<div class="form-group">
<div class="col-sm-7" align="center">
<dt> CHOICES </dt>
</div>
<div class="col-sm-5" align="center">
<dt> CORRECT ANSWER </dt>
</div>
<div class="col-sm-7">
<div id="divCorrect" style="padding-top: 10px">
<input type="text" name="correctanswer" id="inputcorrectanswer1" class="form-control">
</div>
</div>
<div class="col-sm-5" id="divChoiceContainer1">
<div id="divChoice" style="padding-top: 10px">
<input type="text" name="inputChoice1" id="inpChoice1" class="form-control">
</div>
</div>
<div class="col-sm-7" id="otherDivChoiceContainer1">
</div>
<div class="col-sm-7" align="center" style="padding-top: 20px">
<button type="button" class="btn btn-primary btn-lg btn-block col-xs-9" id = "1" align="center" onclick="addChoiceFunction(this.id)" >Add Choices</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?= form_close() ?>
</form>
</div>
</div>
</div>
<div class="col-sm-3 container-fluid container-cards-pf container-pf-nav-pf-vertical nav-pf-persistent-secondary">
<button type="button" class="btn btn-primary btn-lg btn-block col-md-9" align="center" onclick="addQuestionFunction()">Add Question</button>
</div>
These are the scripts I used, they're also in the quizView.php:
<script>
jQuery(document).ready(function($) {
$('#myForm').ajaxForm({
beforeSubmit: function(formData, jqForm, options){
$("div#status").html('');
},
success: function(response){
var result = $.parseJSON(response);
$("div#status").html('<div class="alert alert-' + result.status + '" role="alert"> HI </div>');
}
});
});
</script>
<script>
$(document).ready(function () {
// click the list-view heading then expand a row
$(".list-group-item-header").click(function(event){
if(!$(event.target).is("button, a, input, .fa-ellipsis-v")){
$(this).find(".fa-angle-right").toggleClass("fa-angle-down")
.end().parent().toggleClass("list-view-pf-expand-active")
.find(".list-group-item-container").toggleClass("hidden");
} else {
}
})
// click the close button, hide the expand row and remove the active status
$(".list-group-item-container .close").on("click", function (){
$(this).parent().addClass("hidden")
.parent().removeClass("list-view-pf-expand-active")
.find(".fa-angle-right").removeClass("fa-angle-down");
})
});
</script>
<script>
function addQuestionFunction() {
$num = $('div .list-view-pf-view').length + 1;
$divId = "#" + $num;
$toClone = $('#div1').clone(true).find("input").val("").end();
$($toClone).find("#containerDiv").addClass("hidden").end();
$($toClone).find("#otherDivChoiceContainer1").attr("id", "otherDivChoiceContainer"+ $num);
$($toClone).find("#otherDivChoiceContainer" + $num).find("#divChoice").remove().end();
$($toClone).find("#1").attr("id", $num);
$($toClone).find("#questionnumber").text("Question " + $num + ": ");
if($($toClone).find("span").hasClass("fa-angle-down")) {
$($toClone).find(".fa-angle-right").removeClass("fa-angle-down").end();
}
$('#list1').append($toClone);
}
function addChoiceFunction(clicked_id) {
$num = parseInt(clicked_id);
$divId = "#otherDivChoiceContainer" + $num;
$numChoice = $($divId + " input").length + 2;
$toClone = $('#divChoice').clone(true).find("input").val("").end();
$toClone.find("#inpChoice1").attr("id", "inpChoice" + $numChoice).attr("name", "inputChoice" + $numChoice);
// $toClone.find("#inpChoice" + $num).attr("name", $("#inpChoice"+$num).attr("name").replace(/\\[\d+\\]/,"inputChoice" + $num));
//alert($numChoice);
$($divId).append($($toClone));
}
function saveQuizSet() {
$('#quizsetnum').val($('#quizset').val());
$quizsetval = $('#quizset').val();
$('#divquizset').empty();
$('#divquizset').append(' <p align="center"> QUIZ SET: '+ $quizsetval + '</p>');
// $('#divquizset').append(" <p align="center"> QUIZ SET: A </p>");
}
</script>
And for my script sources I used these:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://malsup.github.io/jquery.form.js"></script>
<script src="http://www.patternfly.org/components/jquery/dist/jquery.min.js"></script>
<script src="http://www.patternfly.org/components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="http://www.patternfly.org/components/patternfly-bootstrap-combobox/js/bootstrap-combobox.js"></script>
<script src="http://www.patternfly.org/components/bootstrap-datepicker/dist/js/bootstrap-datepicker.js"></script>
<script src="http://www.patternfly.org/components/bootstrap-select/dist/js/bootstrap-select.min.js"></script>
<script src="http://www.patternfly.org/components/d3/d3.min.js"></script>
<script src="http://www.patternfly.org/components/c3/c3.min.js"></script>
<script src="http://www.patternfly.org/components/datatables/media/js/jquery.dataTables.js"></script>
<script src="http://www.patternfly.org/components/google-code-prettify/bin/prettify.min.js"></script>
<script src="http://www.patternfly.org/components/clipboard/dist/clipboard.min.js"></script>
<script src="http://www.patternfly.org/components/patternfly/dist/js/patternfly.min.js"></script>
<script src="http://www.patternfly.org/components/jquery-match-height/dist/jquery.matchHeight-min.js"></script>
<script src="http://www.patternfly.org/assets/js/patternfly-site.min.js"></script>
<link rel="canonical" href="http://www.patternfly.org/pattern-library/navigation/vertical-navigation/vertical-navigation.html">
This is what my form looks like
quizviewform
And this is what it's returning
returnpage
It's inserting on my database, but it doesn't return on the page and ajax doesn't work. :( I hope someone can help me here. Thanks!

Categories