2

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?

3
  • 1
    Your dropping the value into JavaScript as a number, and there's no .replace() function for numbers in JavaScript. Commented Dec 29, 2012 at 15:52
  • @Avionicorn i updated my answer, i think your document ready declaration is not so good too Commented Dec 29, 2012 at 16:00
  • There is no such thing as a "jQuery function". Commented Dec 29, 2012 at 17:13

4 Answers 4

2

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']) . '"'; ?>;
Sign up to request clarification or add additional context in comments.

9 Comments

No, I want to embed php in javascript, so quotes have to be not included.
@Avionicom you need quotes
@Avionicom, since when? As long as I remember PHP is parsed concerning <?php and ?>
@Avionicom, perfect. Don't forget to mark the most helpful of the answers to avoid your acceptance to keep going into decline
@Avionicom Yes, it's in my answer ... somewhere dangling below :)
|
1

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); 
});

4 Comments

@Avionicom what you need to replace from the amount?
letters, dots, comma, dashes... everything is not a number
@Avionicom try now, be sure php is printing your data check the page source and see the value of amount_min if is it correct or are there any errors
what this console.log("original-> "+amount_min); returns in console ?
1

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?

1 Comment

Yes, my php code is outputting a number taken from previous GET, or set it to 0
1

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.

2 Comments

Your function makes numbers like 100.000 -> 100 but since we use european notations, I would like 100.000 becomes 100000
@Avionicom then you could either use json_encode() as mentioned and keep the rest of your current code or use preg_replace() in PHP to perform the conversion.

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.