简体   繁体   中英

Can't redirect to another page after form submit

I have created a form and when the form is submitted it get the value of selected radio button and store in db, then I tried to redirect to another page but the result is that the current page is refreshed without update radio button value even if the value in the db is change, if I go to another page and return in the radio buttons page, the value are updates. How I can redirect to another page?

This is my code:

<?php foreach( $users as $user ): ?>
                <tr>
                    <td><?php echo $user["username"]; ?></td>
                    <td><?php echo $user["role"]; ?></td>
                    <td class="right"><a href="delete_permission.php?idTratta=<?php echo $idTratta; ?>&user=<?php echo $user["username"]; ?>">Elimina permessi utente</a></td>
                    <td class="right"><form action="" method="post">  <label>
    <input type="radio" name="<?php echo $user["username"]; ?>"  value="1" <?php if ($user["role"] == 'configuratore') echo 'checked="checked"'; ?>" />C</label>
  <label>
    <input type="radio" name="<?php echo $user["username"]; ?>"  value="2"<?php if ($user["role"] == 'visualizzatore avanzato') echo 'checked="checked"'; ?>" />VA</label>
  <label>
    <input type="radio" name="<?php echo $user["username"]; ?>"   value="3"<?php if ($user["role"] == 'visualizzatore') echo 'checked="checked"'; ?>" />V </label><input type= "submit" name="sub_<?php echo $user["username"]; ?>"value="Cambia"/></form></td>
                <?php
                   $sub='sub_';

                   if($_POST[$sub.''.$user["username"]]){
                   $permission=$_POST[$user["username"]];
                   $SimpleUsers->updateUserPermission($user["username"],$idTratta,$permission);
                   header('location: tratte.php' );
                   exit();
                  }
                 ?>

                </tr>
                <?php endforeach; ?>

PHP's header("Location:FILE_NAME.PHP") won't work if there is any output written from the script, even a single space.

In your case, you are printing HTML (output), so, redirection is not working.

Solutions:

1) Add ob_start() at the very start of the page. This will store your output to buffer and redirection will occur.

2) Use Javascript redirection. Javascript redirection will occur irrespective of whether you are outputting something on browser or not.

Code:

$SimpleUsers->updateUserPermission($user["username"],$idTratta,$permission);
echo "<script>window.location.href='tratte.php';</script>";
//header('location: tratte.php' );
exit();

In form you write like <input type="radio" name="<?php echo $user["username"]; ?>" and in PHP write like if($_POST[$sub.''.$user["username"]]){ the both name are different.

Replace <input type="radio" name="<?php echo $user["username"]; ?>" with <input type="radio" name="sub_<?php echo $user["username"]; ?>"

Thanks!

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