I have a modal window that sends data to a table in mysql called (jobs) that has three fields (Job_id, Job_Name and Cost) and likewise shows that data in the table in html
My problem is that when sending the data, I get a message that it was correctly, but I sent it to another page and I have to return to the previous page and reload to update, I want that message to me come out in my modal window, or in my page where I have my table (consult_list.php), they told me to use ajax, but I do not know how it is done, I have the following in my file (add_modal.php), so that it runs I have to have a bookstore, do not I know maybe jquery or something? and if it's okay to put my script in that file? or can I place it where I have my table?
<script type="text/javascript">
$('#guardar').click(function(){
var parametros = {
"nombre" : $("#txt_nombre").val(),
"costo" : $("#txt_costo").val()
}
$.ajax({
data : parametros,
url : "agregarModal_validacion.php"
type : "post"
success : function(response){
}
})
});
</script>
<!-- Modal-->
<div id="myModal1" class="modal fade" role="dialog">
<div class="modal-dialog modal-sm">
<!-- Modal content-->
<div class="modal-content">
<!--Cabecera del modal-->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Nuevo Trabajo</h4>
</div>
<!--Contenido del modal-->
<div class="modal-body">
<div class="form-group">
<div class="col-xm-6">
<input class="form-control" type="text" id="txt_nombre" name="txt_nombre" placeholder="Nombre del trabajo" required="">
</div>
</div>
<div class="form-group">
<div class="col-xm-6">
<input class="form-control " type="text" id="txt_costo" name="txt_costo" placeholder="Costo" required="">
</div>
</div>
</div>
<!--Final del modal-->
<div class="modal-footer">
<button type="submit" id="guardar" class="btn btn-success btn-lg" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Guardar</button>
</div>
</div>
</div>
</div>
Is it correct in this way? or how else can I do it? and is that they told me that in my php change the answer and leave it with json, but what is that? How do I do it? I hope you can help me please, I leave my code for my php
<?php
?>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minium-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
<?
$conexion= mysqli_connect("localhost", "root", "root", "registros");
if($conexion)
{
//Variables
$Nombre_Trabajo = $_POST['nombre'];
$Costo=$_POST['costo'];
//realiza la consulta
$consulta= "INSERT INTO trabajos (Nombre_Trabajo, Costo) values ('$Nombre_Trabajo','$Costo')";
//para ejecutar consulta
$resultado=mysqli_query($conexion ,$consulta);
if ($resultado)
{
$messages[] = "Los datos han sido agregados correctamente";
}
else
{
$errors[]= "No se puedo realizar la accion";
}
if (isset($errors)){
?>
<div class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="consulta_lista.php">×</button>
<strong>Error!</strong>
<?php
foreach ($errors as $error) {
echo $error;
}
?>
</div>
<?php
}
if (isset($messages)){
?>
<div class="alert alert-success" role="alert">
<button type="button" class="close" data-dismiss="myModal1">×</button>
<strong>¡Bien hecho!</strong>
<?php
foreach ($messages as $message) {
echo $message;
}
?>
</div>
<?php
}
}
else{
echo "";
}
mysqli_close($conexion);
?>