How to set the modal shows something based on a button click? - php

I really want that my Modal shows something based on which button inside it i press, but i have no idea of how to do this.
Actually i tried to use if(isset($_POST['buttoname'])
but looks like this dowsn't work `
This is my modal , i just want to place the content on the right side (which is white ) , but actually it doesn't work.
Here's the code i tried to use
<!-- Modal Settings -->
<div class="modal fade" id="Settings" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="background-color: #d9534f;color:white !important;text-align: center;font-size: 30px">
<button type="button" class="close" data-dismiss="modal" style="background-color: #d9534f;color:white !important;text-align: center;font-size: 30px">×</button>
<h4 style="background-color: #d9534f;color:white !important;text-align: center;font-size: 30px"><span class="fa fa-gear"></span> Settings</h4>
</div>
<div class="modal-body">
<div class="container">
<div class="list-group col-sm-1">
<form method="post" href="#">
<button type="submit" name="Profile" href="#" class="list-group-item">First </button>
<button type="submit" name="Logins" href="#" class="list-group-item">First </button>
<button type="submit" name="Security" href="#" class="list-group-item">First </button>
</form>
</div> <!-- List -->
</div>
<?php
if(isset($_POST['Profile'])){
echo' <div class="container">
<div class="col-sm-11">
<p> This is a test</p>
</div>
</div>';
}
?>
</div>
<div class="modal-footer" style="background-color: #f9f9f9;">
<button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
</div>
</div>
</div>
</div>
<!-- Modal Settings -->

You need to actually post the button click to have it stored in the $_POST. So just leave the form action empty and it will post this form to the url you already are in.
But this will refresh the page so if you want to make it without refreshing you will need to use javascript.
For example if you are using jquery and you are if you are using bootstrap
In you html put some placeholder div instead of this php if:
</div> <!-- List -->
</div>
<div id="placeholder"></div>
</div>
And in your javascript code:
$("button[name='Profile']").click(function() {
$("#placeholder").html("YOUR HTML CODE");
});

Related

Bootstrap modal on a separated file doesn't close

I'm using modal from bootstrap. The modal opens but it doesn't close either with the cross or close button.
This is what I have on my index.php file:
<div id="editar" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
</div>
And this is the modal which is in a separated .php file:
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Editar Accion</h4>
<button type="button" class="close" data-dismiss="modal" id="cerrar">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="hidden" name="editar" value="<?php echo $id; ?>">
<div class="form-group">
<label for="item_name">Accion:</label>
<input type="text" class="form-control" id="accion" value="<?php echo $accion; ?>">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary mb-3" name="update" id="btn-editar"><span class="glyphicon glyphicon-edit"></span> Editar</button>
<button type="button" class="btn btn-secondary mb-3" data-dismiss="modal"><span class="glyphicon glyphicon-remove-circle"></span> Cancelar</button>
</div>
</div>
</div>
I have to keep them in two files.
If you noticed my comment, I have told you its headache to use two different pages. Also it is not recommended way.
I have created a button for you and on the button click the modal shows up with cancel button that goes away when you click it. It is the way exactly you wanted afterwards.
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Click Me
</button>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Editar Accion</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<input type="hidden" name="editar" value="<?php echo $id; ?>">
<div class="form-group">
<label for="item_name">Accion:</label>
<input type="text" class="form-control" id="accion" value="<?php echo $accion; ?>">
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-primary mb-3" name="update" id="btn-editar"><span class="glyphicon glyphicon-edit"></span> Editar</button>
<button type="button" class="btn btn-secondary mb-3" data-dismiss="modal"><span class="glyphicon glyphicon-remove-circle"></span> Cancelar</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Hope this helps.
I think it is better to make use of JQuery so that you can have more control over what happens (e.g: clear fields, etc) on the modal on close.
$(document).on('click','#button_id',function(e){
e.preventDefault();
//you can clear fields first if you need to
$('#accion').val('');
$('#modal_id').hide(); //close modal
});
Please remember to include the file into the index.php. Keeping modal in separate file will help you reuse the same somewhere in your code, that is if you keep your modal code dynamic enough.

how to show a php page in a modal

I was wondering if it is possible to catch a whole php page in a modal.
It is about this situation:
I have now an anchor which leads me to the page like this:
<?php echo $article['title']; ?>
How can i put the whole content of, lets say, the page post.php?id=1 in a modal like this:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>HERE SHOULD COME THE CONTENT OP THE PHP FILE</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
in post.php; these are the lines which parse the content:
<h3> <?php echo $article['title']; ?> </h3>
<p class="content article-content"> <?php echo nl2br($article['content']); ?> </p>
Using jQuery you can do something like this:
$('#modal-body').load('/your/url/here', {var1: val1, var2: val2}, function(){
// show your modal here
});
See here for more info https://api.jquery.com/load/

Display a bootstrap dynamic modal

I am trying to display a modal when pressing the button "Vezi rezolvare"
I have the following code for the button:
<div class="col-md-4 text-center patrat-block">
<div class="thumbnail">
<img class="img-responsive" src="img/LogoProbleme/pg1logo-v2.jpg" alt="">
<div class="caption">
<h3>Problema 1<br>
<small>Gradina</small>
</h3>
<p>Află dimensiunile grădinii</p>
<ul class="list-inline">
<li>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Problema"><span class="glyphicon glyphicon-pencil"></span> Vezi Rezolvare </button>
</li>
</ul>
</div>
</div>
</div>
And the modal content + php :
<?php
if(isset($_GET['id'])&&isset($_GET['category']))
{
$id=$_GET['id'];
$category=$_GET['category'];
$sql = "SELECT cerinta, rezolvare FROM probleme WHERE id='".$id."'AND category='".$category."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
$cerinta=$row["cerinta"];
$rezolvare=$row["rezolvare"];
}
$_SESSION["cerinta"]=$cerinta;
$_SESSION["rezolvare"]=$rezolvare;
}
}
?>
<div id="Problema" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Problema</h4>
</div>
<div class="modal-body">
<h4>Cerinta</h4>
<div class="well">
<?php echo $cerinta;?>
</div>
<h4>Rezolvare</h4>
<div class="well">
<?php echo $rezolvare;?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Inchide fereastra</button>
</div>
</div>
</div>
</div>
What this is trying to accomplish is to show a modal with some information in my database. I checked and the values are stored successfully in $cerinta and $rezolvare.
It seems like when the button is pressed, there is a fast fade and I am redirected to the top of the page, without showing the modal. I put an <a href="probleme.php?id=1&category=g"> so I know what button has been pressed, but it seems like it refreshes the page and the modal isn't displayed. How can I let the modal be displayed?
Nesting a <button> inside an achor <a> is not valid html syntax because it can't tell which one was clicked. Also the anchor has an href different than '#' so it's just redirecting you to probleme.php?id=1&category=g. You can modify your code as follows (not tested):
Remove the button from inside the anchor and change the anchor to:
<a href="probleme.php?id=1&category=g" class="btn btn-primary"
data-toggle="modal" data-target="#Problema">
<span class="glyphicon glyphicon-pencil"></span> Vezi Rezolvare
</a>
Have the template of the modal somewhere in your html:
<div id="Problema" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Problema</h4>
</div>
<div class="modal-body">
Some fine body
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Inchide fereastra
</button>
</div>
</div>
</div>
</div>
Modify your php to return only the body of the modal:
<?php
// Some good php
echo("
<h4>Cerinta</h4>
<div class=\"well\">
{$cerinta)}
</div>
<h4>Rezolvare</h4>
<div class=\"well\">
{$rezolvare}
</div>");
// The modal body is returned by php
?>
Use ajax to retrieve the modal body and subsequently open the modal. Something in the lines of:
$(document).ready(function() {
// Assign some id to the anchor say id="myAnchor"
$('#myAnchor1').click(function(e) {
e.preventDefault();
// get the modal
var modal = $($(this).data("data-target"));
var url = $(this).attr("href");
var request = $.get(url);
request.done(function(response) {
// get modal
modal.find(".modal-body").html(response);
modal.modal("show");
});
});
});

