Joel Sprouse
Joel Sprouse

Reputation: 3

Set input value to get variable

I have two web pages.

index.php

This contains a form

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

when the form has submitted the action in search.php

search.php

This shows the same form with the inputted data from the previous page

<?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>
    function filterTags() {
        var input, filter, table, tr, td, i, txtValue;
        input = document.getElementById("tagsInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("searchTable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[2];
            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
    </script>
    <input type="submit" value="Submit">
</form>

However, when the input is shown in search.php, there are two tabs on either side of the input.

How do I get rid of this?

View screenshot of result here

Upvotes: 0

Views: 92

Answers (3)

mplungjan
mplungjan

Reputation: 177685

This will insert \n on both sides

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

This will not :

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

And to make sure, you can do this

Postcode <input type="text" name="postcode" 
value="<?=trim(str_replace('-', ' ', $postcode))?>">

Upvotes: 0

PeterSH
PeterSH

Reputation: 475

The problem is the line breaks in this part of your code.

Postcode <input type="text" name="postcode" value="
    <?php
    $postcode = str_replace('%20', ' ', $postcode);
    $postcode = str_replace('-', ' ', $postcode);
    $postcode = str_replace('%09', '', $postcode);
    echo $postcode;
    ?>
">

To solve this, remove all the line breaks by putting the str_replace lines before the 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 ?>">

I also recommend putting all your POST data validation before the HTML, doing this keeps your code looking a lot cleaner.

Upvotes: 0

samanime
samanime

Reputation: 26527

Funny enough, you are getting that error BECAUSE you are using 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;
?>
">

Notice how you have value=" and then you start the <?php on the next line. That white space between the two is what is causing your gap. Just smash those two together and it should go away:

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;
?>">

When a page is rendered, HTML will automatically collapse multiple whitespaces into one, but it'll keep one, which is what you're seeing.

I don't see a place where you'd end up with extra whitespace, but throwing on a trim() couldn't hurt:

Postcode <input type="text" name="postcode" value="
<?php
$postcode = str_replace('%20', ' ', $postcode);
$postcode = str_replace('-', ' ', $postcode);
$postcode = trim($postcode);
echo $postcode;
?>
">

trim() will trim any whitespace from the beginning and end of the string (just in case the user accidentally added some or something).

Upvotes: 1

Related Questions