0

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 ??

2
  • What is #slider1? Is that some kind of input in a form? Commented Jan 25, 2011 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. Commented Jan 25, 2011 at 8:24

4 Answers 4

5

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']);
?>
2

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

0

add it to hidden input in your form

like

$('#hidden_field').val(slider1)
0

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>

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.