繁体   English   中英

AJAX Post和JQuery序列化

[英]AJAX Post and JQuery serialize

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

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

假定属性[data]具有以下属性:
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;
    });

由于不是必需的,所以代码不完整

您应该使用serializeArray

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM