簡體   English   中英

我想將數據從HTML文件發布到php,以及如何重定向到php並在php中顯示數據

[英]I want to post data from HTML file to php and how to redirect to the php and show data in php

我想將數據從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);
      ?>

//在您的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);
      ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM