单击按钮时执行操作php

时间:2016-01-15 10:45:40

标签: html css button

我尝试使用php创建一个表单,用户将在其中输入一些数据,然后我就可以将它们存储在我的数据库中。

到目前为止,我的代码是

<h1>Join Head Hunters <span class="colored-text">NOW</span>!</h1>
        <form class="sform" method="get">
            <input type="text" name="username" value="" placeholder="Username" method="get" maxlength="30">
            <input type="password" name="password" value="" placeholder="Password" method="get" maxlength="30">
            <input type="text" name="first_name" value="" placeholder="First name" method="get" maxlength="30">
            <input type="text" name="last_name" value="" placeholder="Last name" method="get" maxlength="30">
            <input type="text" name="address" value="" placeholder="Address" method="get" maxlength="80">
            <input type="text" name="phone" value="" placeholder="Phone" method="get" maxlength="60">
            <input type="text" name="mail" value="" placeholder="email" method="get" maxlength="40">
            <input type="text" name="prof" value="" placeholder="Profession" method="get">
            <input type="text" name="account" value="" placeholder="Bank Account" method="get">

            <select multiple id="studies" class="specialColor" method="get">
                <option value="highschool degree">Highschool Degree</option>
                <option value="bachelors degree">Bachelors Degree</option>
                <option value="MSc">MSc</option>
                <option value="PhD">PhD</option>
                <option value="MD">MD</option>
                <option value="EdD">EdD</option>
                <option value="JD">JD</option>
            </select>


            <select multiple="multiple" id="skillz" name="skillz[]" method="get">
                <option value="administering programs">Administering Programs</option>
                <option value="advising people">Advising people</option>
                <option value="analyzing data">Analyzing data</option>
                <option value="assembling apparatus">Assembling apparatus</option>
                <option value="auditing financial reports">Auditing financial reports</option>
                <option value="budgeting expenses">Budgeting expenses</option>
                <option value="calculating numerical data">Calculating numerical data</option>
                <option value="finding information">Finding information</option>
                <option value="handling complaints">Handling complaints</option>
                <option value="imagining new solutions">Imagining new solutions</option>
                <option value="interpreting languages">Interpreting languages</option>
                <option value="speaking to the public">Speaking to the public</option>
                <option value="writing letters/papers/proposals">Writing letters/papers/proposals</option>
                <option value="listening to others">Listening to others</option>
                <option value="deciding uses of money">Deciding uses of money</option>
                <option value="determining a problem">Determining a problem</option>
                <option value="setting work/committee goals">Setting work/committee goals</option>
                <option value="maintaining emotional control under stress">Maintaining emotional control under stress</option>
            </select>

            <select multiple="multiple" id="languages" name="languages[]" method="get">
                <option value="english">English</option>
                <option value="greek">Greek</option>
                <option value="german">German</option>
                <option value="japanese">Japanese</option>
                <option value="spanish">Spanish</option>
                <option value="italian">Italian</option>
                <option value="french">French</option>
                <option value="wookie">Wookie</option>
                <option value="klingon">Klingon</option>
                <option value="other">Other</option>
            </select>

            <input type="submit" class="button" name='create_account' value="Create Account"></input>

        </form>

和PHP的那个:

<?php
                $sservername = "localhost";
                $susername = "root";
                $spassword = "";
                $sdbname = "projectDB";
                mysql_connect($sservername, $susername, $spassword) or die("Cannot connect to server.");
                mysql_select_db($sdbname) or die("Cannot select DataBase.");



                if (isset($_POST["create_account"])) {
                    echo "<br><br><br><br><br>Button clicked";

                    //header("Location: signupsuccess.php");
                    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
                    //some error checking, now...
                    if (!preg_match($email_exp, $mail)) {
                        $error_message .= "<font color='red'>The Email address you entered does not appear to be valid.<br/></font>";
                        header("Refresh:0");
                        echo $error_message;
                    }
                }
            ?>

问题在于,无论我尝试多少次按下按钮,它都不会回复&#34;按钮点击&#34;消息,我无法弄清楚原因!

请记住,这是我对php和mysql的第一次尝试。有帮助吗?我用Google搜索太多了,我无法弄清楚我的错误。

1 个答案:

答案 0 :(得分:1)

<form class="sform" method="get">
if (isset($_POST["create_account"]))

GET表单会将数据放入查询字符串,而不是请求正文。 $_POST不会被填充。将方法设置为post

此外,目前您总是要输出Refresh 0标题,因此很可能即使您输出了HTML,页面也会立即刷新,您也不会看到它。