简体   繁体   中英

php/hmtl header() not redirecting to page with id in url

Situation: I have a member database overview where the user can click on an 'update member' link in a table that redirects to a page showing the data of clicked member. In the url it gets the paramater lid.php?lidnummer=4 (of any other id).

On that page is a form that allows the user to add a phone number to that member which goed through the create.php file after pressing the submit button. This page should redirect back to the member page lid.php?lidnummer=4 with the new phone number added.

I am passing the lidnummer through a hidden form field and I try to use this in the header() function.

header("location: ../lid.php?lidnummer=' . $lidnummer . '");

Only after the redirect the url shows: lid.php?lidnummer=%27%20.%205%20.%20%27 and I get a 'call to a member function' error on the page, because the page displays the member data through a function that grabs the lidnummer as parameter through $_GET after pressing the 'update member' button.

<?php
if(isset($_GET['lidnummer'])) 
{ 
    include 'includes/read.php'; 
    $lidnummer = $_GET['lidnummer'];
?>
<div class="contact-form">     
    <h3>Voeg contactgegevens toe:</h3>
    <form action="includes/create.php" method="POST"><b>
        <label for="telefoonnummer">
            Telefoonnummer:
            <input type="text" name="telefoonnummer">
        </label>
        <input type='hidden' name='lidnummer' value='<?php echo $lidnummer ?>'>
        <button type="submit" name='add_telnr'>Voeg telnr toe</button>
    </form><br>
    <form action="includes/create.php" method="POST">        
        <label for="email">
            Email:
            <input type="text" name="email">
        </label>
        <button type="submit" name='add_email'>Voeg email toe</button></b>
    </form><br>
</div>

<div>
    <h3>Lid:</h3>
    <table>
        <tbody>
            <tr>
                <td>#</td>
                <td>Info</td>
                <td>Pas aan</td>
                <td>Delete</td>
            </tr>
            <?php show_single_lid($conn, $lidnummer); ?>          
        </tbody>
    </table>
</div>
<?php } ?>

So I have 2 questions:

  1. How do I get the create.php file to redirect back to the lid.php page with the correct url parameter (lidnummer) after adding the phone number?
  2. How (if solving 1 doesn't fix this) do I then again pass the lidnummer to the lid.php page to show the data of that member again but updated?
header("Location: ../lid.php?lidnummer=$lidnummer");

With double quotes ( " ) you don't need to concatenate strings with the dot symbol.

The weird URL you got was the result of putting unnecessary single quotes, dots and spaces around it. If you look closer:

%27%20.%205%20.%20%27

The characters that cannot be written normally in a URL are encoded: %xx where xx is a number. So:

%27 = '

%20 = space

Which means, that the weird string resolves to ' . 5 . ' ' . 5 . ' ' . 5 . ' which is exactly what you put in the double quotes: ' . $lidnummer . ' ' . $lidnummer . '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM