I have a PHP file that prints some divs and button elements, each one has the same class and they have jQuery events attached.
<?php $i=1; foreach ($questions as $question): ?>
<div class="question" id="<?php echo $i ?>">
<div class="id">
<h3 class="id-header">Domanda #<?php echo $i ?></h3>
<button id="minacce-button" class="btn btn-danger btn-rounded"> Minacce Associate <i class="fas fa-info-circle"></i></button>
</div>
<div class="testo">
<p><?php echo $question['testo'] ?></p>
</div>
<div class="valutazione">
<div class="btn-group shadow-0" role="group">
<button type="button" class="btn btn-danger compliance" id="<?php echo $question['id']; ?>" value="Non Compliance" data-color="dark">
Non Compliance
</button>
<button type="button" class="btn btn-warning compliance" id="<?php echo $question['id']; ?>" value="Da Verificare" data-color="dark">
Da Verificare
</button>
<button type="button" class="btn btn-success compliance" id="<?php echo $question['id']; ?>" value="Compliance" data-color="dark">
Compliance
</button>
<input type="hidden" name="id_question[]" id="q<?php echo $question['id']; ?>" value="">
<input type="hidden" name="value_question[]" id="v<?php echo $question['id']; ?>" value="1">
</div>
</div>
<div class="container-1">
<div class="spiegazione">
<p class="spiegazione-header">Spiegazione <i class="fas fa-info-circle"></i></p>
<p class="spiegazione-text">
<?php if($question['spiegazione'] != '') echo $question['spiegazione']; else echo('Non ci sono ancora spiegazioni disponibili!') ?>
</p>
</div>
<div class="contromisura">
<p class="contromisura-header">Contromisura <i class="fas fa-check-circle"></i></p>
<p class="contromisura-text">
<?php if($question['contromisura'] != '') echo $question['contromisura']; else echo('Non ci sono ancora contromisure disponibili!') ?>
</p>
</div>
<div class="sanzioni">
<p class="sanzioni-header">Sanzioni <i class="far fa-frown"></i></p>
<p class="sanzioni-text">
<?php
$db = pdo_connect_mysql();
$sanzioni = select("SELECT * FROM sanzioni_domande WHERE id_domanda ".$question['id']." ORDER BY tipo", $db);
if($sanzioni) {
foreach ($sanzioni as $sanzione) {
$output = "- ".$sanzione['nome']." (";
$sanzione['tipo'] == 1 ? $output .= "sanzione penale!" : $output .= "sanzione amministrativa!";
$output .= "): ".$sanzione['descrizione'];
echo $output;
} // fine foreach
} else {
echo('Non ci sono ancora sanzioni disponibili!');
} // fine if
?>
</p>
</div>
</div>
</div>
<hr>
<?php $i++; endforeach; unset($questions); ?>
Here i have another php page that after making an ajax adds more divs with the same class as the other, they all have jQuery event click attached as the previous page but the click does not work
$output = '';
//print_r($questions);
$i=$_POST['last']+1;
//echo $i."\n";
foreach ($questions as $question) {
//echo $i;
$output.= '<div class="question" id="'.$i.'">
<div class="id">
<h3 class="id-header">Domanda #'.$i.'</h3>
<button id="minacce-button" class="btn btn-danger btn-rounded"> Minacce Associate <i class="fas fa-info-circle"></i></button>
</div>
<div class="testo">
<p>'.$question['testo'].'</p>
</div>
<div class="valutazione">
<div class="btn-group shadow-0" role="group">
<button type="button" class="btn btn-danger compliance" id="'.$question['id'].'" value="Non Compliance" data-color="dark">
Non Compliance
</button>
<button type="button" class="btn btn-warning compliance" id="'.$question['id'].'" value="Da Verificare" data-color="dark">
Da Verificare
</button>
<button type="button" class="btn btn-success compliance" id="'.$question['id'].'" value="Compliance" data-color="dark">
Compliance
</button>
<input type="hidden" name="value_question" id="v'.$question['id'].'" value="1">
</div>
</div>
<div class="container-1">
<div class="spiegazione">
<p class="spiegazione-header">Spiegazione <i class="fas fa-info-circle"></i></p>
<p class="spiegazione-text">
'; if($question['spiegazione'] != '') $output .= $question['spiegazione']; else $output .= 'Informazioni su questa domanda non disponibili!';
$output .= '</p>
</div>
<div class="contromisura">
<p class="contromisura-header">Contromisura <i class="fas fa-check-circle"></i></p>
<p class="contromisura-text">';
if($question['contromisura'] != '') $output .= $question['contromisura']; else $output .='Non ci sono ancora contromisure disponibili!';
$output .= '</p>
</div>
</div>
</div>
<hr>';
$i++;
} //unset($questions);
echo $output;
Here it is my jQuery code:
$(".compliance").on("click", function(event) {
var id_domanda = $(this).attr("id");
var valutazione = -1;
//alert("Testo bottone cliccato:-" + $(this).val() + "-");
switch ($(this).val()) {
case 'Non Compliance':
valutazione = 0;
break;
case 'Da Verificare':
valutazione = 1;
break;
case 'Compliance':
valutazione = 2;
break;
default:
valutazione = -1;
}
// Assegnazione valutazione a input valutazione domanda
$("#v"+id_domanda).val(valutazione);
$("#q"+id_domanda).val(id_domanda);
alert("Valutazione assegnata: " + valutazione);
alert("Riepilogo:\nID Domanda: " + $("#q"+id_domanda).val() + "\nValutazione: " + $("#v"+id_domanda).val() );})
$(".compliance").on("click",
Only works on content when the DOM is loaded.
If you load content trough AJAX and want JQuery events to work on then you need to change it to:
$(document).on('click', '.compliance', function(event) {})
That way the click event will work also on ajax loaded content
Related
I have a system that after uploading multiple photos it generates a subtitles field for each photo, I want to update the database of these subtitles. How to do this. My table in database - tb_fotos_portfolio table (id, portfolio_id, legenda, arquivo).
Obs .: It would be only the part of UPDATE the subtitles
<?php
$sql = "SELECT * FROM tb_fotos_portfolio WHERE id_portfolio = $id";
$query = $DB->Select($sql);
if (count($query) == 0) { ?>
<div class="box-body">
<h3 class="text-center text-danger" style="margin: 140px 0;">Imagem não enviada!</h3>
</div>
<?php } else { ?>
<div class="box-body no-padding">
<ul class="users-list clearfix">
<?php foreach($query as $item) { ?>
<li>
<a class="popup-link" href="../../../upload/portfolio/imagem/<?php echo $item['arquivo']; ?>">
<img src="../../../upload/portfolio/miniatura/<?php echo $item['arquivo']; ?>" alt="<?php echo $item['arquivo']; ?>"/>
<span class="users-list-name"><?php echo $item['arquivo']; ?></span>
</a>
<a class="btn btn-label btn-danger btn-xs" href="acoes.php?acao=deleteFotos&id=<?php echo $_GET['id'];?>&arquivo=<?php echo $item['arquivo'];?>"><i class="fal fa-trash-alt"></i>Excluir</a>
<span class="users-list-name"><?php echo $item['legenda']; ?></span>
<form id="" action="acoes.php?acao=legendas_fotos" method="post" enctype="multipart/form-data">
<input type="text" name="legenda[]" id="legenda" class="form-control" placeholder="Legenda" value="<?php echo $item['legenda']; ?>"/>
</li>
<?php } ?>
</ul><!-- /.users-list -->
</div>
<button type="submit" class="btn btn-success btn-label btn-sm"><i class="fa fa-check"></i> Atualizar Legendas</button>
</form>
acao.php
case 'legendas_fotos':
$legenda = $_POST['legenda'];
...
$sql = "UPDATE tb_fotos_portfolio SET legenda= $legenda ";
$vCampos = array('id'=>$id);
$DB->Execute($sql, $vCampos);
// Volta para o form
header("location:form.php?id=" . $id);
exit;
break
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
here my problem is the values that are selected through append are not getting inserted in to the post value..here am pasting my code
<div id="update_new_<?php echo $value->id;?>" class="update_new_<?php echo $value->id;?>">
<div class="form-group mb0" style="margin-bottom: 0px">
<label for="field-1"
class="col-sm-4 control-label">Other Document</label>
<div class="col-sm-5">
<div class="fileinput fileinput-new" data-provides="fileinput">
<?php
if (!empty($value->documents)) {
$uploaded_file = json_decode($value->documents);
}
if (!empty($uploaded_file)):foreach ($uploaded_file as $v_files_image): ?>
<div class="">
<input type="hidden" name="fileName[]"
value="<?php echo $v_files_image ?>">
<span class=" btn btn-default btn-file">
<span class="fileinput-filename"> <?php echo $v_files_image ?></span>
×
</span>
<strong>
<a href="javascript:void(0);" class="RCF"><i
class="fa fa-times"></i> Remove</a></strong>
<p></p>
</div>
<?php endforeach; ?>
<?php else: ?>
<span class="btn btn-default btn-file"><span
class="fileinput-new">Select File</span>
<span class="fileinput-exists"><?= lang('change') ?></span>
<input type="file" name="files[]">
</span>
<span class="fileinput-filename"></span>
<a href="#" class="close fileinput-exists" data-dismiss="fileinput"
style="float: none;">×</a>
<?php endif; ?>
</div>
<div id="msg_pdf" style="color: #e11221"></div>
</div>
<div class="col-sm-3">
<strong><a href="javascript:void(0);" id="update_more" onclick="add_field('<?php echo $value->id;?>');" u_id="<?php echo $value->id;?>" class="addCF update_more_<?php echo $value->id;?>"><i
class="fa fa-plus"></i> Add More
</a></strong>
</div>
</div>
</div>
here is my script please have a look
<script type="text/javascript">
var maxAppends = 0;
function add_field(id)
{
var update_new = $('<div class="form-group" style="margin-bottom: 0px">\n\
<label for="field-1" class="col-sm-4 control-label">Documentsss</label>\n\
<div class="col-sm-5">\n\
<div class="fileinput fileinput-new" data-provides="fileinput">\n\
<span class="btn btn-default btn-file"><span class="fileinput-new">Select file</span><span class="fileinput-exists" >Change</span><input type="file" name="files[]" ></span> <span class="fileinput-filename"></span>×</div></div>\n\<div class="col-sm-2">\n\<strong>\n\<i class="fa fa-times"></i> Remove</strong></div>');
maxAppends++;
$(".update_new_"+id).append(update_new);
}
</script>
Here while clicking to add more fields its coming correctly but the values selected in that field is not getting while passing the value through post and i have a doubt that if it is caused by using dynamic way off adding the data.
The problem i figured out is the problem is the <div id="update_new_<?php echo $value->id;?>"> is inside a foreach loop so how can we done this with in a foreach loop.
In your script, change .update_new_ to #update_new_
Basically I have this code where I want to extract some data from a database and display it on a pop-up window. The problem is when I click on the eye icon ( labelled More ) I want it to display the correspondent id of that table row but I can't seem to figure out how to do that.
For more references I set up a temporary website to better see the problem : http://twgtest-org.stackstaging.com/bau50/bau50_extract.php
You can see that when I click on more the displayed ID is actually all the IDs from the database instead of it being only the ID of that row
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
echo'<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<div>';
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo $id;
}
echo '</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>';
?>
This is the very simply solution: add a new modal for each row identified by its id for example for id "4" you will open the modal with id "modal4" and so on:
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo'<div class="modal fade" id="more'.$id.'" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<div>';
echo $id;
echo '</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>';
}
?>
This is ok for not so many rows or if you don't want to write some javascript.
A nicer solution would be to use one single modal with all content inside and show/hide the selected one, for this you need to use some js code and don't use the standard bootstrap attribute-driven methods.
In this example I'm using jquery and js code (se at the bottom).
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-moreid="'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo '<div id="more'.$id.'">'.$id.'</div>';
}
?>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- YOU NEED JQUERY FOR THIS: -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(function(){
$("button[data-moreid]").click(function(){
$("#more .modal-body > div").hide(); //hide all more div
var id=$(this).attr("data-moreid"); //get id from pushed button
$("#more .modal-body #more"+id).show(); //show only pushed id
$("#more").modal("show"); //show modal
});
});
</script>
I am working on a project where I came across where when I click plus sign the data will hide and the appear in the div next to it.
here is the code:
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" href onclick="return addSrvToCart('itemservices<?php echo $id ?>')" >
<strong>₹ <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding" id="cart">
<div class="col-md-12" >
</div>
</div>
<div class="col-sm-12 no-right-padding" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
<label class="pull-right">/*what should i write here to show the sum */: ₨ </label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
And This the Jquery code:
<script>
function addSrvToCart(elem){
alert($('#' + elem).html());
$('#' + elem).hide();
//return false;
//what should i write here the show that hidden div
document.getElementById('summery);
return false;
}
</script>
This is the picture and I want to display the data in the Cart div and show the amount
You need to change in both HTML and Script to achieve the result
Try like this
HTML
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" onclick=" addSrvToCart('itemservices<?php echo $id ?>')" >
₹<strong> <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding">
<div class="col-md-12" id="cart" >
</div>
</div>
<div class="col-sm-12 no-right-padding pull-right" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
Total: ₨ <label class="" id="sumAmount">0</label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
Script:
function addSrvToCart(elem){
var div=$('#' + elem);
div.hide();
$('#cart').append(div.html());
var total = parseInt(div.find('strong').html()) + parseInt($('#sumAmount').html());
$('#sumAmount').html(total);
}
It will produce output as
I think it will help you.