Llevo varios dias intentando mandar un formulario bootstrap mediante POST con Jquery y cuando recibo los datos en el archivo PHP tienen valores nulos. En cambio si hago lo mismo por GET va perfecto.
El formulario lo muestro tambien con bootstrap.
FORMULARIO
Código:
JQUERY<div id='new-users>' class='modal fade'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close new-entity' data-dismiss='modal' aria-label='Close'> <span aria-hidden='true'>×</span> </button> <h4 class='modal-title'>New user</h4> </div> <form id='new-<?php echo $entity ?>-form'> <div class='modal-body'> <fieldset> <h4><p class='text-light-blue'>Personal Information</p></h4> <div class='row'> <div class='col-sm-6'> <div class='form-group'> <label for='username'>User name</label> <input class='form-control' id='username' name='username' type='text' /> </div> </div> <div class='col-sm-6'> <div class='form-group'> <label for='password'>Password</label> <input class='form-control' id='password' name='password' type='password' /> </div> </div> </div> <div class='row'> <div class='col-sm-12'> <div class='form-group'> <label for='fullname'>Full name</label> <input class='form-control' id='fullname' name='fullname' type='text' /> </div> </div> </div> <div class='row'> <div class='col-sm-6'> <div class='form-group'> <label for='email'>Email</label> <input class='form-control required email' id='email' name='email' type='text' /> </div> </div> <div class='col-sm-6'> <div class='form-group'> <label for='status'>Status</label> <div class='form-group'> <label class='radio-inline'> <input type='radio' name='status' id='status' value='1' checked> Enabled </label> <label class='radio-inline'> <input type='radio' name='status' id='status' value='0'> Disabled </label> </div> </div> </div> </div> </fieldset> <fieldset> <h4><p class='text-light-blue'>Other Information</p></h4> <div class='row'> <div class='col-sm-12'> <div class='form-group'> <label for='description'>Description</label> <textarea placeholder='Enter comments ...' rows='4' class='form-control' name='description' id='description'></textarea> </div> </div> </div> </fieldset> </div> </form> <div class='modal-footer'> <div class='col-xs-12'> <button class='btn btn-success pull-primary app-action-insert' data-entity='<?php echo $entity ?>' data-action='insert' data-action-type='' data-value=''><i class='fa fa-save'></i> Save</button> <button style='margin-right:5px' class='btn btn-danger pull-right' data-dismiss='modal'><i class='fa fa-times'></i> Cancel</button> </div> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->
Código:
function insertEntity(entity, action, actionType, value) { // Formato: users-insert.php var file = entity + '-insert.php'; // Serializo los datos del formulario var data = $('#new-' + entity +'-form').serialize(); $.ajax({ method: 'POST', url: file, dataType: 'json', data: data, beforeSend: function () { //alert("Estoy en beforesend"); }, success: function (request) { $('#new-' + entity).modal('hide'); console.log(request); // Cargo el contenedor con los datos encontrados if (request.status == 'ok'){ var container = '.' + entity + '-table'; $(container + ' tbody').append(request.content); // Actualizo la lista poniendo el nuevo elemento al final } else { // Si se ha producido un error lo muestro } }, error: function (xhr, err) { // Notifico al usuario de que se ha borrado el registro correctamente var type ='error'; var message = "readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\n \n responseText: " + xhr.responseText; alert(message); }, complete: function (jqXHR, textStatus) { //alert("la funcion se completo con " + textStatus); } }); }
CODIGO PHP
Código PHP:
<?php
include_once('core/init.php');
$entity = 'users';
$status = '';
$message = '';
$content = '';
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$email = $_REQUEST['email'];
$fullname = $_REQUEST['fullname'];
$description = $_REQUEST['description'];
$status = $_REQUEST['status'];
$user = Users::create($username,$password,$email,$fullname,$description,$status);
if ($user == null)
{
$status = 'error';
$message = 'Se ha producido un error al insertar los datos';
}
else
{
$status = 'ok';
$message = 'Datos añadidos correctamente';
$content = "<tr>
<td>{$user->getId()}</td>
<td>{$user->getUserName()}</td>
<td>{$user->getEmail()}</td>
<td>{$user->getFullName()}</td>
<td>{$user->getStatus()}</td>
<td>{$user->getCreationDate()}</td>
<td>
<div class='pull-right'>
<div class='btn-group'>
<button data-id='{$user->getUserName()}' data-entity='{$entity}' type='button' class='btn btn-info action-edit'><i class='fa fa-pencil-square-o'></i> Edit</button>
<button data-id='{$user->getUserName()}' data-entity='{$entity}' type='button' class='btn btn-danger action-delete'><i class='fa fa-trash-o'></i> Remove</button>
</div>
</div>
</td>
</tr>";
}
$salidaJSON = array ("status" => $status,
"message" => $message,
"content" => $content);
echo json_encode($salidaJSON);
?>