Json从html代码输出

时间:2013-09-17 07:33:46

标签: javascript php jquery html json

   <form action:"" name="sample" method="post"/>
    Firstname:<input type="text" name="firstname" /><br>
     Lastname:<input type="text" name="lastname"/><br>
     DOB:<input id="datepicker" /><br>
    Email:<input type="text" name="email"/><br>
    Gender:
     <input type="radio" name="Male" value="Male"/> Male
   <input type="radio" name="Female" value="Female"/> Female<br>
    Address: <textarea name="address" rows="2" cols="20"></textarea><br>
      <input type="submit" value="Submit" />
        </form>

我需要像

这样的json输出
   {"Firstname":"","Lastname":"","Email":"","Address":""}

3 个答案:

答案 0 :(得分:1)

您可以按如下方式获得所需的JSON:

var obj = new Object();
obj.FirstName="first";
obj.LastName="Last";
obj.DOB="1/1/1990";
obj.Gender="Male";
obj.Address="Test Address";

alert(JSON.stringify(obj));

答案 1 :(得分:0)

<?php
//We have a post
if ($_POST) {
  $data = json_encode($_POST);
  var_dump($data);
}

答案 2 :(得分:0)

如果你想将它发布为json对象,那么你可以使用jQuery(参见第二个链接)。

How to post a complex HTML form as JSON?

Serializing to JSON in jQuery