Bootstrap Modal Works On Mobile Device Only

Hello,
I created a bootstrap 3 modal and it works on my mobile device but on my desktop when you click the button it does not open the modal.
I've tried resizing the modal box even adjusting it's zindex but those did not fix the problem.
Does anyone know what the issue is i'm having?
<div class="container-fluid formbox ">
<div id="cageModal" class="modal fade" role="dialog" class="col-sm-10 col-sm-offset-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Cage The Great</h4>
</div>
<div class="modal-body">
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/VqghSmwdnpg" frameborder="no" allowfullscreen></iframe>
<button type="button" class="btn btn-default" data-dismiss="modal">See More</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="container-fluid formbox ">
<div class="col-xs-12 col-sm-12">
<h3>Get Familiar with the artists below.</h3>
<div class="col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3">
<div> <button type="button" class="btn btn-block btn-info btn-lg" data-toggle="modal" data-target="#cageModal">Cage The Great</button>
</div>
</div>
</div>
</div>
Non-working code on jfiddle
Your code is correct. But your bootstrap references (CDN) are not.
replace your references to Bootstrap with the latest. Or better yet, use Bootstrap locally and never touch it!
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
Here is your broken jfiddle, now working

Two Bootstrap Modals to edit two different files content

First let me start by saying I know this is going to be a really basic question. I am new to PHP and struggling a little with this task.
Intro:
I am making a little app on a local WAMP server to manage a basic client database and also manage all my VHOST enntries for development. I know there are security issues with editing Windows Hosts file as I'm about to request but this will be strictly a local site.
I have the following PHP
<?php include 'template-parts/header.php' /** calling of header(to make it uniform in all template file) **/?>
<div class="container home">
<h3> Delete </h3>
<div class="btn-group">
<button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button>
<button data-toggle="modal" class="btn btn-primary btn-sm" name="vhostsedit" data-target="#modalvhost"><span class="glyphicon glyphicon-trash"></span> Edit VHOST.conf</button>
</div>
<?php
// configuration
$url = 'delete.php';
$file = 'C:/Windows/System32/Drivers/etc/hosts';
// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
file_put_contents($file, $_POST['text']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
<!-- Modal 1 -->
<div class="modal fade" id="modalhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">×</button>
<h4 class="modal-title">Delete Host File Entry</h4>
</div><!-- /modal-header -->
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-12">
<!-- HTML form -->
<form action="" method="post">
<textarea name="text" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
<p><strong>NOTE:</strong> Ensure only lines similar to <kbd>127.0.0.1 www.dev.xxxxx</kbd> are deleted</p>
</div>
</div>
</div>
</div><!-- /modal-body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->
<!-- Modal 2 -->
<div class="modal fade" id="modalvhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">×</button>
<h4 class="modal-title">Delete VHOST File Entry</h4>
</div><!-- /modal-header -->
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-12">
<!-- HTML form -->
<form action="" method="post">
<textarea name="text" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
</div>
</div>
</div>
</div><!-- /modal-body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->
</div>
</div>
</body>
</html>
The first modal works perfectly, I click the button <button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button> and this opens up the Bootstrap Modal, presents the Windows Host file within the textarea and allows me to add and delete at will, I click the modal's save button and it works.
The question I need to acheive this same action with the second modal window however this one needs would need to edit C:/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf'.
I simply don't know how to duplicate the first correctly working PHP to now also work with the VHOST editing requirement.
Any help or pointers would be greatly appreciated.
EDIT
Hi Neal, I tried putting your solution into place and got some errors that I'm not entirely sure how to get past...
I currently have this in my PHP file:
<div class="container home">
<h3> Delete </h3>
<div class="btn-group">
<button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button>
<button data-toggle="modal" class="btn btn-primary btn-sm" name="vhostsedit" data-target="#modalvhost"><span class="glyphicon glyphicon-trash"></span> Edit VHOST.conf</button>
</div>
<?php
// check if form has been submitted
if (!empty($_POST['hostinput'])){ //i prefer to use empty rather than isset you can read about it
// configuration
$url = 'delete.php';
$file = 'C:/Windows/System32/Drivers/etc/hosts';
// check if form has been submitted
if (isset($_POST['hostinput']))
{
// save the text contents
file_put_contents($file, $_POST['hostinput']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($file);
}
else if(!empty($_POST['vhostinput'])){
// configuration
$url = 'delete.php';
$file = 'C:/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf';
// check if form has been submitted
if (isset($_POST['vhostinput']))
{
// save the text contents
file_put_contents($file, $_POST['vhostinput']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($file);
}
?>
<!-- Modal 1 -->
<div class="modal fade" id="modalhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">×</button>
<h4 class="modal-title">Delete Host File Entry</h4>
</div><!-- /modal-header -->
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-12">
<!-- HTML form -->
<form action="" method="post">
<textarea id="hostinput" name="hostinput" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
<p><strong>NOTE:</strong> Ensure only lines similar to <kbd>127.0.0.1 www.dev.xxxxx</kbd> are deleted</p>
</div>
</div>
</div>
</div><!-- /modal-body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
<!-- Modal 2 -->
<div class="modal fade" id="modalvhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">×</button>
<h4 class="modal-title">Delete VHOST File Entry</h4>
</div><!-- /modal-header -->
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-12">
<!-- HTML form -->
<form action="" method="post">
<textarea id="vhostinput" name="vhostinput" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
</div>
</div>
</div>
</div><!-- /modal-body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->
</div>
And the error I get on the second call is:
Notice: Undefined variable: text in C:\Users\xxx\xxx\xxx\devlogs\delete.php on line 116
<button class="btn btn-primary btn-sm" data-target="#myModal1" data-toggle="modal" type="button"> Edit</button> <!-- first button for the first modal-->
<button class="btn btn-primary btn-sm" data-target="#myModalA1" data-toggle="modal" type="button"> Comment</button> <!-- second button for the second modal-->
<div id="myModal1" class="modal fade bs-example-modal-lg" aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1">
<div id="myModalA1" class="modal" aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1">
This code seems similar to your code but im not too sure if there is some differences (this one i am 100% sure that its working perfectly fine).
Now for your question where you want to submit the form for the second modal. There are many ways to handle this ( like using ajax and saving everything without the need of forms or refreshing the page.) but since you said you are new to php and you already started in this way so... what i can suggest is that....
1) for every input you have you must give it an id and name. so your inputs should look like this (you can set the same name and id for the same field but every field must be unique):
<textarea class="form-control" rows="15" id="hostinput" name="hostinput"></textarea>
<textarea class="form-control" rows="15" id="vhostinput" name="vhostinput"></textarea>
2)Then in your php code you type:
// check if form has been submitted
if (!empty($_POST['hostinput'])){ //i prefer to use empty rather than isset you can read about it
// save the text contents
file_put_contents($file, $_POST['text']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Moved.', htmlspecialchars($url));
exit();
}
esleif(!empty($_POST['vhostinput'])){
//SAVE THE VHOST
}
This way what you are doing is whenever a submit button pressed you try and check the two values and save them again both of them.
WARNING: this method is a bit dangerous.... lets say that the user edited the first text but then he clicked cancel.... then edited the second text then he clicked submit for the second text.... you will actually save both of them....
The way i usually do this is by using javascript and ajax so i dont have to refresh the page also...
Hope i helped....

Categories