zeckdude
zeckdude

Reputation: 16163

How can I remove characters from get variables before sending form data?

I have a form where users enter the amount of rent to pay.

<form action="search_results.php" method="get">
  <input type="text" name="rent" value="$" />
</form>

I am using jQuery to keep the dollar sign in the field when the user enters their rent amount.

$('input[name=rent]').keyup(function() {
   $(this).val(function(i,v) {
     return '$' + v.replace('$',''); //remove existing, add back.
   });
 });

When the form sends to the search_results.php page, it sends to the url with the following get variable attached to it:

http://www.domain.com/search_results.php?rent=%24

When the page begins to load, it shows the following error message:

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /nfs/c09/h01/mnt/12345/domains/www.domain.com/html/search_results.php on line 551

I'm pretty sure the error occurs because my sql statement is using the $_GET['rent'] variable to sort through all the listings with less than the rent amount entered, and it cannot do that with the dollar sign in it.

How can I remove the dollar sign character from the input field before it sends to the search_results.php page OR how can I remove the %24 from the url on the search_results.php page and then reload the page?

Upvotes: 1

Views: 936

Answers (4)

This errors occurs because you are not giving mysqli resource to it.

if (!mysqli_query($link, $query)) {
   printf("Errormessage: %s\n", mysqli_error($link));
}

You are simply calling this function like that: mysqli_error();

Upvotes: 0

Yehonatan
Yehonatan

Reputation: 3225

Well, here is a better solution - You can filter the data after he passed:

$rent = str_replace('$','',$_GET['rent']);
$rent = str_replace('%24','',$rent);

Btw - I suggest you to learn more about SQL injection.

Upvotes: 1

Ram
Ram

Reputation: 144689

try this:

$('form').submit(function(){
  $('input[name=rent]').val($('input[name=rent]').val().replace('$', ''))
})

Upvotes: 2

J&#248;rgen
J&#248;rgen

Reputation: 9130

This should work:

$('form').submit(function(){
  $('input[name=rent]').val(function(index, valueWith$){
    return valueWith$.substring(1);
  });
  return true;
});

Upvotes: 1

Related Questions