AJAX Post和JQuery序列化

时间:2015-11-30 11:53:24

标签: javascript php jquery ajax

我的JS代码包含: var data = $('#idSelected').serialize();方法发送的$.post(); 现在,当我在PHP中收到它时,它看起来像这样:

[data] => id=&nombre=Alejandro&apellido1=&apellido2=&direccion=&codigoPostal=&fechaNacimiento=¬as=

属性[数据]具有以下特征:
id,nombre,apellido1,apellido2,direccion,codigoPostal,fechaNacimiento,notas,fechaRegistro

第一个问题:“¬as”属性是什么意思? 第二个问题:我如何unserialize()数据或访问$_POST['data']

中的属性

编辑:请求的表单,.tpl和js代码:

    public function pintarFormulario($elemSel = null) {
    $tpl = file_get_contents ( 'templates/form.tpl', true );
    $vacio = "";
    if ($elemSel == null) {
        // ALTA
        $tpl = str_replace ( "{{ID}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{NOMBRE}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{APELLIDO1}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{APELLIDO2}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{DIRECCION}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{CODIGOPOSTAL}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{FECHANACIMIENTO}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{NOTAS}}", "$vacio", $tpl );
        $tpl = str_replace ( "{{SUBMIT}}", "Alta", $tpl );
        $tpl = str_replace ( "{{SUBMIT2}}", "alta", $tpl );
    } else {
        // MODIFICACION
        $tpl = str_replace ( "{{ID}}", $elemSel [0]->id, $tpl );
        $tpl = str_replace ( "{{NOMBRE}}", $elemSel [0]->nombre, $tpl );
        $tpl = str_replace ( "{{APELLIDO1}}", $elemSel [0]->apellido1, $tpl );
        $tpl = str_replace ( "{{APELLIDO2}}", $elemSel [0]->apellido2, $tpl );
        $tpl = str_replace ( "{{DIRECCION}}", $elemSel [0]->direccion, $tpl );
        $tpl = str_replace ( "{{CODIGOPOSTAL}}", $elemSel [0]->codigoPostal, $tpl );
        $tpl = str_replace ( "{{FECHANACIMIENTO}}", $elemSel [0]->fechaNacimiento, $tpl );
        $tpl = str_replace ( "{{NOTAS}}", $elemSel [0]->notas, $tpl );
        $tpl = str_replace ( "{{SUBMIT}}", "Modificacion", $tpl );
        $tpl = str_replace ( "{{SUBMIT2}}", "modificacion", $tpl );
    }
    return $tpl;
}

TPL在这里:

<form id="envioDatos" name="envioDatos" method="post">
<div name="div1" id="div1">
    <input type="hidden" id="id" name="id" value="{{ID}}" />
  <label for="nombre">Nombre</label>
    <input placeholder="Nombre" type="text" name="nombre" id="nombre" value="{{NOMBRE}}" required/>
  <label for="apellido1">Apellido1</label>
    <input placeholder="Apellido1" type="text" name="apellido1" id="apellido1" value="{{APELLIDO1}}" required/>
</div>
<div name="div2" id="div2>
  <label for="apellido2">Apellido2</label>
    <input placeholder="Apellido2" type="text" name="apellido2" value="{{APELLIDO2}}" id="apellido2">
  <label for="direccion">Direccion</label>
    <input placeholder="dirección" type="text" name="direccion" id="direccion" value="{{DIRECCION}}" required/>
</div>  
<div name="div3" id="div3>
  <label for="codigoPostal">CodigoPostal</label>
    <input type="number" min="10000" max="99999" placeholder="CodigoPostal" type="text" name="codigoPostal" value="{{CODIGOPOSTAL}}" id="codigoPostal">
  <label for="fechaNacimiento">FechaNacimiento</label>
    <input type="date" placeholder="AAAA/MM/DD" type="text" name="fechaNacimiento" id="fechaNacimiento" value="{{FECHANACIMIENTO}}" required/>
</div>
<div name="div4" id="div4>
  <label for="notas">Notas</label>
    <textarea placeholder="Introduzca si lo desea alguna nota" name="notas" id="notas" rows="4" cols="50">{{NOTAS}}</textarea>
    <br>
  <input id="submit" name="{{SUBMIT2}}" type="button" value="{{SUBMIT}}">
</div>
</form> 

<span id="result"></span>

 <form id="id_selected" name="id_selected" method="post" style="background:none; border:none">
    <input type="hidden" id="idSelected" name="idSelected" value="" />
    <input type="hidden" id="eliminar" name="eliminar" value="" />
</form> 

我的JS代码:

$(document).ready(function(){
    //Clickar en cualquier lado del tr (menos el ultimo td) para actualizar ese registro
    $("#tablaDatos tr td:not(:last-child").click(function() {
        if (confirm("¿Seguro que desea modificar el registro?")){
            $("#idSelected").val($(this).closest('tr').attr('id'));
            var data = $('#idSelected').serializeArray(); 
            console.log(data);
            $.post(
                    'crud.php',
                    {data: data},
                    function(response){
                        $('#result').html(response);
                    }
            );
            console.log(data);
            return false;
        }else
            return false;
    });

不完整的代码,因为它不是必需的

1 个答案:

答案 0 :(得分:1)

您应该使用serializeArray

var data = $('#idSelected').serializeArray();
// Here data is not string id=...&nombre=.. but a object

$.post("page.php", data[0]);