如何根据php if语句的结果运行javascript

时间:2014-03-09 12:01:58

标签: javascript php html forms if-statement

好的,当我自己测试你的解决方案时,它可以很好地工作,但作为完整代码的一部分,它仍然无法正常工作。首先是在没有出现提示框的情况下运行,并且在尝试某些解决方案时它说我在第一行收到预期的表达式错误。任何人都可以帮助解决这个问题吗? 提前谢谢。

    <?php      
$firstname = $_POST['firstname'];   
$surname = $_POST['surname'];
$housenum = $_POST['housenumber'];
$contactnum = $_POST['contactnumber'];
$email = $_POST['email'];
$city = $_POST['city'];
$county = $_POST['county'];
$username = $_POST['username'];
$password = $_POST['password'];
$repeatpassword = $_POST['passwordrepeat'];
$postcode = $_POST['postcode'];
$uniid = $_POST['uniid'];
$usertype = $_POST['usertype'];

if ($password == $repeatpassword) {
    if (strlen("$password") >= 8)   {
        // when all tests are run queries etc can replace these comments.
        if (strlen("$postcode") >= 6 && strlen("$postcode") <= 7)   {
            if (strlen("$uniid") == 9)   {
                if (strlen("$firstname") != 0 && strlen("$surname") != 0)   {
                    if (strlen("$email") != 0)  {
                        if (strlen ("$city") != 0)  {
                            if (strlen ("$county") != 0)    {
                                if (strlen("$contactnum") == 11)    {
                                    // collects input from user from the signup form using $_POST method.
                                    // inserts data into the respective column in table userinfo in the mysql database systemone.
                                    // connect to database

                                    $con=mysqli_connect("localhost:3306","root","123paulrivers","systemone");

                                    // Check connection

                                    if (mysqli_connect_errno())
                                    {

                                    // if connection to database fails. States error.

                                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                                    }
                                    if ($usertype == "Administration")  {
                                ?>
                                <body onLoad = "promptMessage();">    
                                    <script type='text/javascript'>
                                            function promptMessage() {

                                                var x = 38773;

                                                var code = prompt('Enter the administration code you have been given:', 'Enter code here');

                                                    if (code == x) {
                                                        alert('Admin code accepted');
                                                    } else {
                                                        prompt('The code you hae entered is inccorect', 'Enter code here or change Usertype');
                                                        }
                                        }   
                                       </script>
                                 </body>
                                 <?php

                                 }

                                     $sql="INSERT INTO completeinfo (FirstName, Surname, UniID, HouseNumber, AddressLineOne, AddressLineTwo, City, PostCode, County, PhoneNumber, Email, Username, Password, UserType)
                                     VALUES
                                     ('$_POST[firstname]','$_POST[surname]','$_POST[uniid]','$_POST[housenumber]','$_POST[addresslineone]','$_POST[addresslinetwo]','$_POST[city]','$_POST[postcode]','$_POST[county]','$_POST[contactnumber]','$_POST[email]','$_POST[username]','$_POST[password]','$_POST[usertype]')";

                                     if (!mysqli_query($con,$sql))   {
                                       die('Error: ' . mysqli_error($con));
                                       }   
                                       else  {

                                    // collects input from user from the signup form using $_POST method.
                                    // inserts data into the respective column in table account in the mysql database systemone.
                                    // SHA1 is an encryption for all passwords entered to improve security.

                                    // states if there was an error or record was added successfully.

                                    // add if clause checking to see the user type if pg perform $postgrad, if lecturer perform $lecturer. 

                                    if ($usertype == "Post Graduate")   {
                                    $postgrad = "INSERT INTO postgraduate (FirstName, Surname, UniID, Username) SELECT Firstname, Surname, UniID, Username FROM completeinfo WHERE UserType = 'Post Graduate'";
                                    if (!mysqli_query($con,$postgrad))   {
                                       die('Error: ' . mysqli_error($con));
                                       }   
                                       else  {
                                        header("Location:SignUpComplete.html");
                                       }
                                    }

                                            if ($usertype == "Lecturer")    {
                                            $lecturer = "INSERT INTO lecturer (FirstName, Surname, UniID, Username) SELECT Firstname, Surname, UniID, Username FROM completeinfo WHERE UserType = 'Lecturer'";
                                            if (!mysqli_query($con,$lecturer))   {
                                       die('Error: ' . mysqli_error($con));
                                       }   
                                       else  {
                                            header("Location:SignUpComplete.html");
                                            }
                                        }    
                                       }
                                    header("Location:SignUpComplete.html");    

                                } else  {
                                    header("Location:ContactNumberFail.html");
                                }
                            } else  {
                                header("Location:CountyFail.html");
                            }    
                        } else  {
                            header("Location:CityFail.html");
                        }     
                    } else  {
                        header("Location:EmailFail.html");
                    }                         
                } else  {
                    header("Location:NameFail.html");   
                }
            } else  {
                header("Location:UniIDFail.html");
            }
        } else    {
            header("Location:PostCodeFail.html");
        }
    } else  {
        header("Location:PasswordLengthFail.html");
     }
} else    {
        header("Location:PasswordMatchFail.html");
    }

    ?> 

上面是PHP,下面是HTML表单,它将信息发送给PHP。

    <body>
     <!--Form sends information entered by the user to userinfo.php
        for processing. Each name value is the variable which is called
        in the php doc.-->
    <form action="StoreUserInfo.php" method="post">
        User Type: <select name="usertype">
                       <option>Lecturer</option>
                       <option>Post Graduate</option>
                       <option>Administration</option>
                   </select><br />
        First Name: <input type="text" name="firstname"/><br />
        Surname: <input type="text" name="surname"/><br />
        Uni ID: <input type="number" name="uniid" /><br />
        House Number: <input type="number" name="housenumber"/><br />
        Address Line One: <input type="text" name="addresslineone"/><br />
        Address Line Two: <input type="text" name="addresslinetwo"/><br />
        City: <input type="text" name="city"/><br />
        Post Code: <input type="text" name="postcode"/><br />
        County: <input type="text" name="county"/><br />
        Contact Number: <input type="number" name="contactnumber"/><br />
        Email: <input type="email" name="email"/><br />
        Username: <input type="text" name="username" /><br />
        Password: <input type="password" name="password" /><br />
        Re-enter Password: <input type="password" name="passwordrepeat" /><br />
        <input type="submit" value="Submit"/>
    </form>
  </body>

1 个答案:

答案 0 :(得分:1)

  • 缺少引文

    <body onLoad = "promptMessage();">

以下是重做的整个代码:

<?
if ($usertype == "Administration")  {
                                    ?>
                                    <body onLoad = "promptMessage();">    
                                        <script type='text/javascript'>
                                                function promptMessage() {

                                                    var x = 38773;

                                                    var code = prompt('Enter the administration code you have been given:', 'Enter code here');

                                                        if (code == x) {
                                                            alert('Admin code accepted');
                                                        } else {
                                                            prompt('The code you hae entered is inccorect', 'Enter code here or change Usertype');
                                                            }
                                            }   
                                           </script>
                                     </body>

你应该用分号结束你的javascript语句; 例如:

`var x = 38773;`

和 像你一样加载javascript:

<body onLoad = "promptMessage();">

被认为是不好的做法。

你可以尝试考虑使用类似的东西:

<script type="text/javascript">
    window.onload = promptMessage;
</script>