[英]jQuery $.post doesn't send data
I'm trying to send data via $.post but when I'm check the network section of Firefox, it seems that data is never sent, yet the success event is triggered. 我正在尝试通过$ .post发送数据,但是当我检查Firefox的网络部分时,似乎从未发送过数据,但触发了成功事件。 I don't know what's wrong... I tried with $.post and $.ajax and I got the same result.
我不知道这是怎么回事...我尝试使用$ .post和$ .ajax获得相同的结果。
HTML HTML
<button id="nuevoadmin">Agregar administrador</button>
<article id="addadmin">
<form id="newadmin">
<label>Nombre</label> <input type="text" name="nomadmin" required/>
<label>Puesto</label>
<select name="puesto" id="jobs">
</select>
<label>Correo electrónico</label> <input type="email" name="emailadmin" required/>
<span class="vacio">Campo requerido</span>
<span class="malcorreo">Introduzca una dirección de correo válida</span>
<label>Clave de acceso</label> <input type="password" name="claveadmin" required/>
<span class="badpass">La contraseña sólo puede contener de 6 a 10 caracteres alfanuméricos</span>
<button id="enviaadmin">Agregar</button>
</form>
</article>
jQuery for ajax functions jQuery for Ajax函数
$(document).ready(function() {
muestraemp();
$('#nuevoadmin').on('click', listaPuesto);
$('#admins table tbody').on('tr td:last-child', 'click', function() {
var rowIndex = $(this).closest('tr').data('value');
modifica(rowIndex);
});
$('#lightbox').ready(function() {
$('#enviaadmin').on('click', function() {
var datos = $(this).parent('form').serialize();
enviar(datos);
});
});
});
function listaPuesto() {
var puestos = '';
var uno = '<option value="0" selected>Seleccione un puesto</option>';
$.getJSON('Main.php', 'add', function(data) {
$.each(data, function(index, item) {
puestos += '<option value="'+item.puesto+'">'+item.puesto+'</option>';
});
$('#jobs').html(uno+puestos);
});
}
function muestraemp() {
var empleados = '';
$.post('Main.php', function(data) {
$.each(data, function(index, item) {
empleados += '<tr data-value="'+item.idEmpleado+'"><td>'+item.nombre+'</td><td>'+item.correo+'</td><td>'+item.puesto+'</td><td>lololo</td></tr>';
});
$('#admins table tbody').append(empleados);
}, 'json');
}
function enviar(formemp) {
$.post('Main.php', formemp, function(data) {
$('#successadd').show();
muestraemp();
});
}
Lightbox is generated dynamically in another script 灯箱是在另一个脚本中动态生成的
var nuevoadmin = $('#addadmin');
$('#nuevoadmin').on('click', function() {
lightbox(nuevoadmin);
});
And this is the url I'm sending data 这是我发送数据的网址
<?php
include('Empleado.php');
$empleado = new Empleado('localhost', 'root', 'mysql', 'mydb');
if(isset($_GET['add'])) {
$empleado->listaPuestos();
}
else {
$empleado->muestraEmpleados();
}
if(isset($_POST['formemp'])) {
$empleado->agregaEmpleado($_POST['nomadmin'], $_POST['claveadmin'], $_POST['emailadmin'], $_POST['puesto']);
//$empleado->muestraEmpleados();
}
?>
Thanks in advance. 提前致谢。
You are trying to detect form submission using a invalid key or form field. 您正在尝试使用无效的键或表单字段来检测表单提交。
You have to use any of the form field to detect form submission or simply detect using following code 您必须使用任何表单字段来检测表单提交或仅使用以下代码进行检测
if(isset($_POST)){
/* Form submitted using post method process data here... */
}
I solved it. 我解决了 The problem was that I was expecting to fetch the name of the variable that contains the serialized form.
问题是我期望获取包含序列化表格的变量的名称。 I just changed this line:
我刚刚更改了这一行:
if(isset($_POST['formemp']))
To this: 对此:
if(isset($_POST['nomadmin'], $_POST['claveadmin'], $_POST['emailadmin'], $_POST['puesto']))
Anyway, thanks for your help! 无论如何,谢谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.