I have modal bootstrap window that insert data in database using AJAX and JQUERY but I couldn't close window after insert and I want also to show message like Successfully inserted see below code
---- modal window ----
<!-- Modal employer-->
<div class="modal fade" id="employer" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form" action='insert.php' method='post' id='myform'>
<div class="form-group">
<label for="exampleInputEmail1">Nom</label>
<input type="text" name="nom"class="form-control"
id="exampleInputEmail1" placeholder="Nom"/>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Prenom</label>
<input type="text" name="prenom" class="form-control"
id="exampleInputPassword1" placeholder="Prenom"/>
</div>
<div class="form-group">
<label for="exampleInputPassword1">telephone</label>
<input type="text" name="telephone"class="form-control"
id="exampleInputPassword1" placeholder="telephone"/>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Fonction</label>
<input type="text" name="fonction" class="form-control"
id="exampleInputPassword1" placeholder="Fonction"/>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Salire</label>
<input type="text" name="salaire" class="form-control"
id="exampleInputPassword1" placeholder="Salire"/>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" id='insert' class="btn btn-primary">
Save changes
</button>
</form>
</div>
</div>
</div>
</div>
---- insert.php ----
<?php
require('config.php');
$nom = $_POST['nom'];
$prenom = $_POST['prenom'];
$telephone = $_POST['telephone'];
$fonction = $_POST['fonction'];
$salaire = $_POST['salaire'];
$sql = "insert into employer (nom, prenom,telephone,fonction,salaire) values ('$nom','$prenom','$telephone','$fonction','$salaire')";
if(mysqli_query($con, $sql)){
echo 'success';
?>
<script type="text/javascript">
$(document).ready(function(){
$('#employer').modal('hide');
});
</script>
<?php
}
?>
---- insert.js----
$('#myform').submit(function(){
return false;
});
$('#insert').click(function(){
$.post(
$('#myform').attr('action'),
$('#myform :input').serializeArray(),
function(result){
$('#result').html(result);
}
);
});
When you get your response from ajax request then close modal window.
$('#employer').modal('hide');
As according to your example:
$('#insert').click(function(){
$.post(
$('#myform').attr('action'),
$('#myform :input').serializeArray(),
function(result){
$('#employer').modal('hide'); // ADD CODE FOR CLOSE MODAL
$('#result').html(result);
}
);
});
Try this:
$('#modal').modal('close');
It will close the modal.
Related
I am trying to get the value from a input form, the problem is that I create this input form with jQuery and Ajax so if I check what is the value of that input, it is empty.
The Html that I load dynamically:
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputEmail1">Desde</label>
<input type="time" class="form-control" id="hola" aria-describedby="emailHelp" placeholder="Desde">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputEmail1">Hasta</label>
<input type="time" class="form-control" id="until" aria-describedby="emailHelp" placeholder="Hasta">
</div>
</div>
</div>
<hr>
<button type="button" data-id="'.$i.'" class="btn btn-primary search_turn">Buscar</button>
<hr>
<h4>Turnos Disponibles</h4>
<div id="table" class="table-responsive">
<table style="width: 100%;" class="table table-bordered table-striped">
<thead>
<tr>
<th>Nombre</th>
<th>Lu</th>
<th>Ma</th>
<th>Mi</th>
<th>Ju</th>
<th>Vi</th>
<th>Sa</th>
<th>Do</th>
<th>Horas</th>
</tr>
</thead>
<tbody class="turns">
</tbody>
</table>
</div>
</div>
<div class="modal-footer">
<input type="hidden" id="year" value="">
<input type="hidden" id="month" value="">
<input type="hidden" id="id_week" value="'.$i.'">
<input type="hidden" id="id_branch_office" name="id_branch_office" value="'.$id_branch_office.'">
<button type="button" class="btn btn-primary">Continuar</button>
</div>"'
This html above I added to this modal dinamically:
<!-- Modal -->
<div class="modal fade bd-example-modal-xl" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Seleccionar Turno - Semana</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
</div>
</div>
</div>
When I click in the button Buscar from the first HTML the input since must take the value that I put but it is empty.
My Ajax code:
$( 'body' ).on( "click", '.search_turn', function() {
var $('#edit-auction-modal ').find('input#input-id')
var id = $(this).attr('data-id');;
var since = $('#since').val();
var until = $('#until').val();
var id_week = $('#id_week').val();
var id_branch_office = $('#id_branch_office').val();
if(since != '' && until != '')
{
alert(5);
$.ajax({
type:'POST',
url:"{{ url('/turn/search') }}",
data: {
_token: "{{csrf_token()}}", since: since, until: until, id_branch_office: id_branch_office, id_week: id_week
},
success:function(result)
{
$(".turns").html('');
$(".turns").html(result);
}
});
}
});
If I check since variable is empty, why?
I am newbie to web programming. I would like to insert data into MySQL DB, for that I am using PHP and this data is coming from a form in a Bootstrap Modal. When I click the Savebutton, nothing is happening. I been following a this tutorial and I have done exactly that tutor is doing. What went wrong with my code?
HTML Code and JavaScript:
<body>
<p><br/></p>
<div class="container">
<p></p>
<button class="btn btn-primary" data-toggle="modal" data-target="#addData">Insert data</button>
<!-- Modal -->
<div class="modal fade" id="addData" tabindex="-1" role="dialog" aria-labelledby="addLabel">
<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" id="addlLabel">Insert Data</h4>
</div>
<form>
<div class="modal-body">
<div class="form-group">
<label for="nm">Name</label>
<input type="text" class="form-control" id="nm" placeholder="Name here">
</div>
<div class="form-group">
<label for="em">Email</label>
<input type="email" class="form-control" id="em" placeholder="Email">
</div>
<div class="form-group">
<label for="hp">Hone Number</label>
<input type="number" class="form-control" id="hp" placeholder="Your Phone">
</div>
<div class="form-group">
<label for="al">Address</label>
<textarea class="form-control" id="al" placeholder="Your address"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" onclick="saveData()"class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-3.1.1.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
function saveData(){
var name=$('#nm').val();
var email=$('#em').val();
var phone=$('#hp').val();
var address=$('#al').val();
$.aja({
type:"post",
url:"server.php?p=add",
data:"nm="+name+"&em="+email+"&hp="+phone+"&al="+address,
success: function(msg){
alert('Success Insert data');
}
});
}
</script>
PHP Code:
<?php
$db=new PDO('mysql:host=localhost;dbname=ajax','root','');
$page=isset($_GET['p'])?$_GET['p']:'';
if($page=='add'){
$name=$_POST['nm'];
$email=$_POST['em'];
$phone=$_POST['hp'];
$address=$_POST['al'];
$stmt=$db->prepare("insert into users values('',?,?,?,?)");
$stmt->bindParam(1,$name);
$stmt->bindParam(2,$email);
$stmt->bindParam(3,$phone);
$stmt->bindParam(4,$address);
if ($stmt->execute()) {
echo "Success";
}else{
echo "Fail".mysqli_error();
}
}else if($page=='edit'){
}if($page=='del'){
}else{
}
?>
I have used the development in Chrome, no error is being shown.
Typo error.You have $.ajx({}).It must be $.ajax({}).Also send your data in object format.like this..
$.ajax({
type:"post",
url:"server.php?p=add",
data:{nm:name,em:email,hp:phone,al:address},
success: function(msg){
alert('Success Insert data');
}
});
I am trying to added record using a modal. I have use the following code but my record is not added into the database.. help me through pls
<link rel="stylesheet" type="text/css" href="/Project/Style/bootstrap.css"/>
<script src="Style/jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<?php
include('header.php');
require_once('Connections/conn.php');
$sql2= "SELECT * from type where cid='1' ";
$results =mysqli_query($conn,$sql2);
?>
<div id="main">
<div class="container" >
<div class="row">
<div class="form-group col-md-3">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
++Add Farm Product
</button>
</div>
<div class="input-group col-md-4 col-sm-offset-2">
<span class="input-group-addon">Search</span>
<input type="text" class="form-control" name="search-text" id="search-text" placeholder="Search By Product Name">
</div>
</div>
<div id="result"></div>
<!-- Modal -->
<div class="modal fade" id="myModal" 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-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Farm Product</h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="col-md-6 padding-top-10">
<label for="proname" class="control-label">Product Name:</label>
<input type="text" class="form-control" id="proname" placeholder="Product Name" name="proname" />
</div>
<div class="col-md-5 padding-top-10">
<label for="pro-type" class="control-label">Product Type:</label>
<select class="form-control" id="pro_type" name="pro_type" >
<option>Select Product type...</option>
<?php foreach($results as $result): ?>
<option value= " <?php echo $result['tid']; ?>" > <?php echo $result['type_name']; ?>
</option>
<?php endforeach ;?>
</select>
</div>
</div><br/>
<div class="row">
<div class="col-md-4 padding-top-10">
<label for="qty" class="control-label">Quantity:</label>
<input type="text" class="form-control" id="qty" placeholder="Quantity" name="qty" />
</div>
<div class="col-md-4 padding-top-10">
<label for="price" class="control-label">Unit Price :</label>
<input type="text" class="form-control" id="price" placeholder="Unit Price" name="price" />
</div>
<div class="col-md-4 padding-top-10">
<label for="pdate" class="control-label"> Production Date :</label>
<input type="date" class="form-control" id="pdate" placeholder="Production Date" name="pdate" />
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
<button type="button" id="add" name="add" class="btn btn-primary">Add Product</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
// haddling the post for adding record
$('#add').click(function(){
var proname = $('#proname').val();
var pro_type = $('#pro_type').val();
var qty = $('#qty').val();
var price = $('#price').val();
var pdate = $('#pdate').val();
var datas="proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate"+pdate;
$.ajax({
type: "POST",
url: "addpro_exec.php",
data: datas,
success:function(data){
},
error: function(){
}
});
});
</script>
The code handling the posted data is below
//addpro_exec.php file
<?php
require_once('Connections/conn.php');
if(isset($_POST['add'])){
$proname=$_POST['proname'];
$pro_type= $_POST['pro_type'];
//$typname=$_POST['type_name'];
$qty= $_POST['qty'];
$price= $_POST['price'];
$pdate= $_POST['pdate'];
$asdate = \date('F');
$sql="INSERT into product VALUES('','$proname','','$qty','$price','$pdate','','$pro_type','1') ON DUPLICATE KEY UPDATE qty = qty+$qty";
$sql2= "INSERT into year_estm VALUES('','$proname','$asdate','$qty','') ON DUPLICATE KEY update Production_input=Production_input+$qty ";
$AddProduction= mysqli_query($conn, $sql2) or die('Cannot add to production'. mysqli_error($conn));
$RecAdd= mysqli_query($conn,$sql) or die('Cannot add Product' . mysqli_error());
if($RecAdd){
header('location:viewfarm.php');
}
}
else{
}
?>
Try the following line in your javascript:
var datas="add=1&proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate="+pdate;
That should fix the problem.
WARNING: Always filter and a properly quote user input before inserting into a database in order to avoid SQL injection hacks. Use prepared statements to help achieve this.
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
I'm trying a modal to my php page so when the form is submitted a modal would be triggered, but it doesn't work
here's my code:
<?php
if(isset($_POST['add'])){
$genre=$_POST['genre'];
$desc_genre=mysqli_real_escape_string($cnxn,$_POST['desc_genre']);
$sql = "INSERT INTO genres (genre, desc_genre) VALUES ('".$genre."', '".$desc_genre."')";
if(mysqli_query($cnxn, $sql)){
echo "<script>
$('#myModal').modal('show');
</script>";
}
?>
<div id="myModal" class="modal hide fade" tabindex="-1" data-backdrop="static" data-keyboard="false">
<div class="modal-body">
<p>This Genre Has Been Added Successfully</p>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn">Cancel</button>
<button type="button" data-dismiss="modal" class="btn btn-primary">Continue Task</button>
</div>
</div>
<div class="login-page">
<?php }else{ ?>
<div class="form">
<form class="login-form" name="f1" method="post" action="" enctype="multipart/form-data">
<input required name="genre" type="text" placeholder="Add Genre"/>
<textarea required name="desc_genre" type="text" placeholder="Description of the Genre"></textarea>
<button type="submit" name="add">ADD</button>
</form>
</div>
ps: everything is running fine except for the modal showing
This will be running before jQuery has loaded. You need it to run after the library has loaded:
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
I am using Twitter Bootstrap for both tabs and modals. I have two tabs that use identical code. In the first tab the modals work perfectly, but in the second tab the screen will darken but the modal does not appear.
here is my code for calling the modal (same code used in both tabs) in list.php:
<a type="button" href="edit.php?id='.$id.'" data-target="#edit_modal" data-toggle="modal">Edit</a>
here is my modal in list.php
<div class="modal hide fade" id="edit_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Edit Testimonials</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
</div>
and here is edit.php where the true body of the modal is
<div id="edit_modal">
<div class="modal-body">
<?php echo ($name_error != "") ? $name_error : ""; ?>
<form class="form-horizontal" action="edit.php?id=<?php echo $id; ?>" method="post">
<div class="control-group">
<label class="control-label">Testimonial</label>
<div class="controls">
<textarea class="input-xlarge" name="body" cols=100 rows=5><?php echo $testimonial['body']?></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Author</label>
<div class="controls">
<textarea class="input-xlarge" name="author" cols=100 rows=5><?php echo $testimonial['author']?></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Update Testimonial" name="submit-form" />
Close
</div>
</form>
</div>