Suppose i have many values here with form method POST
$_POST["value1"]
$_POST["value2"]
$_POST["value3"]
$_POST["value4"]
$_POST["value5"]
$_POST["value6"]
$_POST["value7"]
and i want to send them to nextpage.php
any function to do that? Besides using
<form method="POST" action="nextpage.php">
<input type="hidden" name="value1" value="value1 />
</form>
Passing without session
If there is no security concern and your post data contains something like search parameters . For example $_POST has
array('query'=>'keyword', 'orderby' => 'name', 'range' => '4-10' )
You can generate a query string from that data using http_build_query and create anchor tag for user to click and pass on that data to next page along with url.
$url = 'nextpage.php?' . http_build_query($_POST);
it will generate a url like nextpage.php?query=keyword&orderby=name&range=4-10 that you can use in html anchor tag and in next page you can get it from $_GET.
Using session
Alternatively you already have the option you storing it in $_SESSION and after using destroy the session in order to keep your site performance up.
store all your values in $_SESSION and use it in next page, or you can create URL using these values and redirect your page to nextpage.php
For passing post values to next page store the complete $_POST superglobal array variable into session and then on next page you can access those values using $_SESSION variable
Alternatively you can use curl to send HTTP request to next page using POST method
Then those variables will be accessible using $_POST variable on next page
Please refer the code snippet mentioned below as an example for sending HTTP request using post method through curl
$url='http://203.114.240.77/paynetz/epi/fts';
$data = array('login' => '11','pass' => 'Test#123','ttype' =>'NBFundTransfer','prodid'=>'NSE','amt'=>50,'txncurr'=>'INR','txnscamt'=>0,'clientcode'=>007,'txnid'=>uniqid(),'date'=>date('d/m/Y H:i:s'),'custacc'=>'123456789');
$datastring = http_build_query($data);
//die($url.'?'.$datastring);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);
$output = curl_exec($ch);
//echo $output; die;
curl_close($ch);
you can use Session or cookie to access to other page
Use this code.
<!DOCTYPE HTML>
<html>
<head>
<title>First page</title>
</head>
<body onload="document.getElementById('send').submit()">
<form id="send" action="next_page.php" style="display: none;">
<?PHP
foreach($_POST as $key => $val)
{
echo '<input type="hidden" name="'.$key.'" value="'.$val.'" />';
}
?>
</form>
</body>
</html>
Related
I am new to JSON data transfer. I want to make a user click on a link in a webpage and that should redirect the user to another page with his login credentials in the url and display it there. Now this all I want to send and receive through JSON . I am working on PHP environment. I am adding a short code on which I am working but not knowing how to proceed exactly.
send.php
<?php
$data = '{ "user" : [
{ "email" : "xyz#gmail.com",
"password" : "xyz#123",
"employee_id" : 77
}
]
} ';
$url_send ="http://localhost/cwmsbi/recieve.php";
$str_data = json_encode($data);
function sendPostData($url_send, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post))
);
$result = curl_exec($ch);
curl_close($ch); // Seems like good practice
return $result;
}
echo " " . sendPostData($url_send, $str_data);
?>
And receive.php
<?php
$json_input_data=json_decode(file_get_contents('php://input'),TRUE);
print_r( $json_input_data);
?>
Now when I am running send.php on my localhost, it displays the data on same page but does not goes to recieve.php.
How this can be achieved? I am curious and in need of this too. How can I run a JSON file and where should i obtain results? Your guidance will be immensely useful to me right now.
First of all i see you are json encoding $data two times (as when it gets defines it is already a json string and then you do $str_data = json_encode($data);).
If you want to achive the change of location with post data too, you can't use curl
(POST data and redirect user by PHP CURL - read this question for further infos) - and i don't think you can do it by php only.
If i was trying to achive what you're trying to achive (and i would never make a page to show login password to users - as it is bad practice to show a password, even in emails), i suggest to set the json string into $_SESSION variable in send.php and redirect with header("Location: http://localhost/cwmsbi/recieve.php") where you get the json data from $_SESSION variable and you print it.
I did not make an example as i think this one perfectly suites you:
https://stackoverflow.com/a/42215249/9606459
Extra hint: even if placing the password in php $_SESSION variable is better than put it in post request, remember you are doing bad practice and at least remember to empty out that json string in $_SESSION variable after you print it.
e.g.:
unset($_SESSION['user_data']);
I have to create an simple form:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML page</title>
</head>
<body>
<form method="post" action="process.php">
<input type="text" name="firstname" placeholder="rahul_sharma">
<button type="submit">send</button>
</form>
</body>
</html>
From my process.php file, I have to hit an url like below:
https://stackoverflow.com/api?rahul_sharma
which will give back an json response
{"status":"Success","username":"your username is RAHULSHARMA"}
If status is success, have to display the username value.
New to php.Any help is appreciated.
you can call API using curl.
$url='https://stackoverflow.com/api?';
$call_url = $url . $_POST['first_name'] ;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $call_url,
CURLOPT_SSL_VERIFYPEER => false,
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;//{"status":"Success","username":"your username is RAHULSHARMA"}
In your process.php file, you can use the $_POST superglobal to fetch the form data.
$firstname = $_POST['firstname'];
After that you can concatenate it using the . operator to form the url.
$url = "https://your-api-site.com/api?" . $firstname;
Next you can fetch the content from the url using a curl request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
The result from fetching the url will be obtained in the $output variable. Suppose the result from your API is a JSON string like the one you provided, you can use json_decode PHP function to convert it to an associative array.
$result = json_decode($output);
Now you can use if conditions to check if status is Success and display the username.
if ($result['status'] == "Success") {
echo $result['username'];
}
I have the following code:
<?php
define('ENVIRONMENT', 'tests');
$_POST['id']='AccountPagesView.a_book/45';
$_POST['old_value']='1';
$_POST['value']='2';
header("Location: http://localhost/index.php/welcome/update_record");
?>
I need to set $_POST array in this script and load script by url. But the script from url tells me that $_POST array is null. Why? How can I set the $_POST array and send it to script by url? Thank you in advance.
UPDATE:
I have some code which must be tested, and there is some script on the url "http://localhost/index.php/welcome/update_record", and it uses values from $_POST array; so, I can't change this script, and I want to test it. How can I do it?
UPDATE2:
<?php
//include ('\application\controllers\welcome.php');
define('ENVIRONMENT', 'tests');
$_POST_DATA=array();
$_POST_DATA['id']='AccountPagesView.a_book/45';
$_POST_DATA['old_value']='1';
$_POST_DATA['value']='2';
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/index.php/welcome/update_record');
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST_DATA);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_exec($ch);
?>
You cannot. A redirect will always result in the target page loaded via GET.
However, you could use the session to store these values. Call session_start(); on both pages and use the superglobal array $_SESSION instead of $_POST.
I believe this is what you need to send POST values from one PHP script to another, without using JS, If you absolutely don't want to use $_SESSION though that is what you should be using.
$ch = curl_init();
$data = array('id' => 'AccountPagesView.a_book/45', 'old_value' => '1', 'value' => '2',);
curl_setopt($ch, CURLOPT_URL, 'http://path-to/other.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
$_POST only exists if the request was sent through the POST method, which means a form was sent.
You could use $_SESSION instead.
This is one of the prime reason why $_SESSION's should be used. See this other question for an explanation: PHP - Pass POST variables with header()?
<?php
session_start();
define('ENVIRONMENT', 'tests');
$_SESSION['id']='AccountPagesView.a_book/45';
$_SESSION['old_value']='1';
$_SESSION['value']='2';
header("Location: http://localhost/index.php/welcome/update_record");
Then on index.php/welcome/update_record
<?php
session_start();
define('ENVIRONMENT', 'tests');
$id = $_SESSION['id'];
$old_value = $_SESSION['old_value'];
$value = $_SESSION['value'];
//do something
Two answers.
If the following does not work:
<?php
define('ENVIRONMENT', 'tests');
$_POST['id']='AccountPagesView.a_book/45';
$_POST['old_value']='1';
$_POST['value']='2';
require("/index.php/welcome/update_record");
?>
(I am a bit flabbergasted about the page URL.)
Then:
As you insist on POST (as is your right in asking), you can do:
<html>
<head>
</head>
<body>
<form action="/index.php/welcome/update_record" method="post">
<input type="hidden" name="id" value="AccountPagesView.a_book/45">
<input type="hidden" name="old_value" value="1">
<input type="hidden" name="value" value="2">
<input type="hidden" name="ENVIRONMENT" value="tests">
</form>
<script type="text/javascript">
document.forms[0].submit();
</script>
</body>
</html>
Where that define of ENVIRONMENT needs to be solved somehow.
If the target script uses $_REQUEST which is $_POST + $_GET (i.o. $_POST) then you do a HTTP GET URL: ...-?id=...&old_value=1&value=2 which would be the simplest solution.
i have no idea how to solve a problem with sending $_POST. I want to fill a form at example.com
//at example.com
<form action="foo.php" method="post" >
<input name="bar1" type="text" />
<input name="bar2" type="text" />
<input name="bar3" type="text" />
<input value="Send" type="submit" />
</form>
and then it goes to foo.php :
<?php //foo.php
echo 'added: <p>'.$_POST['bar1'].'<br />'.$_POST['bar2'].'<br />'.$_POST['bar3'];
?>
and in the same time it also send
$_POST['bar1'], $_POST['bar2'], $_POST['bar3']
to exampledomain.com/foobar.php where it can be saved to a file - that's not a problem.
I don't know how to send info to both php scripts at once - one is external one. I guess i have to send it somehow inside foo.php
There is kind of solution - redirecting to exampledomain.com/foobar.php inside foo.php but it isn't acceptable in my case - I want to do it without making user exit example.com
Thanks in advance and hope you can undestand my problem - if not just ask a comment
EDIT: Based on Pete Herbert Penito's answer:
<?php //inside foo.php
$url = 'http://exampledomain.com/foobar.php';
$fields_string='';
foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
I would use CURL to construct a post request:
<?php
// these variables would need to be changed to be your variables
// alternatively you could send the entire post constructed using a foreach
if(isset($_POST['Name'])) $Name = $_POST['Name'];
if(isset($_POST['Email'])) $Email = $_POST['Email'];
if(isset($_POST['Message'])) $Message= htmlentities($_POST['Message']);
$Curl_Session = curl_init('http://www.site.com/cgi-bin/waiting.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
?>
From Link:
http://www.askapache.com/php/sending-post-form-data-php-curl.html
In your foo.php:
<?php
include 'http://exampledomain.com/foobar.php';
Note: you need to enable allow_url_fopen in your php.ini file.
You will need to do one of the POST's with javascript ajax.
Then the real post which will redirect the browser like normal.
http://www.w3schools.com/jquery/ajax_post.asp
$(selector).post(url,data,success(response,status,xhr),dataType)
I have a submit form with method POST, I want to write a script that can automatically submit this form, the reason why I need this is for testing purposes. I need a lot of data in little time in order to test a search based on those form fields, and I do not have time to mannulally do this. Is this possible?
You can use curl to simulate form submit.
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/script.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true ); //enable POST method
// prepare POST data
$post_data = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3');
// pass the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data );
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Source:http://php.net/manual/en/book.curl.php
if your not comfortable using curl you could use a php library called snoopy that simulates a web browser. It automates the task of retrieving web page content and posting forms.
<?php
/* load the snoopy class and initialize the object */
require('../includes/Snoopy.class.php');
$snoopy = new Snoopy();
/* set some values */
$p_data['color'] = 'Red';
$p_data['fruit'] = 'apple';
$snoopy->cookies['vegetable'] = 'carrot';
$snoopy->cookies['something'] = 'value';
/* submit the data and get the result */
$snoopy->submit('http://phpstarter.net/samples/118/data_dump.php', $p_data);
/* output the results */
echo '<pre>' . htmlspecialchars($snoopy->results) . '</pre>';
?>
Let PHP fill the form with data and print out a Javascript that posts the form, PHP can not post it on it's own thou.
You can use php.net/curl to send POST requests with PHP.