I am trying to set a php variable for use in a later page with out using $_GET or $_POST. I am also assigning the same variable to a text box. I already have the function to assign the textbox variable but not the php variable.
function setText(data){
document.getElementById("filtext").value = data.value
// <?php $_SESSION["provid"] = ?>
// I am trying to set the php variable above
}
Any help would be greatly appreciated!
-
you need something like ajax to send it as either a get or post variableRodrigo– Rodrigo2014年07月30日 20:44:46 +00:00Commented Jul 30, 2014 at 20:44
-
PHP = server, JavaScript = client - they can't interact how you want. Use AJAX.tymeJV– tymeJV2014年07月30日 20:46:05 +00:00Commented Jul 30, 2014 at 20:46
-
Cookies would work, but Ajax is your best bet.HartleySan– HartleySan2014年07月30日 20:48:01 +00:00Commented Jul 30, 2014 at 20:48
2 Answers 2
I think in this case, setting a cookie would be the easiest solution. By setting the cookie using Javascript, the cookie key and value will automatically be sent to PHP with the next page request.
See also:
- http://www.w3schools.com/js/js_cookies.asp, especially "A Function to Set a Cookie"
- http://www.w3schools.com/php/php_cookies.asp, especially "How to Retrieve a Cookie Value?"
Comments
To send a value for a server-side language you can use AJAX.
function setPHPVariable()
{
var myAjax;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
myAjax=new XMLHttpRequest();
}
else
{// code for IE6, IE5
myAjax=new ActiveXObject("Microsoft.XMLHTTP");
}
myAjax.onreadystatechange=function()
{
if (myAjax.readyState==4 && myAjax.status==200)
{
alert('PHP returned: ' + myAjax.responseText);
}
}
myAjax.open("GET","my_file.php?my_var=12345", true);
myAjax.send();
}
In your PHP code, you can use:
$myVar = $_GET['my_var'];
$_SESSION['my_var'] = $myVar; // or even $_SESSION['my_var'] = $_GET['my_var'];