【问题标题】:$_GET to accept values with space$_GET 接受带空格的值
【发布时间】:2019-07-20 08:50:05
【问题描述】:

我在这里检查了所有可能的解决方案,不幸的是它不起作用。我的代码似乎有什么问题?

我尝试了以下方法。

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

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

但没有运气。

我在isset中尝试了以下操作

$accounttitle = $_GET['accounttitle'];

urlencode($_GET["accounttitle"])

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

这是我的isset

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

这是我的表格

            <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>

这是我的脚本

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

我可以echo(),但它不适用于带有空格的值。

【问题讨论】:

  • $_GET['accounttitle'] 的值是多少,是 username 还是完整的 names user。
  • @dean 它包含ADVANCES TO EMPLOYEES、OUTING EXPENSES 等值。
  • 好的,这将是一个复杂的代码。就像名称一样,您需要在单词空格之间添加-,并将更新后的值分配给您的$_GET['accounttitle'] 链接。
  • 您需要:?accounttitle='+encodeURIComponent($(this).val()); 见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • @drew010 这个也可以,谢谢!

标签: javascript php html sqlsrv


【解决方案1】:

您的问题不是接收 $_GET,而是您的传出链接。

<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>

【讨论】:

  • 你先生,是一个活生生的救星!太感谢了!我要专注于接收输出,我不知道问题出在&lt;option&gt;
  • 隧道视野很容易获得;)。很高兴为您提供帮助
【解决方案2】:

我更喜欢 trim 'trim()' php 函数而不是字符串替换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2016-07-13
    • 2014-12-26
    相关资源
    最近更新 更多