PHP表单HTML按钮


php form html button

我有一个网站,你可以选择一个网站,你想被重定向到一个下拉菜单&然后单击一个按钮将被重定向到它。

虽然我很聪明,但我的电脑上有病毒,& &;从旧备份恢复。短篇小说;这个网站已经不能用了。下面是代码:

HTML:

   <form method="post" name="form1" id="form1" action="process.php">
       <select name="taskOption" id="taskOption2">
        <option value="Select">Please select a site</option>
        <option value="http://www.Itslearning.com">Itslearning</option>
        <option value="http://www.NDLA.no">NDLA</option>
       </select>
    </form>
    <button onclick="FormSubmit()" class="button button1 button1:hover">Take me there</button>
PHP:

<?php
$taskOption $_POST["taskOption"];
if ($taskOption) {
    header("Location: $taskOption");
}
else {
   echo "Venligst velg en side.";
   exit;
}

?>
JAVASCRIPT:

    function FormSubmit() {
document.form1.submit();
}

您收到的错误是什么?PHP文件中至少有一个错误:

<?php
$taskOption = $_POST["taskOption"];
if ($taskOption) {
    header("Location: $taskOption");
} else {
   echo "Venligst velg en side.";
   exit;
}
?>

注意第二行中的"="。

首先,看起来您在PHP中缺少一个赋值操作符(=)。

应该是这样的:

<?php
$taskOption = $_POST["taskOption"];
if ($taskOption) {
    header("Location: $taskOption");
}
else {
   echo "Venligst velg en side.";
   exit;
}
?>
Simple table to store urls:
create table `urls` (
    `id` int(10) unsigned not null auto_increment,
    `url` varchar(255) not null default '0',
    `hash` varchar(16) not null default '0',
    `hits` smallint(5) unsigned not null default '0',
    primary key (`id`),
    unique index `hash` (`hash`)
)
engine=innodb;

Gives this structure:
+-------+----------------------+------+-----+---------+----------------+
| Field | Type                 | Null | Key | Default | Extra          |
+-------+----------------------+------+-----+---------+----------------+
| id    | int(10) unsigned     | NO   | PRI | NULL    | auto_increment |
| url   | varchar(255)         | NO   |     | 0       |                |
| hash  | varchar(16)          | NO   | UNI | 0       |                |
| hits  | smallint(5) unsigned | NO   |     | 0       |                |
+-------+----------------------+------+-----+---------+----------------+

Populated with dummy urls & unique hashes:
+----+--------------------------------+------------------+------+
| id | url                            | hash             | hits |
+----+--------------------------------+------------------+------+
|  1 | http://www.example.com/page/1  | 53abc566010de29a |   45 |
|  2 | http://www.example.com/page/2  | 8664d7fca34963d2 |   83 |
|  3 | http://www.example.com/page/3  | fe06dca79d3d0415 |   49 |
|  4 | http://www.example.com/page/4  | 3913aaaef701ecad |   35 |
|  5 | http://www.example.com/page/5  | eb2eddc3ca2406c3 |   93 |
|  6 | http://www.example.com/page/6  | acc809b96c6a42d9 |   50 |
|  7 | http://www.example.com/page/7  | 63a4e53b1b374fcb |   90 |
|  8 | http://www.example.com/page/8  | d9c13a146fc7c69a |   18 |
|  9 | http://www.example.com/page/9  | eaa944c7e9a4ef7c |   76 |
| 10 | http://www.example.com/page/10 | 59f9d294a29601c9 |   13 |
+----+--------------------------------+------------------+------+

In the php page that displays the menu for the user to choose from
<?php
    $dbhost =   'localhost';
    $dbuser =   'xxx';
    $dbpwd  =   'xxx';
    $dbname =   'xxx';
    $db =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
    $sql='select * from `urls` order by `id`;';
    $result=$db->query( $sql );
    if( $result ){
        $html=array();
        $html[]="<form name='launcher' method='post' action='process.php'>";
        $html[]="<select name='taskOption'>";   
        while( $rs=$result->fetch_object() ){
            $html[]="<option value='{$rs->hash}'>{$rs->url}";
        }
        $result->close();
        $db->close();
        $html[]="</select>";
        $html[]="<input type='submit' value='Go' />";
        $html[]="</form>";
        echo implode( PHP_EOL, $html );
    }

?>


<?php
    /* process.php */
    /*
        Rather than sending the actual URL via POST we only send a hash
        which is then used in the sql to find the real url from the database.
    */
    $errors=array();
    $url=false;
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['taskOption'] ) ){
        /* rudimentary sanitisation of the string */
        $hash = filter_input( INPUT_POST, 'taskOption', FILTER_SANITIZE_STRING );
        /* db credentials */
        $dbhost =   'localhost';
        $dbuser =   'xxx';
        $dbpwd  =   'xxx';
        $dbname =   'xxx';
        $db =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
        /* construct statement to find the url */
        $sql='select `url` from `urls` where `hash`=?';
        $stmt=$db->prepare( $sql );
        if( $stmt ){
            /* bind the variable - hash */
            $stmt->bind_param( 's', $hash );
            $result=$stmt->execute();
            /* */
            if( $result ){
                $stmt->store_result();
                $stmt->bind_result( $url );
                $stmt->fetch();
                $stmt->free_result();
                if( !$url ) $errors[]='Unable to locate url';
                $sql='update `urls` set `hits`=`hits`+1 where `hash`=?';
                $stmt=$db->prepare( $sql );
                if( $stmt ){
                    $stmt->bind_param('s',$hash);
                    $stmt->execute();
                    $stmt->free_result();
                } else {
                    $errors[]='sql error updating hit count';
                }
            } else {
                $errors[]='No result found in database';
            }
        } else {
            $errors[]='sql error whilst preparing initial statement';
        }
        $stmt->close();
        $db->close();


        /* Redirect the user if all went well otherwise show an error message */
        if( empty( $errors ) ) exit( header("Location: $url") );
        else exit( "There were errors processing your request." );
    }
    /* only accept POST requests */
    exit( 'Bad foo - wrong method' );
?>