Php $\u POST['variable']不起作用

Php $\u POST['variable']不起作用,php,jquery,post,http-post,Php,Jquery,Post,Http Post,我有一个html页面,其中包含一些select和textarea,没有被标记包围,还有一个运行以下脚本的按钮: function sendReservationData(){ var partenzaSel = $( "#selPartenza" ).val(); var partenzaTArea = $( "#textP" ).val(); var destinazioneSel = $( "#selDestinazione" ).val(); var des

我有一个html页面,其中包含一些select和textarea,没有被标记包围,还有一个运行以下脚本的按钮:

function sendReservationData(){
    var partenzaSel = $( "#selPartenza" ).val();
    var partenzaTArea = $( "#textP" ).val();
    var destinazioneSel = $( "#selDestinazione" ).val();
    var destinazioneTArea = $( "#textD" ).val();
    var nPasseggeri = $( "#selPasseggeri" ).val();

    var baseURL = window.location.origin;
    var serviceURL = baseURL + window.location.pathname;    

    $.post(serviceURL, {'partenzaSel' : partenzaSel, 'partenzaTArea' : partenzaTArea, 'destinazioneSel' : destinazioneSel, 'destinazioneTArea' : destinazioneTArea, 'nPasseggeri' : nPasseggeri},
         function(response){
            location.reload();
         }   
    ); } 
我使用这种方式来构建url,因为我正在使用xamp,当我完成时,我必须将代码上传到univerity服务器中,在该服务器中,我不知道网站的完整路径或正确url。 当我点击按钮时,我想使用我与脚本一起发送的变量,我向您展示了启动事务的脚本,并对数据库进行了一些查询,以便查看我是否能够在同一页面中,在按钮之后,从select和textarea收集数据,我编写了以下代码,这些代码将与数据库交互完成

 <?php
$Partenza = $partenzaSel = $partenzaTArea = $destinazioneSel = $destinazioneTArea = $nPasseggeri = "";
if (isset($_POST['partenzaSel']) and isset($_POST['partenzaTArea']) and isset($_POST['destinazioneSel']) and isset($_POST['destinazioneTArea']) and isset($_POST['nPasseggeri']) ){

     $partenzaSel = $_POST['partenzaSel'];
     $partenzaTArea = $_POST['partenzaTArea'];
     $destinazioneSel = $_POST['destinazioneSel'];
     $destinazioneTArea = $_POST['destinazioneTArea'];
     $nPasseggeri = $_POST['nPasseggeri'];

     echo $partenzaSel + $partenzaTArea + $destinazioneSel + $destinazioneTArea + $nPasseggeri + "prova";
}                                                   
?>
echo向我显示了任何内容,因此我尝试获取保存在javascript中的变量的操作无效,我无法使用这些值继续工作。我怎么了?我该怎么办?谢谢您的帮助。

请从javascript中删除location.reload并将php更改为以下内容

<?php
$Partenza = $partenzaSel = $partenzaTArea = $destinazioneSel = $destinazioneTArea = $nPasseggeri = "";
if (isset($_POST['partenzaSel']) and isset($_POST['partenzaTArea']) and isset($_POST['destinazioneSel']) and isset($_POST['destinazioneTArea']) and isset($_POST['nPasseggeri']) ){

     $partenzaSel = $_POST['partenzaSel'];
     $partenzaTArea = $_POST['partenzaTArea'];
     $destinazioneSel = $_POST['destinazioneSel'];
     $destinazioneTArea = $_POST['destinazioneTArea'];
     $nPasseggeri = $_POST['nPasseggeri'];

     echo $partenzaSel . $partenzaTArea . $destinazioneSel . $destinazioneTArea . $nPasseggeri . "prova";
     exit;
}                                                   
?>

现在,如果脚本接收到数据,它应该会带着输出退出脚本。

您的问题是,您希望PHP服务器端编码在页面加载后处理一些事情,当您重新加载页面时,就像什么都没有发送到页面一样,要么将其作为表单发送,要么使用JQuery处理PHP响应并在页面上显示。 JQuery在浏览器端是活动的,只要页面打开,PHP就会在页面从web服务器发送到浏览器之前进行处理

让php端:

`print json_encode($partenzaSel . $partenzaTArea . $destinazioneSel . $destinazioneTArea . $nPasseggeri . "prova");`
然后使用前面的jquery:

$.post(serviceURL, {'partenzaSel' : partenzaSel, 'partenzaTArea' : partenzaTArea, 'destinazioneSel' : destinazioneSel, 'destinazioneTArea' : destinazioneTArea, 'nPasseggeri' : nPasseggeri},
     function(response){
        data = JSON.parse(response);
        $("#responseDiv").text(data);
     }   
); }
并在网页上用id responseDiv创建一个div,这样它就可以将文本放入该框中

