drze7794 2016-03-09 20:59
浏览 87
已采纳

序列化表单并使用POST方法使用ajax发送序列化数据

I'm trying to serialize a form and send the serialized data with ajax using POST method ..

index.php

<form id ="form" class = "form">
        <input type = "text" name = "name" />           
        <input type = "number" name = "age" />
        <input type = "number" name = "id" />
        <input type = "submit" name = "submit"><br/>
</form>
<p id = "result"></p>

Jquery snippets

<script>
    $(document).ready(function(){
        $("#form").submit(function(){
            var data = $("#form").serialize();
            insertStudent(data);
            return false ;
        });
        function insertStudent(data){
            $.post("process.php" , { data : data} , function(str){
            $("#result").html(str);
            });                 
        }
   });
</script>

process.php

$ret = $_POST["data"];
echo "<br />".$ret["name"];

And now , the result is :

Notice: Undefined index: name in C:\xampp\htdocs\try.php on line 3

When i tried to echo $_POST["data"] the result is :

name=Ahmed&age=111&id=222

how can i use every name individually such as : $_POST["name"] ... $_POST["age"] ... $_POST["id"] ?

  • 写回答

2条回答 默认 最新

  • doushao1948 2016-03-09 21:15
    关注
        function insertStudent(data){
            $.ajax({
                url: 'process.php',
                data: data,
                type: 'POST',
                dataType: 'json',
                success: function(str){
                     $("#result").html(str);
                }
            });     
        }
    

    And then print $_POST in your PHP file :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条