Avionicom
Avionicom

Reputation: 191

.replace() not working inside a jQuery function

I don't understand why replace() function doesn't work into my jQuery function:

jQuery(document).ready(function($){
   var amount_min =  <?php if($_GET['amount_min']) echo $_GET['amount_min']; else echo '0'; ?>;
   var amount_min = amount_min.replace(/[^\d]/g, "");
   $('input[name=amount]').val(amount_min);
});

Whatever input I give (for example "100ab" or "10.000") it doesn't replace it with "100" or "10000". How to do?

Upvotes: 2

Views: 322

Answers (4)

Ja͢ck
Ja͢ck

Reputation: 173562

You don't need the .replace() code (it also only works on strings); PHP can already do the proper conversion for you:

$(function() {
   var amount_min = <?php echo isset($_GET['amount_min']) ? (int)$_GET['amount_min'] : 0; ?>;
   $('input[name=amount]').val(amount_min);
});

You could also use filtering for this:

<?php echo filter_input(INPUT_GET, 'amount_min', FILTER_VALIDATE_INT, array('options' => array('default' => 0))); ?>

Note that filter_input would not accept a value like 100abc, so use wisely.

If you still want to use strings safely in JavaScript you should use json_encode().

Btw, any answer that involves an unmodified echo of a request variable from PHP inside JavaScript code is wrong and can cause XSS attacks! You have been warned.

Update

The regular expression based replacement can also be done in PHP:

var amount_min = <?php echo (int)preg_replace('/\D+/', '', isset($_GET['amount_min']) ? $_GET['amount_min'] : 0); ?>;

Since all non-digits are removed, you can safely apply the (int) cast.

Upvotes: 1

Alexander
Alexander

Reputation: 23537

You forgot to put double-quotes.

var amount_min = "<?php if($_GET['amount_min']) echo $_GET['amount_min']; else echo 0; ?>";

Because, replace works in Strings.


UPDATE #1

If for any religious reason you don't want to wrap the PHP in double quotes then output them along with the number.

var amount_min = <?php echo '"' . ($_GET['amount_min'] ? $_GET['amount_min'] : 0) . '"'; ?>;

UPDATE #2

Compulsory validation you can use:

var amount_min = <?php echo '"' . (int)($_GET['amount_min']) . '"'; ?>;

Upvotes: 2

Filippo oretti
Filippo oretti

Reputation: 49817

can you please try this:

$(document).ready(function(){

     var amount_min =  "<?php if($_GET['amount_min']){ echo $_GET['amount_min'];}else{ echo '0';} ?>";

      console.log("original-> "+amount_min);

      var amount_min = amount_min.replace(/\D/g,'');

      console.log("replaced-> "+amount_min); 
});

Upvotes: 1

Blender
Blender

Reputation: 298176

Your PHP code is outputting a number:

var amount_min = 100;

Since you're expecting a string, wrap it in quotes:

var amount_min =  "<?php if($_GET['amount_min']) echo $_GET['amount_min']; else echo '0'; ?>";

I haven't touched PHP in years, but I think you could simplify your code a little:

var amount_min =  "<?php echo($_GET['amount_min'] || '0'); ?>";

Also, why don't you just fetch the GET parameter with JavaScript?

Upvotes: 1

Related Questions