Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Problema con redirect

  1. #1

    Problema con redirect

    questa è la pagina:
    Codice PHP:
    <table>
        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
            <tr>
                <td>Title:</td>
                <td>
                    <input type="text" name="title" value="guida ai css" id="txtTitle" style="width: 250px;" />
                </td>
            </tr>
            <script type="text/javascript">
                var campo = new LiveValidation('txtTitle', { validMessage: "\u221a" });
                campo.add(Validate.Presence);
            </script>
            <tr>
                <td>Author:</td>
                <td>
                    <select name="author">
                        <option value="*">*</option>
                        <?php
                        
    foreach (selectAuthor () as $rowAuthor) {
                            echo 
    "<option value='" $rowAuthor['author_id'] . "'>" $rowAuthor['author_name'] . "</option>";
                        }
                        
    ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Editor:</td>
                <td>
                    <select name="editor">
                        <option value="*">*</option>
                        <?php
                        
    foreach (selectEditor () as $rowEditor) {
                            echo 
    "<option value='" $rowEditor['editor_id'] . "'>" $rowEditor['editor_name'] . "</option>";
                        }
                        
    ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Price:</td>
                <td>
                    <input type="text" name="price" value="0" id="txtPrice" />
                </td>
            </tr>
            <script type="text/javascript">
                var num = new LiveValidation('txtPrice', { validMessage: "\u221a" });
                num.add(Validate.Numericality);
            </script>
            <tr>
                <td>Isbn:</td>
                <td>
                    <input type="text" name="isbn" value="" />
                </td>
            </tr>
            <tr>
                <td>
                    Note:
                </td>
                <td>
                    <textarea name="note" rows="6" cols="40">
                    </textarea>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="Submit" name="Submit" />
                </td>
            </tr>
        </form>
    </table>

    <?php
                        
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
                            if (
    $_POST['title'] == NULL) {
                                echo 
    'Insert a title';
                            } elseif (
    $_POST['author'] == "*") {
                                echo 
    'Insert an author';
                            } elseif (
    $_POST['editor'] == "*") {
                                echo 
    'Insert an editor';
                            } elseif (!
    is_numeric($_POST['price'])) {
                                echo 
    'Price is not a number';
                            } else {
                                try {
                                    
    $prepare $config->getPdo()->prepare("INSERT book (title, author_id, editor_id, price, isbn, note) VALUES (?,?,?,?,?,?)");
                                    
    $prepare->execute(array(
                                        
    strtolower(trim($_POST['title'])),
                                        
    strtolower(trim($_POST['author'])),
                                        
    strtolower(trim($_POST['editor'])),
                                        
    trim($_POST['price']),
                                        
    strtolower(trim($_POST['isbn'])),
                                        
    strtolower(trim($_POST['note']))
                                    ));
                                    
    header('location:[url]http://localhost/book/index.php[/url]');
                                } catch (
    Exception $e) {
                                    die(
    $e->getMessage());
                                }
                            }
                        }
    dopo il submit e la conclusione della query di insert dovrebbe fare il redirect in home page.
    invece ricarica la pagina e basta.
    sapete il motivo??

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2010
    Messaggi
    719
    Questa sintassi è completamente sbagliata...

    header('location:http://localhost/book/index.php');

    Intanto non si usa l'html nei redirict e poi (non ne sono sicuro) si dovrebbe mettere "Location" e non "location"

    header('Location: http://localhost/book/index.php');

    In oltre non puoi mettere un header dopo che hai iniziato l'output, quindi ho metti il codice che controlla se stai usando il post come metodo all'inizio (soluzione più semplice) o metti le funzioni di "Output Control" (soluzione più difficile).


  3. #3
    no la sintassi nn è quella.
    è come hai scritto te, è il forum che l'ha messa così.

    cmq ho messo tutto sopra, tanto va bene lo stesso.

    però avrei una domanda:
    Codice PHP:
    <table>
        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
            <tbody>
                <tr>
                    <td>User:</td>
                    <td><input type="text" name="username" value="" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" value="" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit" name="submit" /></td>
                </tr>
        </form>
    </table>

    <?php
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
        
    $utente_loggato $user;
        try {
            
    $query $config->getPdo()->prepare("SELECT * FROM user WHERE user_name='" trim(sha1($_POST['username'])) . "' AND user_pass='" trim(sha1($_POST['password'])) . "'");
            
    $query->execute();
            if (
    $query->rowCount() == 1) {
                
    $_SESSION['login_eseguito'] = TRUE;
                
    $_SESSION['utente_loggato'] = $utente_loggato;
                
    header('location:[url]http://localhost/book/index.php[/url]');
                return 
    TRUE;
            } else {
                return 
    FALSE;
            }
        } catch (
    PDOException $e) {
            
    $error $e->getMessage() . "
    "
    ;
            return 
    $error;
            die();
        }
    }
    questo funziona.
    sai dirmii come mai??

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.