PDA

View Full Version : pass value from js variable to php variable



blacktiger786
Jan 28th, 2014, 09:13 AM
hi everyone i have 2 file one is test.js and second is test.php
in test.js file i have this function

function randString(n)
{
if(!n)
{
n = 35;
}

var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

for(var i=0; i < n; i++)
{
text += possible.charAt(Math.floor(Math.random() * possible.length));
}

return text;
}
var randomvalue = randString(34);

this function generate 34 random values and save to var randomvalue in test.js file its fine

now what i want
i have $value variable in test.php file
i want when var randomvalue in test.js get value
this same value also pass the $value variable in test.php
and $value variable in test.php show also same value at a time

how to push value var randomvalue in test.js to in $value variable in test.php?

sunfighter
Jan 28th, 2014, 04:00 PM
The only way(s) you can have communication between A program on the server(PHP) and one in side the browser(javascript) is A.) submitting a form. B.) using session variables, but this is not strictly javascript. And C.) ajax.
Here is ajax with a return that is used to show the variable was sent from JS to PHP and PHP sent it back.



<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body style="background-color: # AA5303;"><!-- #80ff00;">-->


<div id="here">Information</div>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function randString(n){
if(!n){
n = 35;
}
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for(var i=0; i < n; i++){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
$.ajax({
type: "POST",
url: "ajax.php",
data: { name: text}
})
.done(function( msg ) {
document.getElementById("here").innerHTML = "Data Saved: " + msg ;
});
}
var randomvalue = randString(34);
</script>
</body>
</html>


This is ajax.php


<?php
$cosmo = $_POST["name"];
if ($_POST['name'])
echo $cosmo;
?>


It just might be better to make the variable up in the PHP and forget JS.

DrDOS
Jan 28th, 2014, 04:07 PM
You can also put the variable in the URL as a 'get' value and fetch it in the php using $_GET, or you can put it in a cookie.

blacktiger786
Jan 28th, 2014, 05:12 PM
thanks for reply
actually i use this function in jspopunder
jspopunder('http://localhost/test.php?randomvalue='+randomvalue+'')
this link in test.js
its open link with also random number
and we easly $_GET from this link in php file
but we want also in test.js random pass to direct in php file

after we match the random number from $_GET from url and get from js file value
then show our page

you asked why this method
because i want secuitry on our link i want no person see detail when its run direct link its
open in url but its detail show when he get from popup

glenngv
Jan 29th, 2014, 12:21 AM
Why not do this onclick of the link?

1. Randomize a number.
2. Save it in a cookie.
3. Open popup passing the generated random number in the querystring.
4. In PHP, read the cookie and compare against the the random number in querystring.
5. If they are the same, then display the content, otherwise don't.
6. Always delete the cookie on PHP side regardless if content is displayed or not.

This means that even if user clicks on the link and the content is displayed, if he refreshes the popup, it will not display the content. The user will be forced to always click the link to see the content.

Note that this mechanism is not fool-proof. Any knowledgeable user might be able to figure out what to do.

blacktiger786
Jan 29th, 2014, 05:20 AM
Why not do this onclick of the link?

1. Randomize a number.
2. Save it in a cookie.
3. Open popup passing the generated random number in the querystring.
4. In PHP, read the cookie and compare against the the random number in querystring.
5. If they are the same, then display the content, otherwise don't.
6. Always delete the cookie on PHP side regardless if content is displayed or not.

This means that even if user clicks on the link and the content is displayed, if he refreshes the popup, it will not display the content. The user will be forced to always click the link to see the content.

Note that this mechanism is not fool-proof. Any knowledgeable user might be able to figure out what to do.

thanks sir i am really want this . can you tell me how to save this random value in cookie and how to after match its delete its cookie value any tutorial

Thanks again

glenngv
Jan 29th, 2014, 05:53 AM
There are tons of cookie scripts on the web. Here's one from a sticky post here in CF:

http://www.codingforums.com/showthread.php?p=172730#post172730

For using cookies in PHP:

http://php.about.com/od/advancedphp/qt/php_cookie.htm