我如何在jquery中通过ajax发布一些数据,并从myxmlfeed.php中获取所有json编码数据

时间:2014-11-07 07:06:37

标签: php jquery ajax json

如何通过jquery发布一些数据并从myxmlfeed.php中获取所有json编码数据, 我从某个网站获得此代码... 在这里,他们将一些数据发布到myxmlfeed.php并处理它,在json编码后,他们正在打印数据我还要打印id,title,date_start,date_end

<head>
           <title>jQuery Test</title>
          <script src="js/jquery-1.11.1.min.js"></script>
         <script type="text/javascript">
         $(document).ready(function() {

                $("#submit").click(function(){

                $.ajax({
                url: "myxmlfeed.php",
                type: "POST",
                data: {
                    amount: $("#amount").val(),
                    firstName: $("#firstName").val(),
                    lastName: $("#lastName").val(),
                    email: $("#email").val()
                },
                dataType: "JSON",
                success: function (jsonStr) {
                    $("#result").text(JSON.stringify(jsonStr));
                }
            });

        }); 

        });

        </script>
        </head>
        <body>
        <div id="result"></div>
            <form name="contact" id="contact" method="post">
             Amount : <input type="text" name="amount" id="amount"/><br/>
             firstName : <input type="text" name="firstName" id="firstName"/><br/>
             lastName : <input type="text" name="lastName" id="lastName"/><br/>
             email : <input type="text" name="email" id="email"/><br/>
            <input type="button" value="Get It!" name="submit" id="submit"/>
            </form>

        </body>

myxmlfeed.php

<?php
    $amount      = $_POST["amount"];
    $firstName   = $_POST["firstName"];
    $lastName    = $_POST["lastName"];
    $email       = $_POST["email"];
   if(isset($amount)){
        $data = array(

            "amount"     => $amount,
            "firstName"  => $firstName,
            "lastName"   => $lastName,
            "email"      => $email
        );
        echo json_encode($data);
    }
?>

<?php
$age=array("id"=>"3","title"=>"first entry","date_start"=>"2014-11-22","date_end"=>"2014-11-30");

echo json_encode($age);


?>

1 个答案:

答案 0 :(得分:1)

尝试这样并告诉我它是如何为你工作的:

<?php
    $amount      = $_POST["amount"];
    $firstName   = $_POST["firstName"];
    $lastName    = $_POST["lastName"];
    $email       = $_POST["email"];
   if(isset($amount)){
        $age=array("id"=>"3","title"=>"first entry","date_start"=>"2014-11-22","date_end"=>"2014-11-30");
        $data = array(
            "amount"     => $amount,
            "firstName"  => $firstName,
            "lastName"   => $lastName,
            "email"      => $email
        );
        echo json_encode(array_merge($data, $age));
    }
?>

如果这是您需要的,请不要忘记接受答案!