编程语言
首页 > 编程语言> > Javascript / PHP重定向

Javascript / PHP重定向

作者:互联网

各位程序员大家好!

我正在开展个人项目(主要是学习php / javascript),并且在点击链接时遇到了重定向问题.我在我创建的标签页上有一些奇怪的情况,我认为这可能是导致我的问题的原因.

我正在尝试允许用户单击(由于css使其看起来与正常情况不同)将它们重定向到包含更多详细信息的新页面.我认为我页面上的第二个标签是什么让我失望,因为我有一个表格.

我已经尝试了很多不同的东西,比如window.location.href =“”,location.href =“”,document.location =“”等……但是总会发生同样的事情.我能够得到两条警报消息,所以我知道我正在进入我的JavaScript(即使我把它放入它自己的.js文件中).

无论如何建议/帮助将是非常有帮助的.此外,如果有人建议稍微清理此代码,这也将是真正有用的.

以下基本上就是我所拥有的.

在此先感谢您的帮助!

<html>
<head>
    <title>test site</title>
    <link rel="stylesheet" href="test.css" type="text/css" media="screen" />
    <script src="test.js" type="text/javascript"></script>
    <script type="text/javascript">
        function viewDetails(modelId){
            alert(modelId);
            window.location.href="new url?ModelID=" + modelId;
            alert('redirecting would be way awesome...');
        }
    </script>
</head>

