First of all, i want you to know that i'm a beginner, (please be kind) and i first searched for a solution but couldn't find.
I'm trying to build a blog using an php mvc framework. In this moment, when i want to edit an article, i'm redirecting to a new page and do there the work, but i want to switch to jquery (or javascript) and i'm stuck to selecting the article category from the select menu. I have an twig global variable containing all the categories (for the navigation). This is my code so far:
JS:
$('#editModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var title = button.data('title')
var category = button.data('category')
var body = button.data('body')
var modal = $(this)
modal.find('#title').val(title)
modal.find('#article-id').val(button.data('id'))
modal.find('#category').val(category)
modal.find('#body').val(body)
});
Modal:
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Changed your mind? Paste your thoughts!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form class="form" method="post" action="/adminzone/articles/update" id="formEditArticle">
<div class="modal-body">
<input id="article-id" name="id" type="hidden" value={{article.id}} />
<div class="form-group">
<label for="title">Title</label>
<input id="title" class="form-control" name="title" placeholder="Title" value="{{ article.title }}" autofocus required pattern=".{2,80}" title="The title must have at least 2 characters and maximim 80."/>
</div>
<div class="form-group">
<label for="category">Category</label>
<select id="category" class="form-control" name="category" required>
{% for category in categories %}
<option value="{{ category.id }}" {% if category.id == article.category %} selected="selected" {% endif %}>{{ category.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea id="body" class="form-control" name="body" placeholder="Paste here your ideas..." required minlength="20" rows=10>{{ article.body }}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-lg btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-lg btn-primary mr-5"><i class="fa fa-lg fa-save"></i> Spreat the word!</button>
</div>
</div>
</form>
</div>
</div>
</div>
The button:
Related
enter image description here
The image is showing the data in the table.
What I want is when I click the view button, it will pop up the modal to display the row of data.
I want get the row of data and display in the pop up modal.
After I added this script, the modal will pop up and close directly.
The modal cannot show, the modal just display one second and close directly
script code
$(document).ready(function() {
$('.resetbtn').on('click', function() {
$('#resetmodal').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children("td").map(function() {
return $(this).text();
}).get();
console.log(data);
$('#pass_id').val(data[0]);
});
});
button code
<button type="button" style="border:none; background-color: transparent;" data-toggle="modal" data-target="#resetmodal" class="resetbtn">view<i class="glyphicon glyphicon-asterisk" style="font-size:15px; color:blue;"></i></button> |
modal code
<div class="modal fade" id="resetmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title" id="exampleModalLabel"><b>RESET PASSWORD</b></h3>
</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="modal-body">
<div class="form-group">
<input name="pass_id" id="pass_id" type="text" class="form-control" value="<?php echo $db_pass_id; ?>"></input>
</div>
<div class="form-group">
<label><b>Password : </b></label> <span class="validation">*</span>
<input name="p_password3" id="p_password3" type="password" class="form-control input-box" onkeypress="return AvoidSpace(event)" onblur="this.value=removeSpaces(this.value);" minlength="6" required></input>
<input type="checkbox" onclick="myFunction3()"> Show Password</input>
</div>
<div class="form-group">
<label><b>Confirm Password : </b></label> <span class="validation">*</span>
<input name="p_password4" id="p_password4" type="password" class="form-control input-box" onkeypress="return AvoidSpace(event)" onblur="this.value=removeSpaces(this.value);" minlength="6" required></input>
<input type="checkbox" onclick="myFunction4()"> Show Password</input>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="reset" class="btn btn-primary">RESET</button>
<button type="button" name="close" class="btn btn-warning" data-dismiss="modal">CLOSE</button>
</div>
</form>
</div>
</div>
</div>
data-target="#resetmodal" does the show/hide for you!. But inside your script you have
$('#resetmodal').modal('show');
In other words, you are showing modal with that script and hiding with data-target="#resetmodal"
i have tried to create form input fields dynamically and i succeeded but when all other fields are being submitted, only the dynamically created fields are not please help out.. thanks..
my javascript is working fine, its creating the fileds just fine inside the modal..
the problem is when i am submitting it only the dynamically added fields are not submitted..
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method="post">
<div class="col-md-6">
<div class="form-group">
<h6> Contact person in case of emergency</h6
<input class="form-control" placeholder="Contact person" required="" pattern="([A-Z][a-z]*\s*[A-Z]*[a-z]*\s*[0-9]*)" title="Alphbetical characters only, capitalize first letter, full name then phone number" value="<?php echo $info[0]['contactperson'];?>" name="contactperson" type="text">
</div>
</div>
<input class="form-control" hidden placeholder="familymembers" id="fm" value="<?php echo $info[0]['familymembers'];?>" name="familymembers" type="number">
<div class="col-md-6">
<div class="form-group">
<h6> </h6>
<button class="btn btn-success">Edit Family Members List</button>
</div>
</div>
//this is a script to add new input fields daynamically
<script type="text/javascript">
function getfmembers(){
var fn=document.getElementById('fm').value;
for (var i = 0; i < 1; i++) {
document.getElementById('fm').value++;
k=document.getElementById('fm').value;
var btn='<div class="form-group"><input class="form-control" name="fmlis" placeholder="Name of the family member" id="'+k+'" type="text"></div>';
$("#after").after(btn);
alert(document.getElementById(k).name);
}
}
function removefmembers(){
var fn=document.getElementById('fm').value;
$("input").remove("#"+fn);
document.getElementById('fm').value--;
}
</script>
// this is the modal to submit the inputs and its inside the form tag
<div class="modal fade" id="seefmlist" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Family Members List</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-3">
<span onclick="getfmembers()" class="btn btn-sm btn-success">Add Member</span>
</div>
<div class="col-md-3">
<span onclick="removefmembers()" class="btn btn-sm btn-danger">Delete Last Member</span>
</div>
</div>
<br>
<div id="after">
<?php
$flist=explode(",",$info[0]['familylist']);
if (count($flist)>1) {
for ($qq=0; $qq < count($flist)-1 ; $qq++) {
echo '<div class="form-group"><input class="form-control" id="'.($qq+1).'" value="'.$flist[$qq+1].'" name="fmlist'.($qq+1).'" type="text"></div>';
}
}
?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Update changes</button>
</div>
</div>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" name='submitupdate' class="btn btn-primary mt-4">Update Employee information</button>
</div>
</form>
line 31 :
for (var i = 0; i < 1; i++) {
only runs once, if you wanted to have this add more than one you will need to fix
also probably the problem you are hung up on
line 34 : name="fmlis" later in your code you have name="fmlist'.($qq+1).' so, on line 34 you probably want to concat k+1 to the name...
I work Laravel application we display data on datatable. When we update data using Bootstrap modal box using foreach then only fetch last data in the table.
Button where we link:
#foreach($patients as $patient)<br>
<li>
<button type="button"
class="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal{{$patient->patient_id}}">Update Fee</button>
</li>
#endforeach
Modal box:
<div id="myModal{{$patient->patient_id}}" class="modal fade" role="dialog">
<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"><span class="glyphicon glyphicon-edit"></span> Edit Fee</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label> Total Fee</label>
<input type="text" name="fee" class="form-control" readonly value="{{$patient->Fee->Fee}}" />
<input type="hidden" name="id" class="form-control">
</div>
</form>
</div>
</div>
</div>
</div>
Just insert your modal inside #foreach directive (your loop) and that should work on your current code.
#foreach($patients as $patient)<br>
<li>
<button type="button"
class="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal{{$patient->patient_id}}">Update Fee</button>
</li>
<div id="myModal{{$patient->patient_id}}" class="modal fade" role="dialog">
<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"><span class="glyphicon glyphicon-edit"></span> Edit Fee</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label> Total Fee</label>
<input type="text" name="fee" class="form-control" readonly value="{{$patient->Fee->Fee}}" />
<input type="hidden" name="id" class="form-control">
</div>
</form>
</div>
</div>
</div>
#endforeach
on my screen, you can see number : 24,26,33 this is the id_user when i click on "Ajouter Mission", the id are set to 24, instead of 26,33. (24 is the id to auth admin), why the id change to the authentified user when i click on modal button ?
dont comment my indian quality front end, i'll do it later :)
and there is my html :
Sorry, i paste everythig because i have no idea where this problem come from.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
#csrf
You are in home as Admin !
#if(Session::has('succes'))
<div class="alert alert-success">
{{ Session::get('succes')}}
</div>
#endif
</br>
Liste des Utilisateurs
<table class="table table-sstriped card-body">
<thead>
<tr>
<th>Numero Matricule</th>
<th>Nom</th>
<th>Prenom</th>
<th>Rue</th>
<th>Code Postal</th>
<th>Ville</th>
<th>Adresse mail</th>
</tr>
</thead>
<tbody>
#foreach($users as $resp)
<tr>
<td>{{$resp->MATRICULE}}</td>
<td>{{$resp->NOM}}</td>
<td>{{$resp->PRENOM}}</td>
<td>{{$resp->RUE}}</td>
<td>{{$resp->CP}}</td>
<td>{{$resp->VILLE}}</td>
<td>{{$resp->email}}</td>
<td>{{$resp->ID_PERSONNELS}}</td>
<td>
#if($resp->id_role==1)
<form action="{{action('UserController#destroy', $resp['ID_PERSONNELS'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
</td>
#endif
<td>
#if($resp->id_role ==1)
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Ajouter Mission{{$resp->ID_PERSONNELS}}
</button>
#else
No Way, it is Admin
#endif
<!-- Modal -->
<div class="modal fade" data-id="$resp->ID_PERSONNELS" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ajouter une Mission a {{$resp->ID_PERSONNELS}}{{$resp->NOM}}{{$resp->email}}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="{{route('addmission',26)}}" method="post">
#csrf
<div class="modal-body justify-content-center">
<!----------------------------------Body---------------------------------------------->
<label for="NOM" class="col-md-4 col-form-label text-md-center">{{ __('NOM') }}</label>
<div class="col-md-6">
<input id="NOM" type="text" class="form-control #error('NOM') is-invalid #enderror" name="NOM" required autocomplete="NOM" autofocus> #error('NOM')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> #enderror
</div>
<label for="DATE_MISSION" class="col-md-4 col-form-label text-md-center">{{ __('DATE_MISSION') }}</label>
<div class="col-md-6">
<input id="DATE_MISSION" type="date" class="form-control #error('DATE_MISSION') is-invalid #enderror" name="DATE_MISSION" required autocomplete="DATE_MISSION" autofocus> #error('DATE_MISSION')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> #enderror
</div>
<!-- <label class="col-md-4 col-form-label text-md-center" for="ID_PRATICIEN">{{ __('PRATICIEN') }}</label> -->
<!-- <select class="custom-select col-md-4 col-form-label text-md-center" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select> -->
<!--------------------------------End----Body------------------------------------>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
<button type="submit" class="btn btn-primary">Ajouter Mission</button>
</form>
</div>
</div>
</div>
</td>
</tr>
</tbody> #endforeach
</div>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection #csrf
store controller :
public function store(Request $request, $id){
$miss = User::find($id);
$mission = new Mission;
$mission->NOM =$request->input('NOM');
$mission->ID_PERSONNELS = $miss->ID_PERSONNELS;
$mission->ID_NOTE_DE_FRAIS = 1;
$mission->DATE_MISSION = $request->input('DATE_MISSION');
$mission->save();
return redirect()->action('AdminController#index')->with('succes', 'Mission Ajoutée');
}
and to be sure, the controller for table :
public function index()
{
$id=auth()->id();
$users = User::all();
return view('homeAdmin')->with('users', $users);
}
thx for help :)
You should be using something like AngularJS for this sort of stuff.
However, I believe your issue is that all your generated modals share the same ID, causing the same one to be opened by all the buttons.
Try making the ID of the modal something like MODAL-{{ ID_HERE }} and remember to set the data-target of the button to MODAL-{{ ID_HERE }}
Do this to your button:
data-target=“#MODAL-{{ $resp->id }}”
And to your modal:
id=“MODAL-{{ $resp->id }}”
I don't know what your database structure looks like but use something unique to each row to implement this, e.g. $resp->id
Honestly though, use something like AngularJS, you wouldn’t then have to programmatically generate a modal for all of the entries, you could just fetch the data on page load, ng-repeat them all in however you want and then have a singular modal that refers to the one they clicked, look into it.
I have this form that lets you choose or add an option to a dropdown field, the problem is when you click the add option, it calls down the modal but it also submits the whole form, but i put a required tag on an input field so it doesn't really pass the form unless completed
Here's the form:
<div class="col-lg-12">
<form action="<?php echo base_url(); ?>products/purchase" role="form" accept-charset="utf-8" method="post">
<div class="form-group">
<div class="col-lg-12">
<label class="control-label">Name</label>
<input type="text" name="name" class="form-control inline" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10">
<label class="control-label">Class</label>
<div class="input-group">
<select name="class_ID" class="form-control" required>
<option value="">Select Class</option>
<?php if(!empty($cls)){
if (is_array($cls)){
foreach ($cls as $row) {?>
<option value="<?php echo $row['class_id']?>"><?php echo $row['class_Name']; ?></option>
<?php }
}
}
else{ ?>
<option value=""></option>
<?php }?>
</select>
<span class="input-group-btn">
<button class="btn btn-theme" data-toggle="modal" data-target="#addClass"><i class="fa fa-plus"></i></button>
</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-12 pull-right">
<input type="submit" class="btn btn-success pull-right" value="Order" style="margin-left:5px;">
</form>
Back
</div>
</div>
</div>
And the Modal:
<div class="modal fade" id="addClass" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add New Product Classification</h4>
</div>
<div class="modal-body">
<form action="<?php echo base_url(); ?>products/add_class" method="post" accept-charset="utf-8" role="form">
<div class="form-group">
<div class="col-lg-6">
<label for="class_Name">Classification Name</label>
<input type="text" name="class_Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-3">
<label for="is_active">Enabled</label>
<select name="is_active" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" value="ADD">
</form>
</div>
</div>
</div>
</div>
</div>
Possibly due to using class btn-success twice, You hit it when opening the modal and its triggered at the bottom of the model footer
balance your html tags
i.e. for every opened tag, there must exist a closed tag and in the same order too.
in your modal, youre opening your form tag inside a div that is closed before the form tag is closed. some browsers may automatically close this tag, and therefore the click on your submit button will propagate to the main form.
change to
<div>
<form>
</form>
</div>
open your form before .modal-dialog
change
<button> to <a> or you can tell <button type="button"> that it's button and should only do what button supposed to do which is not to submit the form
w3schools.com/tags/att_button_type.asp
– Shehary