1

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!

3
  • you need something like ajax to send it as either a get or post variable Commented Jul 30, 2014 at 20:44
  • PHP = server, JavaScript = client - they can't interact how you want. Use AJAX. Commented Jul 30, 2014 at 20:46
  • Cookies would work, but Ajax is your best bet. Commented Jul 30, 2014 at 20:48

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

0

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'];

http://www.w3schools.com/ajax/ajax_examples.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.