设置输入值以获取变量

我有两个网页。


索引.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 中时,输入的两侧都有两个选项卡。


我该如何摆脱这个?

https://img1.sycdn.imooc.com/6533322b0001152f06520046.jpg

潇潇雨雨
浏览 90回答 3
3回答

莫回无

有趣的是,您收到该错误是因为您使用的是 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&nbsp;the tabsecho $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&nbsp;the tabsecho $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()将从字符串的开头和结尾删除任何空格(以防万一用户意外添加了一些空格)。

白衣非少年

问题是这部分代码中的换行符。Postcode <input type="text" name="postcode" value="&nbsp; &nbsp; <?php&nbsp; &nbsp; $postcode = str_replace('%20', ' ', $postcode);&nbsp; &nbsp; $postcode = str_replace('-', ' ', $postcode);&nbsp; &nbsp; $postcode = str_replace('%09', '', $postcode);&nbsp; &nbsp; echo $postcode;&nbsp; &nbsp; ?>">要解决此问题,请通过将行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,这样做可以使您的代码看起来更干净。

浮云间

这将在两侧插入 \nPostcode <input type="text" name="postcode" value="&nbsp; &nbsp; <?php ...?>&nbsp; &nbsp; ">这不会:Postcode <input type="text" name="postcode" value="<?php&nbsp;...?>">为了确保,你可以这样做Postcode <input type="text" name="postcode"&nbsp;value="<?=trim(str_replace('-', ' ', $postcode))?>">
打开App,查看更多内容
随时随地看视频慕课网APP