我想将数据从HTML文件发布到php以及如何重定向到php并在php

时间:2017-08-21 09:40:43

标签: javascript php jquery json ajax

我想将数据从html文件发布到php文件,以及如何重定向到php文件并在控制台页面中查看数据。

我是初学者,在ajax方面不是很好。请查看我使用过的以下代码。

我想将json数据发送到php文件。我的问题是如何将json数据发布到php并查看php文件中的数据。我收到了错误,指出了国家未定义的索引。

我的另一个问题是如何解决错误。请帮我纠正错误。

谢谢。

在ajax.html中

    <html>
     <head>
     <title> NEW AJAX</title>
     <script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-
     1.11.3.min.js"></script>
     </head>
     <body>
      <h2>Show Data in PHP</h2>
      <br />
      <br />
      <form>
       <input type="hidden" id="country" value="singapore" readonly>
       <input type="hidden" id="time" value="141253" readonly>
       <input type="button" value="submit me" onlcick="showData();">
      </form>


    <div id="resulte"></div>
    <h3>Look at the console. Click Ctrl + Shift + J to VIEW THE CONSOLE PAGE.
    </H3>
    <script type="text/javascript">
     var country = document.getElementById("country");
     var time = document.getElementById("time");
     function showData() {
       $.ajax({
         type: "POST",
         dataType:  "json",
         url: "abdullahpass.php",
        //async: false,
         data: JSON.stringfy({postcountry: country,
         posttime: time}),
         success: function(data) {
            console.log(data);  
         },
         error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... Look at the console (F12 or 
            Ctrl+Shift+I, Console tab) for more information!');
            $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p>
            <p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p>
            <div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);
          },
       });
    }
    </script>
    </body>
    </html>

在另一个档案中

      <?php
         echo json_decode($_POST['posttime']);
         echo json_decode($_POST['postcountry']);
         echo var_dump($_POST);
      ?>

1 个答案:

答案 0 :(得分:0)

//在您的ajax电话data中,无需strigyfy数据。

<script type="text/javascript">
         var country = document.getElementById("country");
         var time = document.getElementById("time");
         function showData() {
           $.ajax({
             type: "POST",
             dataType:  "json",
             url: "abdullahpass.php",
            //async: false,
             data: {postcountry: country,posttime: time},
             success: function(data) {
                console.log(data);  
             },
             error: function(jqXHR, textStatus, errorThrown) {
                alert('An error occurred... Look at the console (F12 or 
                Ctrl+Shift+I, Console tab) for more information!');
                $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p>
                <p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p>
                <div>'+jqXHR.responseText + '</div>');
                console.log('jqXHR:');
                console.log(jqXHR);
                console.log('textStatus:');
                console.log(textStatus);
                console.log('errorThrown:');
                console.log(errorThrown);
              },
           });
        }
        </script>

我收到错误,指出未定义的索引postcountry 不是错误它是一个警告,使用isSet函数你可以克服相同的和$ _POST ['posttime']和$ _POST [' postcountry']是价值观。您应该仅在数组上使用json_encode

    <?php

         echo (isSet($_POST['posttime']) && $_POST['posttime']) ? $_POST['posttime'] : 'No value for posttime';
         echo (isSet($_POST['postcountry']) && $_POST['postcountry']) ? $_POST['postcountry'] : 'No value for postcountry';


         //this will print total post data with type
         echo var_dump($_POST);
      ?>
相关问题