<body onl oad="load()">

    <div id="tabbed_box_1" class="tabbed_box">
        <h4>Navigation Tabs<small>Select a tab</small></h4>
        <div class="tabbed_area">
            <?php
            mysql_connect('host','user','password');
            mysql_select_db("database");

            echo "<ul class='tabs'>";
                echo "<li><a href='javascript:tabSwitch(1, 2);' id='tab_1' class='active'>Inventory</a></li>";
                echo "<li><a href='javascript:tabSwitch(2, 2);' id='tab_2' >Add Project</a></li>";
            echo "</ul>";

            echo "<div id='content_1' class='content'>";
                echo "<ul>";
                    $modelsSQL = "SELECT * FROM Model ORDER BY Name";
                    $modelsResult = mysql_query($modelsSQL);
                    while ($modelRow = mysql_fetch_array($modelsResult)){
                        $modelID = $modelRow[0];
                        $sqlAvailCount = "SELECT * FROM Project WHERE ModelID = " . $modelID . " AND Sold = 0";
                        $sqlSoldCount =  "SELECT * FROM Project WHERE ModelID = " . $modelID . " AND Sold = 1";
                        $resultAvailCount = mysql_query($sqlAvailCount);
                        $resultSoldCount = mysql_query($sqlSoldCount);
                        $rowAvailCount = mysql_num_rows($resultAvailCount);
                        $rowSoldCount = mysql_num_rows($resultSoldCount);
                        echo "<li><a href='' onclick='javascript:viewDetails($modelID);'>" . $modelRow[1] . "<small>in stock: <value>" 
                        . $rowAvailCount . "</value> sold: <value>" . $rowSoldCount . "</value></small></a></li>";
                    }
                echo "</ul>";
            echo "</div>";

            echo "<div id='content_2' class='content'>";
                echo "<form action='project_insert.php' method='post' name='projectAddForm'>";
                    echo "<table cellpadding='5'>";

                        // Project Model Selection
                        echo "<tr><td>";
                            echo "<label for='model'>Model</label>";
                        echo "</td><td>";
                            echo "<select name='model' style='width: 250px;'>";
                            echo "<option value='-1' selected>SELECT</option>";
                            $modelListSQL = "SELECT * FROM Model ORDER BY Name";
                            $modelListResult = mysql_query($modelListSQL);
                            while ($modelListRow = mysql_fetch_array($modelListResult)){
                                echo "<option value='" . $modelListRow['ID'] . "'>" . $modelListRow['Name'] . "</option>";
                            }
                            echo "</select>";
                        echo "</td></tr>";

                        // Project Material Selection
                        echo "<tr><td>";
                            echo "<label for='material'>material</label>";
                        echo "</td><td>";
                            echo "<select name='material' style='width: 250px;'>";
                            echo "<option value='-1' selected>SELECT</option>";
                            $materialListSQL = "SELECT * FROM Material ORDER BY Name";
                            $materialListResult = mysql_query($materialListSQL);
                            while ($materialListRow = mysql_fetch_array($materialListResult)){
                                echo "<option value='" . $materialListRow['ID'] . "'>" . $materialListRow['Name'] . "</option>";
                            }
                            echo "</select>";
                        echo "</td></tr>";

                        // Project Finish Selection
                        echo "<tr><td>";
                            echo "<label for='finish'>finish</label>";
                        echo "</td><td>";
                            echo "<select name='finish' style='width: 250px;'>";
                            echo "<option value='-1' selected>SELECT</option>";
                            $finishListSQL = "SELECT * FROM Finish ORDER BY Name";
                            $finishListResult = mysql_query($finishListSQL);
                            while ($finishListRow = mysql_fetch_array($finishListResult))
                            {
                                echo "<option value='" . $finishListRow['ID'] . "'>" . $finishListRow['Name'] . "</option>";
                            }
                            echo "</select>";
                        echo "</td></tr>";

                        // Project Craftsman Selection
                        echo "<tr><td>";
                            echo "<label for='craftsman'>craftsman</label>";
                        echo "</td><td>";
                            echo "<select name='craftsman' style='width: 250px;'>";
                            echo "<option value='-1' selected>SELECT</option>";
                            $craftsmanListSQL = "SELECT * FROM Craftsman ORDER BY FirstName";
                            $craftsmanListResult = mysql_query($craftsmanListSQL);
                            while ($craftsmanListRow = mysql_fetch_array($craftsmanListResult)){
                                echo "<option value='" . $craftsmanListRow['ID'] . "'>" . $craftsmanListRow['FirstName'] . " " . $craftsmanListRow['LastName'] . "</option>";
                            }
                            echo "</select>";
                        echo "</td></tr>";

                        //Project Description
                        echo "<tr><td>";
                            echo "<label for='description'>Description</label>";
                        echo "</td><td>";
                            echo "<input type='text' name='description' id='textArea' style='width:250px'>";
                        echo "</td></tr>";

                        // Project Selling Price
                        echo "<tr><td>";
                            echo "<label for='price'>Price</label>";
                        echo "</td><td>";
                            echo "<input id='price' name='price' type='number' style='width:150px'>";
                        echo "</td></tr>";

                        // Project Completion Date
                        echo "<tr><td>";
                            echo "<label for='date'>Finish Date</label>";
                        echo "</td><td>";
                            $dateArray = getdate();
                            $month = $dateArray[mon];
                            $day = $dateArray[mday];

                            if ($month < 10){
                                $month = '0' . $dateArray[mon];
                            }
                            if ($day < 10){
                                $day = '0' . $dateArray[mday];
                            }

                            $todaysDate = $dateArray[year] . '-' . $month . '-' . $day;
                            echo "<input type='date' name='date' value='" . $todaysDate . "' style='width:150px'>";
                        echo "</td></tr>";

                        // Buttons
                        echo "<tr><td align='center'>";
                            echo "<input type='button' name='Save' value='Save' onclick='javascript:validateAndSubmit(this.form);' style='width:100px'>";
                        echo "</td><td align='center'>";
                            echo "<input type='button' name='Cancel' value='Cancel' onclick='javascript:cancelEntry();' style='width:100px'>";
                        echo "</td></tr>";

                    echo "</table>";
                echo "</form>";
            echo "</div>";

            ?>
        </div>
    </div>
</body>

解决方法:

window.location.href可能不会在某些浏览器和案例中触发重载.
您应该在之后添加重新加载

像这样:

window.location.href = '/foo/bar/';
window.locaton.reload(true)

但是,有些浏览器会延迟毫秒来执行location.href设置.在这种情况下,window.location.reload(true)可能在此之前完成.

因此,在重新加载时添加超时:

window.location.href = '/foo/bar/';
setTimeout('window.locaton.reload(true)', 500);

适用于所有浏览器

标签:php,javascript,html,http-redirect
来源: https://codeday.me/bug/20190703/1367920.html