我试了一下:

由于没有提供表单代码,所以我只是用javascript替换了测试数据

使用+而不是时出现问题的原因。在最终echo的php代码中,由于这个echo响应,0和我认为由于javascript后回调不起作用,它会将其视为失败或错误

是的,@yunus saha是正确的,即使我不明白如何使用这个代码location.reload。您可以使用console.log或jquery的append或text函数

HTML

PHP


由于location.reload将从服务器上重新加载/刷新页面,因此您不会看到回显值开始执行var\u dump$\u POST;看看是否有任何变量通过,它们的值是什么。虽然我觉得因为您正在重新加载位置,所以实际上并不是在提交表单。您将信息发送到脚本,并对其进行处理,然后刷新页面。如果你想让它把变量写到一个文本文件中,我肯定你会发现它们正在被替换掉location.reload with console.logresponse一看浏览器调试器日志在PHP中,连接器是一个。点而不是+加号!我想看看控制台。它向我展示了我的源代码加上回声的响应,还有我想要的值。我不知道为什么页面不打印回显,但在我的最终代码中,我不需要它。我只希望变量的值与数据库交互,而且似乎在单击后变量不是临时的,所以我可以尝试继续我的工作,看看我将编写的查询是否有效。不知道这将如何改进任何东西它不会改进任何东西你在哪里看到你的php输出?请记住,您正在发送ajax请求,或者使用开发人员工具,或者使用console.log记录响应以查看输出。我已经使用了console.log。回显不起作用,但变量已保存。我不需要echo,这只是一次尝试,所以我尝试在数据库中使用这些变量,以查看它是否像我需要的那样工作。
<html>
   <body>   
     <form method='post' >

     <input type='submit' value='submit' onclick='sendReservationData();' />
     </form>

   <script type='text/javascript'  src="https://code.jquery.com/jquery-3.3.1.min.js"> 
    </script>
   <script type='text/javascript' >
        function sendReservationData(){
            var partenzaSel = 'partenzaSel';
            var partenzaTArea = 'partenzaTArea';
            var destinazioneSel = 'destinazioneSel';
            var destinazioneTArea = 'destinazioneTArea';
            var nPasseggeri = 'nPasseggeri';

           var baseURL = window.location.origin;
           var serviceURL = baseURL + window.location.pathname;    

       $.post('php3.php',{"partenzaSel" : partenzaSel, "partenzaTArea" : partenzaTArea, "destinazioneSel" : destinazioneSel,"destinazioneTArea" : destinazioneTArea, "nPasseggeri" : nPasseggeri}, function (data){
        console.log(data);
        });
       }
      </script>
    </body>
  </html>
<?php
     $Partenza = $partenzaSel = $partenzaTArea = $destinazioneSel = 
         $destinazioneTArea = $nPasseggeri = "";
      if (isset($_POST['partenzaSel']) and isset($_POST['partenzaTArea']) and 
        isset($_POST['destinazioneSel']) and isset($_POST['destinazioneTArea']) and 
        isset($_POST['nPasseggeri']) ){
       $partenzaSel = $_POST['partenzaSel'];
       $partenzaTArea = $_POST['partenzaTArea'];
       $destinazioneSel = $_POST['destinazioneSel'];
       $destinazioneTArea = $_POST['destinazioneTArea'];
       $nPasseggeri = $_POST['nPasseggeri'];
       echo $partenzaSel .'--'. $partenzaTArea  .'--'.  $destinazioneSel  .'--'.  
      $destinazioneTArea  .'--'.  $nPasseggeri  .'--'.  "prova";
  }                                                   
 ?>