This question already has answers here:
how to remember input data in the forms even after refresh page?
(10 answers)
Closed 4 years ago.
So i got this form:
<form action="send.php" method="post">
Dogecoin-address: <input type="text" name="address"/><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
// starting the session
session_start();
if (isset($_POST['address'])) {
$_SESSION['address'] = $_POST['Submit'];
}
?>
i want to keep"address"data here: when i reload this:
$url = 'https://faucethub.io/api/v1/send?
api_key=4b21af7e916403216ffb11e523f912bc¤cy=DOGE&amount=1&to='.
$_POST['address'];
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
?>
when the user submit his "address" i want his "address"remain/save on the second code when he refreshes!
how is this possible by using $_SESSION?OR by using cookies?or any other way?
i'm new to php and i don't know how to use it
Change your $_POST value to address instead of Submit. This way your address is stored inside the $_SESSION['address'] variable.
You can access the address by using $_SESSION['address'].
Related
I want to read an external site after submit code with captcha. But using (PHP) file_get_contents the captcha is loaded 2 times, so its wrong.
The Form load an image to solve the captcha and sent via POST to the original site that show the result. Normally I solve the captcha and than, in the new site, I read (with my eyes) the content.
Now I want to let PHP read the content. I always solve first the captcha (in my page) and than send it to the other site and let (php) "file_get_contents" read the content.
HERE MY CODE..
<?php
$captcha = $_POST["captcha"];
?>
<html>
<form method="post" action="(this page)">
<img src="remote-captcha"><input type="text" name="captcha">
<submit>
<?php
$postdata = http_build_query(
array(
'captcha' => $captcha
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://www.form-page', false, $context);
$search = '/<div class="myclass">(.*)<\/div>/';
preg_match($search, $result, $show);
echo $show[0];
?>
At this point the form is loaded 2 times so the captcha is wrong.
Can someone help me?
I have application which post data to another site(running other application) with API link, and now I need returned feedback like "Application started!" or "error".. I tried to control variable $result but It returns me nothing. Application is started and everything works fine if I visit link with posted results.
If I visit API link manually, it just give me blank page(runned).
proces.php
<?php
if(isset($_POST['submit'])) {
$ssId = $_POST['ssId'];
$red = $_POST['red'];
$id = $_POST['id'];
$user = $_POST['user'];
$postdata = http_build_query(
array(
'ssId' => $ssId,
'red' => $red,
'id' => $id,
'user' => $user
)
);
$opts = array('http' =>
array(
'user' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://apiLink.com/someKey&ssId='.$ssId.'&red='.$red.'&id='.$id.'&user='.$user.'', false, $context);
header("Location: startApp.php");
}
?>
Problem is your current code handles request using $_POST however links trigger http 'GET' method instead of 'POST', also you have some additional checks like $_POST['submit'], hence links redirect fails to work with your logic.
If you replace $_POST with $_REQUEST then things should work with links as well provided you send all the relevant parameters along with it. Other option is just give a button enclosed in form with hidden input fields.
I've been trying to test recaptcha on localhost (xampp with php 5.3); the code below (taken from this answer) works on my remote host and returns 'success':'true', but on localhost { "success": false, "error-codes": [ "missing-input-response" ] } is returned. However, if I change the POST request to a GET request (essentially toggle the comment of the 2 $result = lines), then I get a successful call.
Why does this happen?! Although I'm happy that it works on my actual site, I'd like to understand why GET works but POST doesn't on localhost
<html><head></head><body><script src='https://www.google.com/recaptcha/api.js' async defer>
if(isset($_POST['g-recaptcha-response'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array('secret' => 'my_secret_key',
'response' => $_POST['g-recaptcha-response']);
$options = array(
'http' => array(
'method' => "POST",
'header' => "Content-type: application/x-www-form-urlencoded" . PHP_EOL,
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
//$result = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=my_secretkey&response=' . $_POST['g-recaptcha-response']);
$result = file_get_contents($url, false, $context);
echo $result;
}
?>
<form method="post" action='recaptchatest.php'>
<input id="test" name="test" />
<div style="left:100px;" class="g-recaptcha" data-sitekey="my_key"></div>
<input type="submit" value="Send" id="submit" class="buttons" />
</form>
</body></html>
For completeness, the output of http_build_query($data) is a correctly formed query string (secret=<my key>&response=<response>) and the output of stream_context_create($options) is Resource id #3
So I have a situation where a user submits some data through forms, then clicks a submit button which directs to a separate .php page where processing is done. After the processing is done I need to go to another .php page and send along with it a POST variable I already know the value of.
In html I would make a form with input(s) and a submit button. How do you do that in php without having a user click a submit button ?
The simplest way I can think of is to put the input from the previous page in a form with hidden input type.
For example:
<?php
$post_username = $_POST['username'];
?>
<form id="form1" action="page2.php" method="post">
<input type="hidden" id="hidden_username" value="<?php echo $post_username; ?>" />
</form>
<script>
document.getElementById("form1").submit();
</script>
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Code taken from here, another question which may provide you with some useful answers.
$.ajax({
type: "POST",
url: "YOUR PHP",
data: { PARAMS }
}).done(function( msg ) {
if(SUCCESS)
{
$.ajax({
type: "POST",
url: "ANOTHER PAGE",
data: { PARAM }
})
.done(function( msg ) {
//Process Here
});
You can post arguments in between if you use Json or Xml. Hope it helps !
A useful way is to use the CURL method.
$url = "test.php";
$post_data = array(
"data1"=>$value1,
....
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//we are doing a POST request
curl_setopt($ch,CURLOPT_POST,1);
//adding the post variables to the request
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;//or do something else with the output
Amadan was on to something.
Just stuck this HTML add the end of my php:
<html>
<form id="form" action="webAddressYouWantToRedirectTo.php" method="POST">
<input type="hidden" name="expectedPOSTVarNameOnTheOtherPage" value="<?php echo $varYouMadePreviouslyInProcessing ?>">
</form>
<script>
document.getElementById("form").submit();
</script>
</html>
I know this is bad form, but we can't change the hidden input name as it is set by SalesForce. I have a form with an input like this:
<input type="hidden" name="00N5000000XXXXX" value="Demo_Account" />
and my PHP to post to them via cURL
$00N5000000XXXXX = $_POST['00N5000000XXXXX'];
which obviously won't work as it has number for a variable name.
When I change the name to:
$Foo = $_POST['00N5000000XXXXX'];
the back end doesn't work because it is expecting the form to submit a value with a name of 00N5000000XXXXX, not Foo or whatever I want to call it.
Obviously, Im not a PHP developer but need some advice on how to get around this.
Thank you.
You don't have to save it to a variable first:
<?php
$transferPostFields = array(
'00N5000000XXXXX'
);
$postFields = array();
foreach ($_POST as $key => $value) {
if (in_array($key, $transferPostFields)) {
$postFields[$key] = $value;
}
}
$curlHandle = curl_init();
curl_setopt_array($curlHandle, array(
CURLOPT_URL => 'http://api.salesforce.com/whatever/urls/they/use',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postFields)
));
$output = curl_exec($curlHandle);
echo 'The output we received from SalesForce was: ' . $output;
?>
If you want to transfer all post fields, simply change the top part (anything above $curlHandle = curl_init() to:
$postFields = $_POST;
If you don't need to go past your own server first, then simply change your form:
<form method="post" action="http://api.salesforce.com/whatever/urls/they/use">