dpea85385 2016-03-26 23:57
浏览 53

AJAX:如何在另一个页面上显示数据

I have the following function below. When a button is clicked, the function below is executed. The function basically sends json data to the page "temp.php", via Ajax

I think the sending of data is successfull. My main problem is printing/displaying the data sent by ajax to the "temp.php" page.

    **Orders.php:**

    function convertToJson()
    {
        var tableData = $('#productOrder').tableToJSON({
            ignoreColumns: [0]
        }); 
        var result = JSON.stringify(tableData)
        $.ajax({
            url: "http://localhost/app/temp.php",
            data: {result},

            type: "POST",
        })
          .done(function( json ) {
                document.getElementById('testDiv').innerHTML = result;
          })

          .fail(function( xhr, status, errorThrown ) {
            alert( "Sorry, there was a problem!" );
            alert( "Error: " + errorThrown );
            alert( "Status: " + status );
            alert( xhr );
          })

          .always(function( xhr, status ) {
            alert( "The request is complete!", status );
          });           
    }

   **This is the content of "temp.php"**
   <?php
   $Id = json_decode($_POST['Id']);
   $Name = json_decode($_POST['Product Name']);
   $Unit = json_decode($_POST['Unit']);

   echo "<h1>$Id</h1>";
   ?>

   **This is the json data (var result) that is being sent by ajax**


   [{"Id":"43","Product Name":"Orchid","Unit":"Test","Rate(PHP)":"600.00","Quantity":"5.00","Sub-Total (PHP)":"3,000.00"}]
  • 写回答

1条回答 默认 最新

  • dongtanlin0765 2016-03-27 09:47
    关注

    Look at the data you are sending:

    data: {result},
    

    (which is shorthand for data: {result: result},)

    So your form encoded data has one key in it, result.

    Now look at the PHP:

    $_POST['Id']
    

    There is no Id in the data you are sending. Only result.

    $_POST['Product Name']
    

    There is no Product Name in the data you are sending. Only result.

    etc.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示