如何让PHP在PHP中工作

时间:2013-08-06 09:57:30

标签: php html mysqli

我试图让这个PHP代码与我的html一起使用。 PHP单独工作但是当我试图使用html表单并使用PHP代码时它根本不做任何事情。我一直试图解决这个问题,但没有任何成功。有人可以解释一下我做错了吗?


    <?php

include_once("config.php");  
if(isset($_POST['submit'])){
       //Perform the verification

       $uname = $_POST['uname'];
       $email = $_POST['email'];
       $email2 = $_POST['email2'];
       $pass = $_POST['pass'];
       $pass2 = $_POST['pass2'];

       if($email == $email2){
        if($pass == $pass2){
            //All good. Carry on.

        $query = $sql->prepare("INSERT INTO login (uname, email, pass) VALUES (?, ?, ?)");
        $query->bind_param('sss', $uname, $email, $pass );
        $query -> execute();
        header("Location: index.html");

        }else{
            echo "Passwords do not match.<br />";
            exit();
            }
        }else{
            echo "Emails do not match.<br /><br />";
            exit();
            }
    $sql->close();
    $query->close();
        }
?>



<html>

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </head>
    <body>
        <div class="container">


            <section>               
                <div id="container_demo" >
                    <a class="hiddenanchor" id="tologin"></a>
                    <div id="wrapper">


                        <div id="login" class="animate form">
                            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                                <h1> Sign up </h1> 
                                <p> 
                                    <label for="usernamesignup" class="uname" data-icon="u">Username</label>
                                    <input id="usernamesignup" name="usernamesignup" required="required" type="text"/>
                                </p>
                                <p> 
                                    <label for="emailsignup" class="email" data-icon="e"> Your email</label>
                                    <input id="emailsignup" name="emailsignup" required="required" type="email"/> 
                                </p>
                                 <p> 
                                    <label for="emailsignup" class="email2" data-icon="e">Confirm your email</label>
                                    <input id="emailsignup" name="emailsignup" required="required" type="email"/> 
                                </p>
                                <p> 
                                    <label for="passwordsignup" class="pass" data-icon="p">Your password </label>
                                    <input id="passwordsignup" name="passwordsignup" required="required" type="password"/>
                                </p>
                                <p> 
                                    <label for="passwordsignup_confirm" class="pass2" data-icon="p">Please confirm your password </label>
                                    <input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" type="password"/>
                                </p>

                                <p class="signin button"> 
                                    <input type="submit" value="Sign up"/> 
                                </p>
                                <p class="change_link">  
                                    Already a member ?
                                    <a href="index.html" class="to_register"> Go and log in </a>
                                </p>
                            </form>
                        </div>

                    </div>
                </div>  
            </section>
        </div>
    </body>
</html>

4 个答案:

答案 0 :(得分:2)

您的问题在这里,您正在检查是否有名为submit的帖子变量:

if(isset($_POST['submit'])){

但是您的表单没有任何名为submit的输入。我建议改为:

if($_SERVER['REQUEST_METHOD'] == 'POST'){

检查REQUEST_METHOD更好,因为它不依赖于帖子数据中的提交按钮。

答案 1 :(得分:0)

您应该将name="submit"属性添加到提交按钮。否则永远不会设置$_POST['submit'],也不会发生任何事情。

答案 2 :(得分:0)

您没有为提交按钮指定名称。

<input type="submit" value="Sign up"/> 

如果没有名称,input的值将不会发送到服务器。基本上提交表单会找到if(isset(并跳过整个代码。

答案 3 :(得分:0)

额外提示:你的HTML文本框名称与$ _POST变量不匹配,我举一个简短的例子:<input type="text" name="usernamesignup" />应该是$ _POST [“usernamesignup”],而不是$ _POST [“uname”]。