Get cookie of parent page (iframe) using JS - javascript

I need to get a cookie created in parent page.
I need get a spesific cookie (example) who was cread on modiface--locatelcolomia.myvtex.com and used this on my child page modiface.locatelcolobmia.com (this one is called by vtex iframe)

You cannot directly get cookies from other pages.
You can send a request to that page to send the cookies.
getCookies.php (put on parent page)
window.onload = function(){document.getElementById('submitpost').submit();}
<?php if(!isset($_COOKIE['COOKIE_NAME_HERE']) {die('set cookie');} ?>
<h3>Please wait</h3>
<form id="submitpost" action="PUT_PREVIOUS_PAGE_URL_HERE" method="post">
<input type="hidden" name="cookie" value="<?php echo $_COOKIE['COOKIE_NAME_HERE']; ?>">
</form>
Then set the cookie to that.
Redirect users without the set cookie to the getCookies.php page.
This form can be intercepted and changed, but cookies can as well. Never put account details in cookies.
This answer requires a web server with PHP, chances are that your host does support PHP.

Thanks for the help, this is the answer: I got the cookie by Jquery and save on a input type hidden at the end send to my iframe calling a php page and send this value by URL (GET).
jQUERY
let cartId = $('#idCartInput').val();
IFRAME
("#idCartBtn").html("<iframe src='http://localhost:81/projectExample/modiface/index.php?cartId="+cartId+"' class='frameModiface' id='pruebasId' allow='camera' name='iframe_a'></iframe>");

Related

Submit form and stay on same page with target='_top'

Here is my form on domain1:
<form id ='myForm' action='domain2' method='GET' target='_top'>
<input type='hidden' name='id' value='00345897'>
<input type='hidden' name='status' value='Success'>
<input type="submit" value="Submit">
</form>
Now when I submit my form it goes to the domain2 as mentioned in the action. Is it possible to get the response back to domain1? I am getting the response if the target attribute is '_self' or if I remove the target attribute. But with target='_top' the page is redirected to domain2. How can I get the response back to domain1? Thanks.
The response can't go anywhere other than to the browser making the response.
If you don't specify a target, then it will appear in the same window or frame as the page with the form on it (replacing the page with the form on it).
If you specify target="_top" then it will appear in the top-level frame replacing that.
If you want to get the data to the site making the request then you need to either:
Make the HTTP request from domain1's server and not from a browser or
Use Ajax to make the request to domain1 (dealing with cross origin issues) and then use JavaScript to send the data to domain1.

How to Redirect to a URL in PHP from with Post Data as URL String

I have searched a lot but did not find any answer...
My Question is, let's say I have a random page and user click on the download button to download a file, and the html in the button is:
<form action="http://seconddomain.com/thanks-for-downloading/" target="_blank" method="post">
<input type="hidden" name="FileID" value="dYnte-m9bZ4">
<div align="center">
<input alt="Download Movie" src="image url here" type="image">
</div>
</form>
This data is submitted to the next page with post method using in the form, as you can note the value is defined as "dYnte-m9bZ4"
In this case, this value is the file download link, eg: downlaodfile.com/dYnte-m9bZ4
I want that when this data is submitted with post method on the next page there should be some PHP code that will automatically attach the defined value to the server URL/link as downlaodfile.com/dYnte-m9bZ4 and the user will be automatically redirected to the download page.
If this is not possible with post form method, then please suggest any other method...
Please tell me how can I do it.
Thanks...
Add this line,from where you want to redirect in your post handler php file.
$var = $_POST["FileID"];
// do input validation ,type-check etc on $var.
header("Location: http://downlaodfile.com/".$var);
I have done same thing of redirecting after form submission
you can check my code # Bitbucket line no 459 of link
Never use user input directly. Never.
Sanitize and validate before using user input.
Escape data when necessary (when data switches execution contexts).
Note: This does not include error handling. This is just a skeleton.
$filteredPost = filter_input_array(INPUT_POST, $filterRulesArray); //Sanitize
$validatedPost = filter_var_array($filterdPost, $validationRulesArray) //Validate
$fileId = urlencode($validatedPost['FileID']); //Potentially, an important step in this case.
header("Location: http://downloadfile.com/{$fileId}");
PHP Manual: filter_input_array()
PHP Manual: filter_var_array()
PHP Manual: urlencode()

How to access parameters that were passed to next page

As part of my requirement i constructed the href link with a variable associated with it as follows
<h5> ', $NAME, '</h5>
Upon click, page is being redirected correctly to single.php and even $ID value can be seen on the browser.
But I dont know how to extract ID parameter in single.php
Can any body please tell me how can i access $ID value in single.php page
<h5> ', $NAME, '</h5>
<?php echo $_REQUEST['id']; ?>
you can use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.
Session:
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.
Cookie:
//One page 1
$_COOKIE['varname'] = $var_value;
//On page 2
$var_value = $_COOKIE['varname'];
GET and POST
You can either add the variable in the link to the next page:
Page2
This will create a GET variable, or include a hidden field in a form that submits to page two:
<form method="get" action="page2.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>
And then on page two
//Using GET
$var_value = $_GET['varname'];
//Using POST
$var_value = $_POST['varname'];
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.
Supposing the $ID is set to 2, your URL is:
http://working.artefacts.co.in/single.php?2
So, in your single.php page you have to write:
$id = $_SERVER['QUERY_STRING'];
', $NAME, '
PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:
Test $GET
When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.

global variable not displayed in div

I declare a variable at the beginning of my .js file:
var option="blabla";
On page 1.html I click on a link to page 2.html where I have
<script>document.write(option);</script>
No text is displayed on 2.html. When I refresh the browser while I am on 2.html I get undefined as an output.
What do I have to do to have the text displayed straight after I click the link?
Alternatively, how can I get the following code work to output strUrl on 2.html:
on 1.html I have a link:
<a href="2.html" onclick="function1("item")">
on 2.html I have a div:
<div id="display">document.write(strUrl);</div>
then I have in my .js file:
function1(searchitem)
{
strUrl = 'http://blabla.com/'
+ '?key=' + searchitem;
}
You try to create a Javascript variable on a page and then use it on another page. This is a more-or-less broad problem, since you want to maintain values across pages. First of all, you need to decide where is this value going to be defined and where is it going to be used. If this is more like a server-side variable, then you need to define it on server-side and then generate it into your Javascript code. If you are using PHP, then you can do it like this:
<script type="text/javascript>
var foo = '<?php echo $bar; ?>';
</script>
Naturally, you need to initialize $bar to be able to do that. If the variable should be a client-side variable, then you need to use localStorage, like this on 1.html:
localStorage.setItem("option", "blablabla");
and then load it on 2.html:
localStorage.getItem("option");
Or, if you need to use it both on server-side and client-side, then you can use a cookie for this purpose. Using cookies i slightly more complex, but my answer to another question should get you going.
Let's focus on the cause this did not work for you. A Javascript variable will cease to exist when the page is unloaded, so you will not be able to use its value after that. So, you need to persist it somehow, storing it either on the server or the computer where the browser is being run.
As a side-note, I should mention that you can use Javascript variables accross pages if you load some pages inside iframes of a page, but that is a different scenario.
This is what FORMS and AJAX were invented for. If your server has a PHP processor (virtually ALL of them do), then you can rename your .html files to .php and use a bit of PHP to accomplish your goal.
A web page ending with .PHP works exactly the same as one ending in .html, except that you can now add snippets of PHP code where desired. It is not necessary to have any PHP code, but if you have some it can do stuff.
Method One: FORMs
If you want to switch to page2.html and see a value sent from page1.html, you can use a FORM construct and post the data from page1 to page2:
page1.php
<form action="2.html" method="post">
<input name="option" type="text" />
<input type="submit" name="sub" value="Go" />
</form>
page2.php
<?php
$p1 = $_POST['option'];
?>
<div>On page1 of this website, you typed: <?php echo $p1; ?>. That's what you did.</div>
Note how a <form> uses the name= attribute for the name of the variable that is sent to the other side.
Example Two: The AJAX method
HTML:
<div id=nonForm">
<input id="option" type="text" />
<input type="button" id="myButt" value="Go" />
</div>
<div id="results"></div>
jQuery:
$('#myButt').click(function(){
var opt = $('#option').val();
$.ajax({
type: 'post',
url: 'page2.php',
data: 'option='+opt,
success: function(john){
if (d.length) alert(john); //display result from Page2 in a pop-up box
$('#results').html(john); //Or, display it right on the page
}
});
});
PAGE2.PHP -- The AJAX processor file
<?php
$opt = $_POST['option'];
//Now, you can do something with the data in $opt, and then send back a result
$rtn = 'Hey, you sent: ' .$opt;
echo $rtn;
The primary (and most important) difference between the two methods is that the FORM will change pages on you. The user will be sent from Page1 to Page2, and the screen will flash as this happens.
What's exciting about AJAX is it sends data to Page2, where Page2 can do something with it (for example, a database lookup), and then Page2 sends different data back to Page1. This new data can then be displayed on the page WITHOUT the page refreshing.
Here are a couple of very basic AJAX examples, to get you going:
AJAX request callback using jQuery

Cookie not set until a second refresh

I have a form on page 1:
<form method="post" action="request-form">
<input
type="text"
id="amzQry"
name="item"
placeholder="What do you need?"
autocomplete="on"
/>
<input
id="autocomplete"
name="destination"
placeholder="where? (e.g. Buenos Aires)"
onfocus="geolocate()"
type="text"
required=""
aria-required="true"
autocomplete="off"
/>
<button type="submit" value="">
Submit
</button>
</form>
I want this information to be held in a persistent way so that even if a user subsequently logs in (to joomla in this case) the cookie data is persistent and can be called. That is why i have used cookies rather than sessions in this case. Correct me if this is not the right way of doing this.
I have some code to set and retrieve the cookie on page 2:
<?php
$itemcookie = $_POST['item'];
$detsinationcookie = $_POST['destination'];
setcookie("itemcookie", $itemcookie, strtotime('+30 days'));
setcookie("destinationcookie", $detsinationcookie, strtotime('+30 days'));
?>
But the cookie data is not appearing on the second page when it loads after form submit. If I refresh the second page the data appears in the right places, i.e. where I have called it with e.g.
<?php
echo $_COOKIE["itemcookie"];
?>
How to get the cookie data available immediately on page 2?
You can't.
If you check the manual:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
^^^^^^^^^^^^^^
This means your cookies will not be available on the page / script where you set them.
You could use another variable to show the value though, for example like:
$itemcookie_value = isset($_POST['item']) ? $_POST['item'] : $_COOKIE["itemcookie"];
Apparently you have some output before you call setcookie()
If you have some output (even one single space character) BEFORE session_start(), setcookie() or, say, header(), the browser will not recognize the cookie and they won't be available right after the script starts.
You can.
All you have to do is set the cookie with a AJAX request that way the cookie is set while you are still on the page then when you refresh the page ONE time the cookie will be available to you.

Categories

Resources