$_POST['variable'] doesn't work

php $_post array
php $_post empty
php post to url
$_get
php post without form
php post request
view post
_get, $_post _request in php

I have an html page with some select and textarea, which are not surrounded by <form> tag, and a button which run the following script:

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();
         }   
    ); } 

I used that way to build the url because I'm working on xamp and when I'll finish, I'll must upload my code in a univerity server in which I don't know the complete path or the right url of the website. When I click the button, I want to use the variables that I send with the script that I showed you for starting a transaction and do some query to the database so, in order to see if I'm able to collect the data from select and textarea, in the same page, after the button, I wrote the following code which will be completed with the interaction with the database.

 <?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";
}                                                   
?>

The echo shows me anything, so my attempt to take variables saved in javascript does not work and I'm not able to use that values to continue my work. What I'm wrong? How can I do? Thank you for help.

Your problem is that you are expecting PHP (server side coding) to handle something AFTER the page is loaded, when you reload the page it is like nothing is sent to it, either send it as a form or handle the PHP response with JQuery and display it on the page. JQuery is active on the browser side and functions as long as the page is open, PHP processes BEFORE the page is sent to the browser from the web server.

make the php side:

`print json_encode($partenzaSel . $partenzaTArea . $destinazioneSel . $destinazioneTArea . $nPasseggeri . "prova");`

Then with the front jquery:

$.post(serviceURL, {'partenzaSel' : partenzaSel, 'partenzaTArea' : partenzaTArea, 'destinazioneSel' : destinazioneSel, 'destinazioneTArea' : destinazioneTArea, 'nPasseggeri' : nPasseggeri},
     function(response){
        data = JSON.parse(response);
        $("#responseDiv").text(data);
     }   
); }

and make a div with the id "responseDiv" on the webpage so it can place the text in that box.

PHP Form Handling, php echo $_POST["email"]; ?> </body> </html>. The output could be something like this: Welcome John Your email address  PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in

Please remove location.reload() from your javascript and change your php to the following.

<?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;
}                                                   
?>

this should now exit the script with an output if it receives the data.

PHP POST, method is invisible to others and has no limits on the amount of information to send. The PHP superglobals $_GET and $_POST are used to collect form-data. The example below displays a simple HTML form with two input fields and a submit button: When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.

I have given a try:

As no form code is provided so i just replaced the test data in javascript.

Problem Reason as you are using + instead of . in php code for final echo so due to this echo response the 0 and i think due to that javascript post callback not working it treats that as failure or error

and yes @yunus-saha is right even i don't understand using of this code location.reload() . You can use either console.log or append or text function of jquery.

HTML

<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

<?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";
  }                                                   
 ?>

PHP GET and POST, which used to collect data from HTML form and URL. $_GET, $_POST, and $_REQUEST $_GET Variable What is it? The $_GET variable is used to get data from a form that is written in HTML. Also in the url $_GET variable will display the data that was taken by the $_GET variable.

PHP - GET & POST Methods, method can be used to send ASCII as well as binary data. The $_POST Variable. The $_POST variable is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form with method="post".

POST (HTTP), requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it. PHP: $_POST Description $_POST is a super global variable which is widely used to pass variables. This super global variable is widely used to handle form data. We will see an example where a particular php script is executed if after the form is submitted to another php script. Here is an example:

PHP: $_POST, $_POST. $HTTP_POST_VARS [deprecated]. (PHP 4 >= 4.1.0, PHP 5, PHP 7). $_​POST --  PHP $_POST The $_POST variable is a superglobal Array that contains data from a form sent with method="post". Information sent from a form with the POST method is invisible to the user and hasn't a specified limit on the amount of information to send.

Comments
  • As location.reload() will reload/refresh the page from the server it is not surprising that you do not see the echoed values
  • start off doing "var_dump($_POST);" and see which (if any) of the variables are getting through and what their values are. Although i feel that because you are reloading the location, thats not actually submitting the form. you are sending the information to the script, its handling it, and then you're refreshing the page. if you were to make it write the variables to a text file im sure you'll find they are getting there
  • Replace location.reload() with console.log(response) an look at the browser debugger log
  • Also in PHP the concatenator is a . dot and NOT a + plus sign!
  • I tried to see the console. It showed to me my source code plus the response of the echo and there are the values that I want. I don't know why the page doesn't print the echo, but in my final code I don't need it. I only want the values of the variables to interact with the database and it seems that the variables are not empy after the click, so I can try to continue my work and see if the queries that I will write will works.
  • Dont see how this will improve anything
  • it doesn't improve anything
  • where do you see your output of php? remember you are sending ajax request either you use a developer tool or you console.log the response to see the output.
  • I've used console.log. The echo doesn't work, but the variables are saved. I don't need the echo, it was just a try, so I'm trying to use these variables with the database in order to see if it works like I need.