This question already has answers here:
How to use store and use session variables across pages?
(8 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
On my page, users have the option to select a client. When they select one, this selection will be set in $_SESSION['selectedClient']. A user may then choose to pick a different client which would have to overwrite the current $_SESSION['selectedClient']. So when I try to update $_SESSION['selectedClient'] it will show me that the session variable has updated. But when I reload the page it will show the previous/first selected client.
Here is the piece of code I currently have to try this:
if($_POST['clientEmail']){
$_SESSION['selectedClient'] = $_POST['clientEmail'];
echo '<script>document.location=\'\?p=home\';</script>';
}
The echo leads to the same PHP file in which I set session var and try to update the session var, which is needed in this case.
I've tried to unset $_SESSION['selectedClient'] before assigning a new post like:
if($_POST['clientEmail']){
unset($_SESSION['selectedClient']);
$_SESSION['selectedClient'] = $_POST['clientEmail'];
echo '<script>document.location=\'\?p=home\';</script>';
}
I haven't been able to find anything on google that could help me, which is why I decided to make a SO account and ask here. Feel free to ask if more information is needed.
EDIT:
When $_SESSION['selectedClient'] is set and I select a new client, the post contains the newly selected value/client. After refresh, post is empty and session is reverted back to the client which was selected before.
selecting new client:
_POST['clientEmail'] = >lol#lol.com<
_SESSION['selectedClient'] = >lol#lol.com<
after refresh:
_POST['clientEmail'] = ><
_SESSION['selectedClient'] = >login#login.com<
please make sure to use session_start in the beginning of file and use isset function to check if there is any POST data
session_start()
if(isset($_POST['clientEmail'])){
$_SESSION['selectedClient'] = $_POST['clientEmail'];
header("Location: .\?p=home");
exit(0);
}
Im working with so called webhooks. What basically happens is. There's a process happening in the background and when that process finishes it will send a POST request to an URL that I have to specify. For example 'www.bla/process.php'.
The post request that is sent will have a body of data. My question is , is it possible to read the data that is sent and just print it out for example?
Yes
It is possible to pass info from one page to another and print it out for example.
There are many method's...
// THE SAME
echo $_POST['DATA1'];
echo ($_POST['DATA1']);
// THE SAME
// IF = DATA IS SET AND NOT EMPTY
if (isset($_POST['DATA1']) && !empty($_POST['DATA1'])) {
$DATA1 = $_POST['DATA1'];
}
echo($DATA1);
I am trying some code to get value from URL through post method and search database table for that value and get info from the database and encode it into JSON response.
Here is my code :
<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","json") or die("Error " . mysqli_error($connection));
if (isset($_POST['empid'])) {
$k = $_POST['empid'];
//fetch table rows from mysql db
$sql = "select `salary` from tbl_employee where `employee_id` = $k ";
} else {
//fetch table rows from mysql db
$sql = "select `salary` from tbl_employee";
}
//fetch table rows from mysql db
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
?>
I used Postman extension on Chrome and pass the values but it is not returning. Instead it is returning the else part.
Postman Screenshot
Looking at your screen shot, you have not passed body key values, instead you passed params.
Click on Body Tab and then pass key & value pair.
As per your screenshot you are sending your empid through query parameter so you need to access that as follows
<?php
if (isset($_GET['empid'])) {
echo $_GET['empid'];
}else{
// else part
}
also for that you need to Request Url in Postman using GET method.
But as you have stated that you want to send empid through POST in postman, you have to send it through form-data in Postman and access it as $_POST["empid"];. following is the screenshot for your reference
else there is another option where you can send the POST data through body as row json and access it as
$rawPostData = file_get_contents('php://input');
$jsonData = json_decode($rawPostData);
and $post will contain the raw data. And you can send it through postman as in following screenshot.
You have to set the Body to "x-www-form-urlencoded" and adding the variables to be posted
Or try this SO question, its already been answered
I replicated the code and db on my system to figure out the problem. I also added some lines of code before if (isset($_POST['empid'])) { for diagnostics sake:
$method = $_SERVER['REQUEST_METHOD'];
echo $method."<br/>";
The application file is index.php deployed in json directory inside webroot.
When I send any request to http://localhost/json directory (either POST/GET), Apache redirects the request as a GET request to index.php (as configured in my Apache config file). I assume this is what you're experiencing.
But when I send the request to http://localhost/json/index.php, the request is accurately received and processed.
Therefore, I would say the solution is that you need to specify the php file and also set the empid parameter as part of the body in Postman (not as part of the url).
I think you should also check the post if emptyif (isset($_POST['empid']) AND ($_POST['empid']) != ""). to allow php to execute the line before else.Sometimes programming becomes unpredictable.
use if(isset($_REQUEST['empid'])) to test in POSTMAN...
Then use if(isset($_POST['empid'])) to test directly from app...
have a look Issue in POSTMAN https://github.com/postmanlabs/postman-app-support/issues/391
To get the value of a variable from the URL(query string), you need to use either $_GET or $_REQUEST.$_POST represents data that is sent to the script via the HTTP POST method.
So, in your code you just need to do this :
$_REQUEST['empid'] instead of $_POST['empid']
In POST method the data is sent to the server as a package in a separate communication with the processing script. Data sent through POST method will not visible in the URL.
Confirm that in postman Content-Type should be application/x-www-form-urlencoded in request header.
Postman reference doc : https://www.getpostman.com/docs/requests
Hey it sounds like you are just needing to do a GET request to your DB.
You are more than welcome to send in variables via a GET request as well.
GET http://localhost/json?empid=3
You can then get data from your GET request like so $_GET['empid']
I suggest a GET request because I see your not actually posting any data to your server, your just handing in a variable in which you want to use to query with.
I do understand that GET requests are less secure, but in your scenario your POST just doesn't seem to want to work. So a different tack might do you justice.
If you want a value from the URL, you need to use $_GET["empid"] instead $_POST["empid"]
Submitting a form through POST method
By post method of form submission, we can send number or length of data. Sensitive information like password does not get exposed in URL by POST method, so our login forms we should use POST method to submit data. This is how we collect data submitted by POST method in PHP
$id=$_POST['id'];
$password=$_POST['password'];
Collecting data submitted by either GET or POST method
If a page is receiving a data which can come in any one of the method GET or POST then how to collect it ? Here we are not sure how to collect the data. So we will use like this.
$id=$_REQUEST['id'];
$password=$_REQUEST['password'];
Looking at the URL you are requesting, you are sending a GET value within your POST request.
http://localhost/json?empid=3
As you can see here, the url holds the empid variable and so the is send to the server as beeing a GET variable ($_GET)
Use $_GET['empid'] to access this variable, while using $_POST to access the other variables.
You could also use $_REQUEST to access both GET and POST data by the same global.
This question already has answers here:
Reading JSON POST using PHP
(3 answers)
Closed 7 years ago.
I want to send json payload to my PHP webservice. I am testing on advance rest client. I already search similar answers. But it is not helping me.
I am sending this json to my php service which is running on localhost.
{
"name":"XXXX",
"password":"YYYYY"
}
I changed the content-type from dropdown to application/json.
However sending value as form parameter from advance rest client works fine.But this is not what i want.
My php code is
$name= isset($_POST['name']) ? mysql_real_escape_string($_POST['name']) : "";
$pass= isset($_POST['pass']) ? mysql_real_escape_string($_POST['pass']) : "";
echo $name;
echo $pass;
but it is always printing the blank value.
I am new to php but found this is the way to receive post params.
I found the solution if any one else facing same issue. To read the json data sent in the request body we can use this method
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
$name = isset($data->name) ? mysql_real_escape_string($data->name) : "";
echo $name
}
Now it will give the correct value.
This question already has answers here:
Using HTML5 file uploads with AJAX and jQuery
(2 answers)
Closed 9 years ago.
I'm building an application that have to add a row in a MyPHP database with some data, included for each row an always different, small JPEG image. At the moment it is structured like this:
JavaScript
function fn_addrow() {
document.write('<form action="ADVBKMNGR-EventClass_MANAGE.php?Mode=ADD" method="POST" enctype="multipart/form-data">')
document.write('<table width="500"><th width="150"></th><th width="350"></th>')
document.write('<TR>')
// Form building
document.write('<input type="submit" value="Ok proceed">')
document.write('</form>')
}
PHP
elseif($WorkMode == 'ADD'):
$ID_ev = $_POST[ID_evcls];
$ID_ds = $_POST[Desc];
// Write all the data in a new row
$query='INSERT INTO '.$db_tabscelta.' (`Cod_App`, `ID_eventclass`, `Descrizione`, `Active`, `Logo_Eve`) VALUES ("'.$Station_ID.'","'.strtoupper($ID_ev).'","'.$ID_ds.'","1","'.mysql_real_escape_string($datimmagine).'")';
$result = mysql_db_query("AdVisual_02_", $query ,$connessione);
unlink($ID_logo);
imagedestroy($ID_logo);
The call at the PHP side works, the record is added, but my problem is that at the end I remain on the PHP page, while I would like to return to the calling page and redisplay the whole table. And, the address of the PHP is displayed in the user screen, and I would NOT like this at all.
Does anybody have suggestions?
Very important: I've tried the way with the XMLHttpRequest() command. It's fine, but I'm not able to upload the image at all.
Store the value of
$_SERVER['HTTP_REFERER']
Once you land on page. This will give you the URL
After finishing the job redirect to this URL
I think this for what you searching for form that submits through ajax even file upload is present
http://www.malsup.com/jquery/form/