dongpin1059 2017-11-12 19:28
浏览 66

从serialize()AJAX中获取PHP中的多个/多个POST变量的最佳方法是什么?

I have a form with many many inputs and I use the following code to send all my form data to php proccesing:

HTML:

<form>
<input type="text" name="a" />
<input type="text" name="b" />
<input type="text" name="c" />
<input type="text" name="d" />
<input type="text" name="e" />
<input type="text" name="f" />
<button type="button" onclick="update()">UPDATE</button>
</form>

<!-- HERE I SHOW THE VALUES FROM ABOVE INPUTS -->
<input type="text" id="a" />
<input type="text" id="b" />
<input type="text" id="c" />
<input type="text" id="d" />
<input type="text" id="e" />
<input type="text" id="f" />

JS:

<script>
function update()
{
    var str = $("form").serializeArray();
    $.ajax({  
        type: "POST",  
        url: "update.php",  
        data: str,  
        dataType: 'json',
        success: function(result) 
        {  
            document.getElementById("a").value = result.a;
            document.getElementById("b").value = result.b;
            document.getElementById("c").value = result.c;
            document.getElementById("d").value = result.d;
            document.getElementById("e").value = result.e;
            document.getElementById("f").value = result.f;
        },
        error: function(result){
            console.log("Error:");
            console.log(result);
        }
    });
    // return false;
}

</script>

AND update.php:

<?php
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$a = $_POST['d'];
$b = $_POST['e'];
$c = $_POST['f'];

$arr = array(
'a' => $a, 
'b' => $b, 
'c' => $c,
'a' => $d, 
'b' => $e, 
'c' => $f
);

echo json_encode($arr);

?>
  1. First question: Is there a simple method of passing all serialize() variables in PHP, instead of writing all of them like I did in my code? There are too many and I think is is not OK that I have to manually add all of them.

     $a = $_POST['a'];
     $b = $_POST['b'];
     $c = $_POST['c'];
     $d = $_POST['d'];
     $e = $_POST['e'];
     $f = $_POST['f'];
    
  2. Is there a short way to pass ajax result array values from PHP in multiple inputs, instead of using the following code?

document.getElementById("a").value = result.a;
 document.getElementById("b").value = result.b;
 document.getElementById("c").value = result.c;
 document.getElementById("d").value = result.d;
 document.getElementById("e").value = result.e;
 document.getElementById("f").value = result.f;

All I am trying to say that I have too many inputs on the form and I don't want to add those variables manually. It takes too much time and it is easy to mistake at getting id's and names.

  • 写回答

1条回答 默认 最新

  • douyan4900 2017-11-12 19:37
    关注

    You can pass your form inputs as array instead of individual name.. Meaning

    <input name='form[a]' type='text'/>
     <input name='form[b]' type='text'/>
     <input name='form[c]' type='text'/>
     <input name='form[d]' type='text'/>
    

    Then in php do this

    echo(json_encode($_POST['form']));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号