简体   繁体   中英

Remove dollar sign from query on Submit

I am working on a website where there is a donate box with a field and a contribute button.

<div id="donate-bar">
    <form name="donate" id="donate">
        <span class="donate-text">DONATE</span>
        <input type="text" class="amount" name="amount" id="amount" value="$10" size="5">
        <input type="submit" id="contribute" formaction="https://website.com/donate" accept-charset="utf-8" formmethod="GET" value="CONTRIBUTE">
    </form>
</div>

However, the query gets sent to the donation processor's website as https://website.com/donate?amount=%2410 and doesn't tick the $10 box. If I remove the $ and submit the query again the value is sent and the correct amount is seen on the second page.

Is there an easy way using PHP or Javascript to have it remove the $ when the form is submitted so that https://website.com/donate?amount=%2410 changes to https://website.com/donate?amount=10 .

I've tried a couple of things but nothing I've tried has worked. Some examples of code would be super helpful since I'm newer to some of this. Can I parse it as integer on submit? Any help would be greatly appreciated. Thank you!

Why not put the Dollar sign outside of the input and store the value as "10":

<div id="donate-bar">
  <form name="donate" id="donate">
    <span class="donate-text">DONATE</span>
     $<input type="text" class="amount" name="amount" id="amount" value="10" size="5">
 <input type="submit" id="contribute" formaction="https://website.com/donate" accept-charset="utf-8" formmethod="GET" value="CONTRIBUTE"></form></div>

$_POST['amount'] = str_replace("$", "", $_POST['amount']);
$('form#donate').on('submit',function() {
  var $input = $('input#amount');
  $input.val(parseInt($input.val().replace(/\$/,''),10));
});

There is a simple way to fix this.

First of all, one of my suggeestions is that you change the field to

type="number"

Second of all, in PHP, you can use the following to remove the dollar sign:

$amount = str_replace('$', '', $_REQUEST['amount']);

You can use split function in jquery as follows

var value = ($('#amount').val()).split('$'); var amount = value[1] ;

In PHP

$value = explode('$',$_POST['amount']); //results array elements without $ symbols $amount = $value[1];

Or also in PHP

$amount = preg_replace("/[^0-9]/","",$_POST['amount']);

//gets only integer parts and omits all other

Also in PHP

$amount = str_replace('$', '', $_POST['amount']);

Hope this helps the simplest way is a hidden field:

  <html>
  <head>
  <script src="http://code.jquery.com/jquery-latest.min.js"
          type="text/javascript"></script>
  <script>
    $(document).ready(function() {
      //number regex - the format $10
      var regex = /\$[1-9][0-9]+/;

      //Setting validations
      $(document).on('blur', '#pretty_amount', function() {
        if(!regex.test($(this).val())) {
           //Do something probably clear field
           alert("Error Invalid number: " + $(this).val());
        } else {
           var newValue = $(this).val().match(/[1-9][0-9]+/g);
           $('#amount').val(newValue);$('#amount').innerHtml(newValue);
        }
      });
    });
    </script>
  </head>
  <body>
  <div id="donate-bar">
    <form name="donate" id="donate">
      <span class="donate-text">DONATE</span>
      <input type="text" style="display: none;" name="amount" id="amount">
      <input type="text" class="amount" name="pretty_amount" id="pretty_amount" value="$10" size="6">
   <input type="submit" id="contribute" formaction="https://website.com/donate" accept-charset="utf-8" formmethod="GET" value="CONTRIBUTE"></form></div>
  </body>
  </html>

This does what you want, if I'm not mistaken.

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM