简体   繁体   中英

how can i post some data's by ajax in jquery and get all the json encode data's from the myxmlfeed.php

how can i post some data's by jquery and get all the json encode data's from the myxmlfeed.php, i got this code from some site... in this they are post some data to the myxmlfeed.php and process it and after json encode they are printing the data i also want to print the 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);


?>

Try like that and tell me how it is working for you:

<?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));
    }
?>

Do not forget to accept the answer if this is what you need!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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