为了账号安全,请及时绑定邮箱和手机立即绑定

设置输入值以获取变量

设置输入值以获取变量

PHP
潇潇雨雨 2023-10-21 10:05:41
我有两个网页。索引.php这包含一个表格<form id="searchForm" class="" action="search.php" method="get" style="position:absolute; top:150pt; left:10pt; font-size:17pt;">  <label for="">Postcode</label>  <input type="text" id="postcode" name="postcode" value="" placeholder="AB12 3CD" oninput="checkPostcode()">  <!-- function isValidPostcode(p) {        var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;        return postcodeRegEx.test(p);    } -->  <br>  <label for="">Type of Course</label>  <select class="" name="coursetype">    <option value="" disabled selected></option>    <option value="online">Online</option>    <option value="">In Person</option>  </select>  <br>  <input type="submit" value="Search"></form>当表单提交 search.php 中的操作时搜索.php这显示了与上一页输入的数据相同的表格<?PHP $postcode = $_GET['postcode'];?>    <form class="" action="search.php" method="get">    Postcode <input type="text" name="postcode" value="    <?php    $postcode = str_replace('%20', ' ', $postcode);    $postcode = str_replace('-', ' ', $postcode);    $postcode = str_replace('%09', '', $postcode); // This line was added to try to remove     the tabs    echo $postcode;    ?>    ">    Tags    <select id="tagsInput" class="js-example-basic-hide-search-multi" name="specialities[]"     multiple="multiple" onkeyup="filterTags()">        <option value="AL">Lorem</option>        <option value="WY">ipsum</option>    </select>    <script>    $(document).ready(function() {        $('.js-example-basic-hide-search-multi').select2({language: "en"});    })    </script>    </script>    <input type="submit" value="Submit"></form>但是,当输入显示在 search.php 中时,输入的两侧都有两个选项卡。我该如何摆脱这个?
查看完整描述

3 回答

?
莫回无

TA贡献1865条经验 获得超7个赞

有趣的是,您收到该错误是因为您使用的是 PHP。


Postcode <input type="text" name="postcode" value="

<?php

$postcode = str_replace('%20', ' ', $postcode);

$postcode = str_replace('-', ' ', $postcode);

$postcode = str_replace('%09', '', $postcode); // This line was added to try to remove 

the tabs

echo $postcode;

?>

">

注意你是如何做到的,然后在下一行value="开始。<?php两者之间的空白就是造成差距的原因。只要将两者粉碎在一起,它就会消失:


Postcode <input type="text" name="postcode" value="<?php

$postcode = str_replace('%20', ' ', $postcode);

$postcode = str_replace('-', ' ', $postcode);

$postcode = str_replace('%09', '', $postcode); // This line was added to try to remove 

the tabs

echo $postcode;

?>">

当渲染页面时,HTML 会自动将多个空白折叠为一个,但它会保留一个,这就是您所看到的。


我看不出有什么地方会出现额外的空白,但加上 a 也trim()没什么坏处:


Postcode <input type="text" name="postcode" value="

<?php

$postcode = str_replace('%20', ' ', $postcode);

$postcode = str_replace('-', ' ', $postcode);

$postcode = trim($postcode);

echo $postcode;

?>

">

trim()将从字符串的开头和结尾删除任何空格(以防万一用户意外添加了一些空格)。


查看完整回答
反对 回复 2023-10-21
?
白衣非少年

TA贡献1155条经验 获得超0个赞

问题是这部分代码中的换行符。


Postcode <input type="text" name="postcode" value="

    <?php

    $postcode = str_replace('%20', ' ', $postcode);

    $postcode = str_replace('-', ' ', $postcode);

    $postcode = str_replace('%09', '', $postcode);

    echo $postcode;

    ?>

">

要解决此问题,请通过将行str_replace放在HTML


$postcode = str_replace('%20', ' ', $postcode);

$postcode = str_replace('-', ' ', $postcode);

$postcode = str_replace('%09', '', $postcode);


Postcode <input type="text" name="postcode" value="<?php echo $postcode ?>">

我还建议将所有POST数据验证放在 之前HTML,这样做可以使您的代码看起来更干净。


查看完整回答
反对 回复 2023-10-21
?
浮云间

TA贡献1829条经验 获得超4个赞

这将在两侧插入 \n


Postcode <input type="text" name="postcode" value="

    <?php ...?>

    ">

这不会:


Postcode <input type="text" name="postcode" value="<?php 

...

?>">

为了确保,你可以这样做


Postcode <input type="text" name="postcode" 

value="<?=trim(str_replace('-', ' ', $postcode))?>">


查看完整回答
反对 回复 2023-10-21
  • 3 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信