Javascript:未捕获的TypeError

时间:2017-01-12 11:37:50

标签: javascript php

    <script>

    function more(){
        var MoreDetails = document.getElementById('MoreDetails');

        MoreDetails.style.display ="block";
    }

    function same(){
        var SameProduct = document.getElementById('SameProduct');

        SameProduct.style.display = "block";
    }
    </script>

    <!DOCTYPE HTML>
    <?php
        $con = mysqli_connect('localhost', 'root', '', 'test') or die("Cannot connect to datbase");
    //  $seller = $_SESSION['sellMail'];
    ?>
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' });</script> 

    <html>
    <head>

</head>
<body>

    <div id = "ProductForm">
        <form id="product" method="POST" enctype = "multipart/form-data" novalidate>
            <table align="center" width="750" border ="2">
                <tr align="center">
                    <td colspan="7"><h2>Insert Product</h2></td>
                </tr>

                <tr>
                    <td align="right"><b>Type of Product: </b></td>
                    <td>
                        <input list="text" name="type" placeholder = "Select a type" required>
                        <datalist id="text">
                            <?php
                                $run = mysqli_query($con, "Select * from typeproduct");
                                while($row = mysqli_fetch_array($run)){
                                    $type_title = $row['type_title'];
                                    echo "<option>$type_title</option>";
                                }
                            ?>
                        </datalist>
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>Product Category: </b></td>
                    <td>
                        <select name = "prd_cat"  required>
                            <option>Select a Category</option>
                            <?php
                                $get_cat = "Select * from categories";
                                $run_cat = mysqli_query($con, $get_cat);
                                while($row = mysqli_fetch_array($run_cat)){
                                    $cat_id = $row['cat_id'];
                                    $cat_name = $row['cat_name'];

                                    echo "<option>$cat_name</option>";
                                }
                            ?>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>Product Brand: </b></td>
                    <td>
                    <select name = "prd_brand" required>
                        <option>Select a Brand</option>
                            <?php
                                $get_brn = "Select * from brands";
                                $run_brn = mysqli_query($con, $get_brn);
                                while($row = mysqli_fetch_array($run_brn)){
                                    $brn_id = $row['brand_id'];
                                    $brn_name = $row['brand_title'];

                                    echo "<option>$brn_name</option>";
                                }
                            ?>
                        </select>


                    </td>
                </tr>

                <tr align="center">
                    <td><input type="submit" name="prdDetails" value="Insert Product"></td>
                </tr>
            </table>

        </form> 
        </div>      


                        <?php

                        if(isset($_POST['prdDetails'])){
                            $brand = $_POST['prd_brand'];
                            $type = $_POST['type'];
                            $cat = $_POST['prd_cat'];

                            $select = mysqli_query($con, "Select * from products where type = '$type' AND cat='$cat' AND brand = '$brand'");
                            if(mysqli_num_rows($select)==0){
                                echo "<script>more();</script>";
                            }else{
                                echo "<script>same();</script>";
                                $i=1;
                            ?>
                        <table id="SameProduct" style="display:none">
                            <tr>
                                <td>S.No</td>
                                <td>Product Image</td>
                                <td>Product Title</td>
                                <td>Product Description</td>
                                <td>Same Product</td>
                            </tr>

                            <?php
                            while($row = mysqli_fetch_array($select)){
                                    $img = $row['img'];
                                    $title = $row['title'];
                                    $desc = $row['p_desc'];
                                ?>
                                <tr>
                                    <td><?php echo $i;?></td>
                                    <td><img src="Product_image/<?php echo $img;?>"</td>
                                    <td><?php echo $title;?></td>
                                    <td><?php echo $desc;?></td>
                                    <td><button>Got the Same Product</button></td>
                                </tr>
                            <?php
                                $i++;
                            }

                            ?>
                            <tr>
                                <td><button>Product Not in list</button></td>
                            </tr>
                            </table>

                            <?php   
                            }

                            ?>

                            <?php

                        }                           
                            ?>



            <form method="POST" id="MoreDetails" enctype="multipart/form-data"> 
                <table style="display:none">

                <tr>
                    <td align="right"><b>Product Image: </b></td>
                    <td><input type="file" name="prd_img" required/></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Price: </b></td>
                    <td><input type="text" name="prd_price" required/></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Description: </b></td>
                    <td><textarea name="prd_desc" cols = "20" rows="10" required></textarea></td>
                </tr>

                <tr>
                    <td align="right"><b>Product Keywords: </b></td>
                    <td><input type="text" name="prd_keyword" required/></td>
                </tr>
                <tr align="center">
                    <td colspan="7"><input type="submit" name="insertPrd" id="insertPrd" value="Insert Now"/></td>
                </tr>
                </table></form>



</body></html>

<?php

    if(isset($_POST['insertPrd'])){
        $title = $_POST['prd_title'];
        $categ = $_POST['prd_cat'];
        $brand = $_POST['prd_brand'];
        $price = $_POST['prd_price'];
        $desc = $_POST['prd_desc'];
        $keywords = $_POST['prd_keyword'];

        $img = $_FILES['prd_img']['name'];
        $img_temp = $_FILES['prd_img']['tmp_name']; 

        move_uploaded_file($img_temp, "Product_image/$img");

        $insert_prd = "INSERT INTO products(cat, brand, title, price, p_desc, img, keyword, seller) VALUES ('$categ', '$brand', '$title', '$price', '$desc', '$img', '$keywords', '$seller')";

        $insert = mysqli_query($con, $insert_prd) or die("Cannot Insert");

        if($insert){
            echo "<script>alert('Product has been added');</script>";
            echo "<script>window.open('Seller.php','_self')</script>";
        }else{
            echo "<script>alert('Sorry! The Product cannot be added');</script>";
            echo "<script>window.open('Seller.php','_self')</script>";
        }
    }

?>

每当我点击prdDetails按钮时,就会出现错误 “未捕获的TypeError:无法读取'null'的属性样式”,无论是调用more()还是调用same()。 我目前正在研究XAMPP服务器。 除此之外一切正常。

2 个答案:

答案 0 :(得分:1)

您对more()和same()的调用发生在它们在DOM中引用的元素之前。

将more()和same()的调用移到元素MoreDetails和SameProduct之后的某个地方。

答案 1 :(得分:0)

这是因为document.getElementById('MoreDetails');没有收到DOM元素,变量MoreDetailsnull

您应该在ID为more()的DOM elemenet之后调用函数MoreDetails,但之前调用它,这样就无法获取此DOM元素。