pjustindaryll
pjustindaryll

Reputation: 377

$_GET to accept values with space

I checked all the possible solutions here, unfortunately it won't work. What seems to be the problem with my code?

I tried the following.

urldecode (http://php.net/manual/en/function.urldecode.php)

str_replace http://php.net/manual/en/function.str-replace.php)

But no luck.

I tried the following in isset

$accounttitle = $_GET['accounttitle'];

urlencode($_GET["accounttitle"])

$accounttitle = str_replace(" ", "", $accounttitle );

Here's my isset

  if(isset($_GET['accounttitle'])){
    $accounttitle = $_GET['accounttitle'];
  } ?>

Here's my form

            <div class="box-header with-border">
              <!--<a href="#addnew" data-toggle="modal" class="btn btn-primary btn-sm btn-flat"><i class="fa fa-plus"></i> New</a> --> 
<div class="form-group">              
<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" style="text-transform:uppercase" required>
<option value="">PLEASE SELECT OPTION</option>
<?php 

while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{

    echo "<option value=".$row['accounttitle'].">".$row['accounttitle']."</option>";

}
  ?>
</select>
</div>
                  </div>
                </form>

Here's my script

$(function(){
  $('#select_account_title').change(function(){
    window.location.href = 'earnings_amendment.php?accounttitle='+$(this).val();
  });
});
</script>

I can echo() but it won't work on values with space.

Upvotes: 0

Views: 61

Answers (2)

Two
Two

Reputation: 672

I prefer trim 'trim()' php function not string replace.

Upvotes: 0

Tim Morton
Tim Morton

Reputation: 2644

Your problem is not receiving $_GET but rather with your outgoing link.

<select class="form-control" id="select_account_title" style="text-transform:uppercase" required>
  <option value="">PLEASE SELECT OPTION</option>
  <?php while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)): ?>
    <option value="<?= urlencode($row['accounttitle'])?>"><?= $row['accounttitle'] ?></option>
  <?php endwhile; ?>
</select>

Upvotes: 1

Related Questions