2

I have a form that submits on itself with edit, delete add parts.

I used a jquery slider to set salary range (It's a job board).

The slider sets SalaryR (the range out put for the screen), SalaryMin and SalaryMax to hidden fields for saving to db.

What I would like to do is have the range set as the already stored amounts if SalaryMin,Max are already set so that I can edit the form. At the moment it defaults back to 20000,50000.

I have no knowledge of Javascript and was surprised I managed to get the min/max values out in the first place. Here is the slider code:

$( "#slider-range" ).slider({
        range: true,
        min: 8000,
        max: 100000,
        step: 500,
        values: [ 20000, 25000 ],
        slide: function( event, ui ) {
            $( "#SalaryR" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ] );
            $( "#SalaryMin" ).val( ui.values[ 0 ] );
            $( "#SalaryMax" ).val( ui.values[ 1 ] );
        }
    });
    $( "#SalaryR" ).val( "£" + $( "#slider-range" ).slider( "values", 0 ) +
        " - £" + $( "#slider-range" ).slider( "values", 1 ) );
0

2 Answers 2

2

I would put these values in a hidden html element on the page and then get the values from these elements in JavaScript. That way, it means you don't have to generate JavaScript through PHP. It keeps your JavaScript independent from your server-side scripting.

PHP

<span id="max" class="hidden"><?php echo $max;?></span>
<span id="min" class="hidden"><?php echo $min;?></span>

CSS

.hidden{display:none;}

JS

var max = $('#max').text();
var min = $('#min').text();
1
  • +1 for suggesting separation of client and server side code Commented Sep 6, 2011 at 10:12
0

Or if the javascript is part of php file, you can use normal PHP tags, for example: <?php echo $amount; ?>. PHP part of the script is processed on the server side and pure javascript code comes to client. Then, your existing script is used.

Example:

var x = { "values" : [ <?php echo $x; ?>, <?php echo $y; ?> ]};

Depends where your JS code is, if you keep it independent or injected in PHP files.

3
  • It's typically thought of as "best practice" to keep it separate from server-side scripting, so that if you change your server side scripting language / framework, you don't have to change your JavaScript and separate from your generated HTML file so that you can take advantage of browser caching to improve page load speed. Commented Sep 6, 2011 at 10:14
  • Ace So it was as simple as putting the following above the JS: <? if (!$SalaryMin) { $SalaryMin = 20000;} if (!$SalaryMax) { $SalaryMax = 25000;} ?> Commented Sep 6, 2011 at 10:21
  • @Spycho: You are absolutely right. However, sometimes it's easier to mix the languages, for example in smaller sites or projects. Usually, it depends on the situation, if you prefer KISS principle or clean design with a bit more overhead. Commented Sep 6, 2011 at 10:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.