Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there any way to pass a javascript variable to a php function or simply assign a js variable to a php variable....???

test("asdas"); I need to update "asdas" to a dynamic value, i.e of the form,

share|improve this question
2  
No you can't do that –  Srinivas Reddy Thatiparthy Nov 28 '10 at 17:49
    
There is nowhere to pass. Then JS starts to run, PHP is already dead. You can send it to another PHP script though, using usual POST or GET method. –  Your Common Sense Nov 28 '10 at 17:51
2  
@OP: It would help if you told us why you need to pass a JS variable to PHP? Generally it's not needed, we might be able to suggest a better way of doing things. –  Mark Nov 28 '10 at 18:01
1  
@Kut: That wasn't clear at all. Are you trying to validate a form? If so, do it twice. Once with JavaScript only, and then again after the form is submitted. Don't use PHP to validate the form prior to submission. If however, you want to post some data without refreshing the page, use AJAX. –  Mark Nov 28 '10 at 18:17
1  
@Kut: I'd call that a subsection of validation :) Validating that the input is unique. The answer is still AJAX, and what you're trying to do is quite common. You probably would have received better answers if you had written that up front. –  Mark Nov 28 '10 at 19:49

3 Answers 3

up vote 1 down vote accepted

You can pass value from js to PHP using ajax request or add js value to URL and then reload page.

Variant #1 (using ajax):

JS side (jquery)

var js_var = 'hello';
$.ajax({
   type: "POST",
   url: "some.php",
   data: "js_var="+js_var,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

PHP side

$js_var = isset($_POST['js_var']) ? $_POST['js_var'] : '';

Variant #2 (with page reload):

JS side

<script type="text/javascript">
<!--
var js_var = 'hello';
window.location = "http://www.yoursite.com/?js_var="+js_var;
//-->
</script>

PHP side

$js_var = isset($_GET['js_var']) ? $_GET['js_var'] : '';

share|improve this answer
    
That would make the page reload which I would consider as looking bad. –  thejh Nov 28 '10 at 17:57
    
@thejh: Then you have to use AJAX. –  Mark Nov 28 '10 at 18:00
    
@thejh, for prevent page reload send params using ajax. –  Valery Viktorovsky Nov 28 '10 at 18:01
    
Don't forget too, that you can pass the value through a form as well. document.getElementById('MyInput').value = my_js_variable; then when it gets submitted, the value will go along with it. –  Rahly Nov 28 '10 at 18:12

When you want to send something from JS to PHP or from PHP to JS, use Ajax. If PHP already knows the value it wants to send to JS when it's delivering the page, you can also embed it in JS:

<script>
  var name="$name";
</script>

Edit: As porneL said, you should not put data in the page that way without escaping it to avoid opening XSS holes.

Oh, and if you use AJAX, also pay attention on not making ajax data text/html or so to avoid XSS, too.

share|improve this answer
    
This is insecure. You should use <script> var = <?php echo json_encode($name); ?>. –  porneL Nov 28 '10 at 18:07
    
@porneL: Depends on where the data comes from and whether it's already encoded/escaped. –  thejh Nov 28 '10 at 18:10
    
Well, then you shouldn't be relying on your luck or handling half of JS's escaping in part of application that doesn't generate JS ;P –  porneL Nov 28 '10 at 18:13

you can't

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.