Comment overlaps to one another - php

I am creating a comment system in PHP (laravel5.5), and jQuery, but when I comment using ajax and then showing the just commented string it overlaps to the previous commented div. How can I show the new comment just after the previous comment?.
my code are as follow:
<div class="container">
<div class="col-md-12">
<div class="panel panel-default widget">
<div class="panel-heading" style="background: #D8E4FC">
<i class="fa fa-comments-o" aria-hidden="true" style="font-size: large"></i>
<h3 class="panel-title">Recent Comments </h3>
<span class="label label-info">{{ $total_comments }}</span>
</div>
<div class="comment-box" id="comment_box">
<?php
foreach ($comments as $comment):
if($comment->video_id == $content_record->code && $comment->comment_type == 1){
?>
<div class="comment" id="comment<?php echo $comment['user_id']; ?>">
<div class="author-thumbnail">
<img src="{{ getProfilePath($comment['image'], 'thumb') }}" alt="<?php echo $comment['video_id']; ?>">
</div>
<div class="comment-text"><strong>
<?php echo $comment['user_id']; ?></strong> - posted
{{ $comment['created_at']->diffForHumans()}}
</div>
<div class="text">
{{ $comment['comment'] }}
</div>
</div>
<?php
foreach ($replys as $reply):
if($reply->video_id == $content_record->code && $reply->replay_for == $comment->id){
?>
<div class="comment coment-replay">
<div class="author-thumbnail"><img src="{{ getProfilePath($reply['image'], 'thumb') }}" alt=""></div>
<div class="comment-text"><strong><?php echo $reply['user_id']; ?></strong> - posted
{{ $reply['created_at']->diffForHumans()}}
</div>
<div class="text">
{{ $reply['comment'] }}
</div>
</div>
<?php
}
endforeach;
?>
<?php
// if(($replys_method =='both' || $replys_method =='ovoo')) :
?>
<div class="comment coment-replay">
<form>
<textarea name="reply" id="reply_{{$comment['id']}}" class="form-control" rows="2" placeholder="Repay" required></textarea>
<input type="hidden" name="video_id1" value="{{$content_record->code}}">
<input type="hidden" id="reply_for_{{$comment['id']}}" name="url1" value="{{$comment['id']}}">
<input type="hidden" name="comment_type1" id="comment_type1" value="2">
<input type="hidden" name="publication1" id="publication1" value="1">
<div>
<?php
$user = auth()->user();
if(!empty($user->id))
{
?>
<button type="submit" value="submit" class="btn btn-success btn-sm pull-right reply ">
<span class="btn-label">
<i class="fi ion-ios-undo-outline"></i></span>Replay </button>
<?php }else{ ?>
<a class="btn btn-success" href="{{URL_USERS_LOGIN}}" style=" margin: 10px 13px;float: right; padding: 5px 7px 5px 10px;"> <span class="btn-label"><i class="fi ion-log-in"></i></span>Login to Replay </a>
<?php } ?>
</div>
</form>
</div>
<script type="text/javascript">
$(".reply").click(function(e){
e.preventDefault();
var namer = '{{$content_record->code}}';
var passwordr = $("#reply_for_{{$comment['id']}}").val();
var comntr = $("#reply_{{$comment['id']}}").val();
var comnttyper = $("#comment_type1").val();
var publicationr = $("#publication1").val();
var tokenr = '{{ csrf_token() }}';
$.ajax({
type:'POST',
url: '{{ URL_COMMENTS_ADD }}',
data:{video_id:namer, url:passwordr, comment:comntr, _token:tokenr, publication:publicationr, comment_type:comnttyper},
success:function(data){
alert('Your comment has been post wait for approval');
console.log(data);
$("#reply").val("");
},
error: function (error) {
alert('Please Login First Before Comment.');
}
});
});
</script>
<?php
}
// endif;
endforeach;
if($total_comments == 0){
echo "<style> #comment-container{margin-top:0px !important} </style>";
}else{
echo "<style> #comment-container{margin-top:50px !important} </style>";
}
?>
</div>
<div id="comment-container">
<div class="movie-heading overflow-hidden"> <span class="wow fadeInUp" data-wow-duration="0.8s">Leave a comment</span>
<div class="disable-bottom-line wow zoomIn" data-wow-duration="0.8s"></div>
</div>
<!-- {!! Form::open(array('url' => URL_COMMENTS_ADD, 'method' => 'POST', 'files' => true, 'name'=>'formComments ', 'novalidate'=>'')) !!} -->
<form>
<input type="hidden" name="video_id" value="{{$content_record->code}}">
<input type="hidden" name="url" value="{{ $video_src }}">
<input type="hidden" name="comment_type" id="comment_type" value="1">
<input type="hidden" name="publication" id="publication" value="0">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="comment" id="cmnt-user-msg" rows="4" class="form-control" placeholder="Write comment here" required></textarea>
<div class="input-top-line"></div>
<div class="input-bottom-line"></div>
</div>
</div>
<div class="col-sm-12">
<?php
$user = auth()->user();
if(!empty($user->id))
{
?>
<button type="submit" value="submit" class="btn btn-success" id="commen"> <span class="btn-label"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> </span> Post Comments </button>
<?php
}
else
{
?>
<a class="btn btn-success" href="{{URL_USERS_LOGIN}}"> <span class="btn-label"><i class="fi ion-log-in"></i></span>Login to Comments </a>
<?php
}
?>
</div>
</div>
</form>
<!-- {!! Form::close() !!} -->
</div>
</div>
</div>
</div>
my jquery code is as following
$("#commen").click(function(e){
e.preventDefault();
var name = '{{$content_record->code}}';
var password = $("input[name=url]").val();
var comnt = $("#cmnt-user-msg").val();
var comnttype = $("#comment_type").val();
var publication = $("#publication").val();
var token = '{{ csrf_token() }}';
$.ajax({
type:'POST',
url: '{{ URL_COMMENTS_ADD }}',
data:{video_id:name, url:password, comment:comnt, _token:token, publication:publication, comment_type:comnttype},
success:function(data){
console.log(data);
$('#comment_box').html('<div class="comment" id="comment"'+data.id+'"><div class="author-thumbnail"><img src="{{ getProfilePath("'+data.user_image+'", "thumb") }}" alt="'+data.video_id+'"></div><div class="comment-text"><strong>'+data.user_name+'</strong> - posted just now </div> <div class="text">'+data.comment+'</div> </div>');
alert('Your comment has been post wait for approval');
$("#cmnt-user-msg").val("");
},
error: function (error) {
alert('Comments after some time later.');
}
});
But it shows this instead in the image shared right here below
only the single comment shows and overlap the previous one, I want to add it after previous commented comment if comment

You are using the html() method which only returns the content of an element, instead use the append() method which adds the content of an element to the end of a parent element.
Use append like this:
$('#comment_box').append('<div class="comment"
id="comment"'+data.id+'"><div class="author-thumbnail"><img src="{{ getProfilePath("'+data.user_image+'", "thumb") }}" alt="'+data.video_id+'"></div><div class="comment-text"><strong>'+data.user_name+'</strong> - posted just now </div> <div class="text">'+data.comment+'</div> </div>');
This will add the div class of "comment" at the end of the parent id "comment_box"

Related

How to post AJAX response to all logged in users as an update

I currently have this code that post for only one user and i want to know if it's possible, that the ajax response can be posted as an update to all logged in users as an update on the website. I appreciate all efforts.
<form method="post" action="" id="postForm">
<input type="hidden" name="userId" id="userId" value="<?php echo $_SESSION['USERID']; ?>" />
<select name="type" class="form-control" style="border:1px solid #eee; border-radius:5px; width:auto !important;">
<option value="">select type of post</option>
<option value="default">Normal Post</option>
<option value="Testimonies">Testimonies</option>
<option value="Prayer">Prayer Request</option>
<option value="counselling">Counselling</option>
<option value="Dreams">Dreams Interpretation</option>
</select>
<textarea class="form-control" style="border:none; resize:none;" rows="1" name="post" id="post" placeholder="What's God saying through you today? <?php $text = explode(' ', $_SESSION['NAME']); echo $text[0]; ?>"></textarea>
<button type="submit" value="postForm" class="submitIt btn btn-info btn-flat">Post</button>
</form>
The AJAX Part
<script>
$(document).ready(function(){
$( "body" ).delegate( ".submitIt", "click", function(e) {
e.preventDefault();
e.stopPropagation();
//the button value
$url = "http://localhost/christ/ajaxPages/";
var buttonValue = $(this).val();
//alert(buttonValue);
if(buttonValue == "postForm") {
var formdata = $('#postForm').serialize();
//alert(formdata);
$.ajax({
type: "POST",
url: $url + "addit.php",
data: formdata,
success: function(data){
$("#discuss").append(data);
$("#post").each(function(){autosize.destroy(this);});
$("#post").val("");
}
})
});
</script>
This is the addit.php file called by above ajax
<?php
include '.././config.php';
//waste in dispose
$arr[] =0;
$db_con = connect();
$sect = $_POST['post'];
$type = $_POST['type'];
$userId = $_POST['userId'];
$date = date('Y-m-d H:i:s');
//time of psoidting
$time = strtotime($date);
$sql = "insert into post(post_body,userId,post_type,date) values(:data,:userId,:type,:dateposted)";
$bio = $db_con->prepare($sql);
$bio->bindParam('data', $sect);
$bio->bindParam('type', $type);
$bio->bindParam('userId', $userId);
$bio->bindParam('dateposted', $date);
$bio->execute();
$arr = $bio->errorInfo();
if($arr[2] == "") {
$disc = $db_con->prepare("select * from post where post_body = :text and userId = :userId");
$disc->bindParam("text", $sect);
$disc->bindParam('userId', $userId);
$disc->execute();
$row = $disc->fetch();
$_SESSION['postId'] = $row['post_id'];
//let us retrive
echo '<!-- Box Comment -->
<div class="box box-widget" id="lofty-'.$row['post_id'].'">
<div class="box-header with-border" style="border-radius:5px 5px 0 0;">
<div class="user-block">
<img class="img-circle" src="'.$url.'/'.userName('icon', $_SESSION['USERID']).'" alt="User Image">
<span class="username"><a style="color:#1da1f2;" href="#">'.ucfirst($_SESSION['NAME']).'</a></span>
<span class="description">Shared - ';
echo humanTiming($time);
echo '</span>
</div>
<!-- /.user-block -->
<div class="box-tools">';
if($row['userId'] == $_SESSION['USERID']) {
echo '<div class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span style="color:#fff">Dropdown Example</span>
<span class="caret"></span></a>
<ul class="dropdown-menu shadow">
<li><a href="#"><button type="button" data-toggle="modal" data-target="#editModal" class="btn btn-box-tool edit" id="edit-post-'.$row['post_id'].'"><i class="fa fa-edit"></i> Edit</button></li>
<li><button type="button" class="btn btn-box-tool submitIt" value="delete" name="del-'.$row['post_id'].'-'.$_SESSION['USERID'].'-post"><i class="fa fa-trash-o"></i> Delete</button></li>
</ul>
</div>';
}
echo '</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body">
<p style="white-space:pre-wrap";><span id="replace-'.$row['post_id'].'">'.$sect.'</span></p>
<div style="margin-left:-10px; margin-right:-10px; padding:4px 4px 0 4px;">
<div class="col-lg-6" style="text-align:left">
<span class="text-muted">
<button style="background:none; border:none;" class="btn btn-box-tool BigLike" name="like-'.$row['post_id'].'-'.$_SESSION['USERID'].'">
<i class="fa fa-thumbs-o-up"></i> <span id="change'.$row['post_id'].'"></span></button>
<span id="seeIt'.$row['post_id'].'"></span><span id="seeItbefore'.$row['post_id'].'"></span></span>
</div>
<div class="col-lg-6" style="text-align:right">
<span class="text-muted">
<span id="seeIt2'.$row['post_id'].'"></span><span id="seeItbefore2'.$row['post_id'].'"></span>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer box-comments" style="background:none; margin:0;" id="comment'.$row['post_id'].'"></div>
<!-- /.box-footer -->
<div class="box-footer" style="border:none; padding-top:1px;">
<form action="." method="post">
<img class="img-responsive img-circle img-sm" src="'.$url.'/'.userName('icon', $_SESSION['USERID']).'" alt="user image">
<!-- .img-push is used to add margin to elements next to floating images -->
<div class="img-push">
<textarea type="text" style="border-radius:20px; background:#eee; border:none; resize:none; overflow:hidden" rows="1" class="form-control input-sm comment-'.$_SESSION['USERID'].'" name="comment-'.'3-'.$row['post_id'].'-'.$_SESSION['USERID'].'" placeholder="Press enter to post comment"></textarea>
</div>
</form>
</div>
<!-- /.box-footer -->
</div>
<!-- /.box-widget -->
<!-- /.box -->';
} else {
echo 'Failed';
}
?>
I want to know if it's possible to modify this code to post the ajax response to all logged in users on the website rather than just the user that made the post. Forgive my English

Fetch data from mysql by checking radio button

i have few models of products and when i click on each model to show me his variants, like on image from below
Here is what i made in html :
<div class="owl-carousel owl-theme no-pb slide-arrow-2" data-
dots="false" data-nav="true" data-items="4" data-lg-items="3"
data-md-items="2" data-sm-items="2" data-margin="30" data-
autoplay="false">
<?php
$qua = "SELECT * FROM produse WHERE categorie = '$id' ORDER BY pozitie";
$qla = mysqli_query($conn, $qua);
while($rova = mysqli_fetch_assoc($qla)){?>
<?php
$id_paletar = $rova['id'];
$sql2=mysqli_query($conn,"SELECT * FROM pret_produse WHERE produs_id='$id_paletar'");
$pret2=mysqli_fetch_array($sql2);
?>
<div class="item">
<div class="product-item">
<div class="product-img">
<img class="img-fluid" src="cms/produse/<?=$rova['poza'];?>" alt="">
<div class="product-overlay">
<ul class="list-unstyled">
</ul>
</div>
</div>
<div class="product-btn ifix">
<div class="btn btn-theme btn-block btn-new" ><span style="font-size: 15px;"><?=$rova['titlu'];?></span>
<div>
<label class="containere">
<input type="radio" name="produse" id="paletar_pret" class="palt" value="<?=$rova['id'];?>">
<p class="checkmark"></span>
</label>
</div>
</div>
</div>
</div>
</div>
<?php }?>
</div>
Here is my jquery:
$(document).ready(function(){
$('.palt').change(function() {
if(this.checked) {
var check= $(this).val();
alert(check);
$.ajax({
url: 'cere_cotatie.php',
type: 'POST',
data: {check:check},
success: function(data){
$('.fetch').append(data);
}
});
}
});
And here is my php file which i use on ajax to call:
<?php
if($_POST)
{
include('config.php');
echo $check = $_POST['check'];
$s = "SELECT * FROM variante WHERE id_var='$check'";
$q = mysqli_query($conn, $s);
while ($r = mysqli_fetch_assoc($q)) {?>
<div class="item" style="width:220px;">
<div class="product-item">
<div class="product-img">
<img class="img-fluid" src="cms/variante/<?=$r['poza'];?>" alt="">
<div class="product-overlay">
<ul class="list-unstyled">
</ul>
</div>
</div>
<div class="product-btn ifix">
<div class="btn btn-theme btn-block btn-new" ><span style="font-size: 15px;"> <?=$r['denumire'];?></span>
<div>
<label class="containere">
<input type="checkbox">
<p class="checkmark"></span>
</label>
</div>
</div>
</div>
</div>
</div>
<?php }?>
<?php }
?>
My problem is like this if i check first box to display me variants of what i checked but my code is doing this way, if i check first box is loading data and after that if i check another box results from first box need to dissapear and to be only what i check.
EDIT: in that print screen you can see the problem.
I think you should add below code
$('.palt').change(function(e) {
e.preventDefault();
});
and remove alert(check);
Hope it work for you.

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!

undefined variable in modal

if i click on view button in my table it should open an modal form and
display all table values to view it.But i'm getting error as undefined
variable in the textbox inside of location,where i called only location
in the table.kindly help out me with how to get datas from dbs as php
code.thanks in advance.
UPDATE: I had updated my code.Kindly check it out,as i called ajax ,but modal box open with empty ,no informations loaded.kindly help it out.
<!-- Main content -->
<div class="main-content">
<h1 class="page-title">OUR POP DETAILS</h1>
<!-- Breadcrumb -->
<ol class="breadcrumb breadcrumb-2">
<li><i class="fa fa-home"></i>Home</li>
<li>Metro Pop</li>
<li class="active"><strong>Action</strong></li>
</ol>
<div class="row">
<div class="col-md-12"></div>
<div class="col-md-4">
<?php $apage = array('id'=>'','name'=>'');?>
<script>var page_0 = <?php echo json_encode($apage)?></script>
<h3><a data="page_0" class="model_form btn btn-sm btn-danger" href="#"><span class="glyphicon glyphicon-plus"></span> Add new record</a></h3>
</div>
</div>
<div id="table-container">
<div class="row">
<div class="col-md-12">
<table id="table" class="table table-striped table-sortable table-condensed " cellspacing="0" width="100%"
data-show-columns="true"
>
<tbody>
<?php if(isset($result) && ($data_record) > 0) : $i=1; ?>
<?php while ($users = mysqli_fetch_object($result)) { ?>
<tr class="<?=$users->id?>_del">
<td><?=$i;?></td>
<td><?=$users->zonee;?></td>
<td><?=$users->location;?></td>
<td><?=$users->pop_type;?></td>
<td><?=$users->switch_name;?></td>
<td><?=$users->switch_ip;?></td>
<td><?=$users->switch_make;?></td>
<td><?=$users->switch_serial;?></td>
<td><?=$users->switch_model;?></td>
<td> <i class="material-icons"></i></td>
<script>var page_<?php echo $users->id ?> = <?php echo json_encode($users);?></script>
<td><a data="<?php echo 'page_'.$users->id ?>" class="model_form btn btn-info btn-sm" href="#"> <span class="glyphicon glyphicon-pencil"></span></a>
<a data="<?php echo $users->id ?>" title="Delete <?php echo $users->name;?>" class="tip delete_check btn btn-info btn-sm "><span class="glyphicon glyphicon-remove"></span> </a>
<button data-toggle="modal" data-target="#view-modal" data-id="<?php echo $users->id; ?>" class=" view_check btn btn-sm btn-info"><i class="glyphicon glyphicon-eye-open"></i></button>
</td>
</tr>
<?php $i++;
} ?>
<?php else : echo '<tr><td colspan="8"><div align="center">-------No record found -----</div></td></tr>'; ?>
<?php endif; ?>
</tbody>
</table>
<?php
if(isset($_SESSION['flash_msg'])) :
$message = $_SESSION['flash_msg'];
echo $error= '<div class="alert alert-success" role="alert">
<span class="glyphicon glyphicon-envelope"></span> <strong>'.$message.'</strong> </div>';
unset($_SESSION['flash_msg']);
endif;
?>
</div>
</div>
</div>
<div id="view-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">
<i class="glyphicon glyphicon-user"></i> POP Information
</h4>
</div>
<div class="modal-body" id="employee_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Form modal -->
<div id="form_modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><i class="icon-paragraph-justify2"></i><span id="pop_title">ADD</span> POP INFORMATION</h4>
</div>
<!-- Form inside modal -->
<form method="post" action="add_edit.php" id="cat_form">
<div class="modal-body with-padding">
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>zonee :</label>
<input type="text" name="zonee" id="zonee" class="form-control required">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>location :</label>
<input type="text" name="location" id="location" class="form-control required">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>pop_type :</label>
<input type="text" name="pop_type" id="pop_type" class="form-control required number">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_name:</label>
<input type="text" name="switch_name" id="switch_name" class="form-control required number">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_ip :</label>
<input type="text" name="switch_ip" id="switch_ip" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_make :</label>
<input type="text" name="switch_make" id="switch_make" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_serial :</label>
<input type="text" name="switch_serial" id="switch_serial" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_model :</label>
<input type="text" name="switch_model" id="switch_model" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>Latitude:</label>
<input type="text" name="latitude" id="latitude" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>Longitude:</label>
<input type="text" name="longitude" id="longitude" class="form-control required" >
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
<span id="add">
<input type="hidden" name="id" value="" id="id">
<button type="submit" name="form_data" class="btn btn-primary">Submit</button>
</span>
</div>
</form>
</div>
</div>
</div>
<!-- /form modal -->
<script type="text/javascript">
$(document).ready(function() {
$(document).on('click','.model_form',function(){
$('#form_modal').modal({
keyboard: false,
show:true,
backdrop:'static'
});
var data = eval($(this).attr('data'));
$('#id').val(data.id);
$('#zonee').val(data.zonee);
$('#location').val(data.location);
$('#pop_type').val(data.pop_type);
$('#switch_name').val(data.switch_name);
$('#switch_ip').val(data.switch_ip);
$('#switch_make').val(data.switch_make);
$('#switch_serial').val(data.switch_serial);
$('#switch_model').val(data.switch_model);
$('#latitude').val(data.latitude);
$('#longitude').val(data.longitude);
if(data.id!="")
$('#pop_title').html('Edit');
else
$('#pop_title').html('Add');
});
$(document).on('click','.delete_check',function(){
if(confirm("Are you sure to delete data")){
var current_element = $(this);
url = "add_edit.php";
$.ajax({
type:"POST",
url: url,
data: {ct_id:$(current_element).attr('data')},
success: function(data) { //location.reload();
$('.'+$(current_element).attr('data')+'_del').animate({ backgroundColor: "#003" }, "slow").animate({ opacity: "hide" }, "slow");
}
});
}
});
$(document).on('click', '.view_check', function(){
//$('#dataModal').modal();
var employee_id = $(this).attr("id");
$.ajax({
url:"view.php",
method:"POST",
data:{employee_id:employee_id},
success:function(data){
$('#employee_detail').html(data);
$('#view-modal').modal('show');
}
});
});
});
});
</script>
**view.php**
<?php
include("config.php");
if(isset($_POST["employee_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "", "mine");
$query = "SELECT * FROM user WHERE id = '".$_POST["employee_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_object($result))
{
$output .= '
<tr>
<td width="30%"><label>Name</label></td>
<td width="70%">'.$users["location"].'</td>
</tr>
<tr>
<td width="30%"><label>Address</label></td>
<td width="70%">'.$users["zonee"].'</td>
</tr>
<tr>
<td width="30%"><label>Gender</label></td>
<td width="70%">'.$users["pop_type"].'</td>
</tr>
';
}
$output .= '</table></div>';
echo $output;
}
?>
Add quota('') like $row['location'] , you are using $row[location]
Or use below code
<div class="col-sm-12">
<label>location :</label>
<input type="text" name="location" id="location" value="<?php
echo $row['location'];?>" />
</div>
Your variable name is $users not $row so you can write this
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>location :</label>
<input type="text" name="location" id="location" value="<?php
echo $users['location'];?>" />
</div>
Try this Code
<tbody>
<?php if(isset($result) && ($data_record) > 0) : $i=1; ?>
<?php while ($users = mysqli_fetch_object($result)) { ?>
<tr class="<?=$users->id?>_del">
<td><?=$i;?></td>
<td><?=$users->zonee;?></td>
<td><?=$users->location;?></td>
<td><?=$users->pop_type;?></td>
<td><?=$users->switch_name;?></td>
<td><?=$users->switch_ip;?></td>
<td><?=$users->switch_make;?></td>
<td><?=$users->switch_serial;?></td>
<td><?=$users->switch_model;?></td>
<td> <a href="http://maps.google.com/?q=<?=$users-
>latitude;?>,<?=$users->longitude;?>" target=\"_blank\"><i
class="material-icons"></i></a></td>
<script>var page_<?php echo $users->id ?> = <?php
echo json_encode($users);?></script>
<td><a data="<?php echo 'page_'.$users->id ?>"
class="model_form btn btn-info btn-sm" href="#"> <span
class="glyphicon glyphicon-pencil"></span></a>
<a data="<?php echo $users->id ?>" title="Delete <?
php echo $users->name;?>" class="tip delete_check btn btn-info
btn-sm "><span
class="glyphicon glyphicon-remove"></span> </a>
<button data-toggle="modal" data-target="#view-
modal" data-id="<?php echo $users->id; ?>" id="getUser"
class="btn btn-sm
btn-info"><i class="glyphicon glyphicon-eye-open"></i>
</button>
</td>
</tr>
<?php $i++;
echo "<div class='modal-body'>
<div id='dynamic-content'>
<div class='form-group'>
<div class='row'>
<div class='col-sm-12'>
<label>location :</label>
<input type='text' name='location' id='location' value='$users->location' />
</div>
</div>
</div>
</div>
</div> "
} ?>
<?php else : echo '<tr><td colspan="8"><div align="center">-------No
record found -----</div></td></tr>'; ?>
<?php endif; ?>
</tbody>
On Click Call Function
<script>
function launch_modal(id)
{
//Store id in variable
var newId = id;
//Ajax Start
$.ajax({
type: "POST",
url: "your_php_page.php",
//send id to php page
data: {theId:newId},
success: function(data){
//to display data in paragraph of Modal
$('.modal-body').html(data);
//to display modal
$('#myModal').modal("show");
},
});
}
</script>
your_php_page.php
<?php
$theId = $_POST['theId'];
if($theId){
$output = '';
$sql = $conn->query("select * from table where id = '$theId'");
$fetch = $sql->fetch_object();
//Append
$output .= '<table class="table table-bordered">
<tr>
<td>Name :</td>
<td>'.$fetch->name.'</td>
</tr>
<tr>
<td>Number :</td>
<td>'.$fetch->number.'</td>
</tr>
';
echo $output;
}
?>

Form with checkboxes pass to ajax

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);

Categories