Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

First of all.. is this possible? I work&create a var in javascript (jquery ui framework) and then when I click on the "Submit" button of my form it should this variable?

HTML & Javascript (Jquery UI):

<script>

      $(document).ready(function() {

var slider1 = $( "#slider1" ).slider( "value" );
var slider2 = $( "#slider2" ).slider( "value" );
      });
</script>

pseudo: HTML & PHP:

<?php echo slider1 ?>

How can I echo the value of #slider1 ??

share|improve this question
What is #slider1? Is that some kind of input in a form? – Dan Grossman Jan 25 '11 at 8:23
1  
You could create a hidden input field that holds the 'slider1' variable for example. Everytime the slider1 value is updated you should also update the hidden input field. Then, after submitting the hidden variable will be available in PHP. – Smartje Jan 25 '11 at 8:24

4 Answers

up vote 5 down vote accepted

You cannot assign a variable from javascript to php, but you can set a javascript variable to a form input element before you send the form, then you can get the values via the $_POST or $_GET (or the $_REQUEST) superglobal arrays, based on your method ().

http://php.net/manual/en/reserved.variables.get.php

http://php.net/manual/en/reserved.variables.post.php

For example:

Javascript:

    $(document).ready(function(){
      var test = "test";
      $('#someinput').value(test);
   });

Html:

<form action="some.php" method="post">
<input type="hidden" id="someinput" name="someinput"/>
<input type="submit" value="send"/>
</form>

PHP:

<?php
  var_dump($_POST['someinput']);
?>
share|improve this answer

Mike you cannot mix server side code with client side code. They are completely seperate!

What you can do is just before the form posts put some value inside a hidden input and check it on the server side

share|improve this answer

add it to hidden input in your form

like

$('#hidden_field').val(slider1)
share|improve this answer

You are misunderstanding how PHP and Javascript work. PHP runs on the server, JavaScript runs on the browser. When JavaScript runs, PHP is already done generating the document.

If you have a JavaScript variable, you need to use JavaScript to alter the element.

For example:

<div id="slider_value"></div>

<script>

      $(document).ready(function() {

var slider1 = $( "#slider1" ).slider( "value" );
var slider2 = $( "#slider2" ).slider( "value" );

// Assign value to DIV
$("#slider_value").html("Value: "+slider1);

});
</script>
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.