简体   繁体   中英

Posting JSON data from one page to another using AJAX and jQuery

I have

1.) textarea

2.) select box

3.) Button

I pass JSON input in textarea, and onclicking the button json data is passed to another php page "sendingInterface.php" from where json data is passed through function to one more page called "index.php" where decoding of json and inserting into database takes place. The response of this is coming in the another textarea.

My html code:

<form id="data" action="" method="post" >

            <h3>Request</h3>
            <textarea name="jsondata" id="jsondata" value="">   </textarea>
            <br>
            <select name="listoptions" id="listoptions">
                <option selected="selected" value="">Select method</option>
                <option value="sendIncidentReport">sendIncidentReport</option>
                <option value="sendDeathReport">sendDeathReport</option>
</select>
            <br>
             <input type="button" name="send_report" id="send_report" value="Send Report">
             <br>
<h3>Response</h3>
         <br>
         <textarea name="response" id="response" rows="8" cols="8" value="">   </textarea>

<script>
    var url="http://localhost/API/sendingInterface.php?fn="
$(document).ready(function() {
        $("#send_report").click(function () {

        $.ajax({
        url:url + "sendIncidentReport", // calling url
        type: 'GET',
        data:{jsondata:$("#jsondata").val()         
        },
        cache: false,
        async: false,
        success: function(data) {
           $('#response').val(data);
        },
        error: function() {
        alert('Error');
        }
        });
});
}); 

$(document).ready(function() {
        $("#send_report").click(function () {

        $.ajax({
        url:url + "sendDeathReport", // calling url
        type: 'GET',
        data:{jsondata:$("#jsondata").val()         
        },
        cache: false,
        async: false,
        success: function(data) {
           $('#response').val(data);
        },
        error: function() {
        alert('Error');
        }
        });
});
});
</script>

PHP code:sendingInterface.php

    <?php
    include_once 'index.php';
    $obj=new send();

    /*sendIncidentReport*/
    if($_GET['fn'] =="sendIncidentReport")
    {  
         $json=file_get_contents("php://input");
         $obj->sendIncidentReport($json);
        //$jsondata=$_GET['jsondata'];
        //$obj->sendIncidentReport($jsondata);
    }
    /*sendDeathReport*/
    if($_GET['fn'] =="sendDeathReport")
    {
          $json=file_get_contents("php://input");
          $obj->sendDeathReport($json);
        //$jsondata=$_GET['jsondata'];
        //$obj->sendDeathReport($jsondata);
    }
    ?>

My commented PHP code is working properly. But I want to use file_get_contents("php://input") to get the json code.

file_get_contents("php://input"); will read raw data from request body only for POST requests. If you have planned to changed your requests from GET to POST then you can use the following code in jquery to pass data to be available on php://input stream.

$.ajax('url',{
'data': JSON.stringify(yourJSONObject), //{jsondata:$("#jsondata").val()}
'type': 'POST',
'processData': false,
});

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