4

My jquery scripts have some php in them to initiate variables, etc. I'm trying to use something like minify to compress it and deter prying eyes. The php is causing issues. Anybody done anything similar?

here is just an example of my php-infused javascript:

    $('input[name=type]:radio').filter('[value=<?=$type?>]').attr('checked', true);
    $('input[name=cert]:checkbox').filter('[value=<?=$cert?>]').attr('checked', true);
    $('input[name=gauge]:checkbox').filter('[value=<?=$gauge?>]').attr('checked', true);
3
  • PHP short tags is really enabled? Commented Mar 5, 2011 at 16:27
  • 1
    The best approch is to separate php and javascript , you can make an ajax call to get a json of your data , then you can access the response and fetch it Commented Mar 5, 2011 at 16:37
  • Is the PHP code only in JavaScript strings or is it used anywhere else? Commented Mar 5, 2011 at 16:48

3 Answers 3

2

PHP attempts to completely separate the JavaScript in this way. In your PHP file you keep the values as follows:

file.php:

<html>
<head>...</head>
<body>
...
<input type="hidden" id ="value_type" value="<?=$type?>" />
<input type="hidden" id ="value_cert" value="<?=$cert?>" />
<input type="hidden" id ="value_gauge" value="<?=$gauge?>" />
...
</body>
</html>

file.js:

$(function() {

  $.data_values = {

     "type": $("#value_type").val(),
     "cert": $("#value_cert").val(),
     "gauge": $("#value_gauge").val()

  };

});

when you have to use the values:

$('input[name=type]:radio').filter('[value="'+$.data_values.type +'"]').attr('checked', true);
$('input[name=cert]:checkbox').filter('[value="'+$.data_values.cert +'"]').attr('checked', true);
$('input[name=gauge]:checkbox').filter('[value="'+$.data_values.gauge +'"]').attr('checked', true);
Sign up to request clarification or add additional context in comments.

Comments

1

Minify won't work with PHP. If you have to keep the PHP in there are there isn't too much of it, you could replace it with a known tag (i.e. 'ABCDE', '12345'), then minify it, then substitute the tags with your PHP again.

1 Comment

That's what I was thinking as well. There isn't much php, so I'll give it a try.
1

When all of your PHP is in JavaScript strings like in your example code then any minification tool worth its salt should work just fine. If you want to use UglifyJS for example, then you can try it online and see if it works for your code.

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.