4

Hello fellow programmers!

I'm working on a personal project (mainly to learn php/javascript) and have ran into an issue with redirection when clicking on a link. I have a bit of a strange situation on a tabbed page I've created and I think that may be what is causing my problem.

I'm trying to allow the user to click the (which due to css has made it look different than normal ) to redirect them to a new page with more details. I THINK that the second tag on my page is what is throwing me off because I have a form in it.

I have tried tons of different things like window.location.href="", location.href="", document.location="", etc... But the same thing always occurs. I am able to get both alert messages, so I know I am getting into my JavaScript (even when I put it into it's own .js file).

Anyway advice/help would be very helpful. Also, if anyone has a suggestion on cleaning this code up a bit, that would also be truly helpful.

Below is basically what I have.

Thanks in advance for your help!

<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 onload="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>

8
  • 1
    The mysql_* functions are no longer maintained and shouldn't be used in any new codebase. It is being phased out in favor of newer APIs. Instead you should use prepared statements with either PDO or MySQLi. Also, instead of attaching function onload you should be placing the script, that you want to run when page has been loaded, right before the closing </body> tag. Commented Sep 21, 2013 at 22:21
  • When testing are you getting any errors in the browser console? Commented Sep 21, 2013 at 22:30
  • terSKo: Thanks for the advice, I will look into that and start changing those over in the near future. Commented Sep 21, 2013 at 23:21
  • Aaran: unfortunately I have not found any errors when running it. I'm using Chromium to do my development, but haven't found anything there. I've also tested in FireFox and didn't find any errors. I haven't tried IE yet because I'm developing in Ubuntu at the moment. Commented Sep 21, 2013 at 23:23
  • 1
    Try changing this: onclick='javascript:viewDetails($modelID);' to this: onclick='viewDetails($modelID);'. You really shouldn't be using pseudo control anyway, but onclick='javascript: makes no sense and could be troublesome. Commented Sep 22, 2013 at 0:21

2 Answers 2

2

window.location.href may not trigger reload in some browsers and cases.. You should add a reload after

like this:

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

But, some browsers delay milliseconds to perform location.href set. In this cases the window.location.reload(true) may complete before this.

Therefore, add a timeout in reload:

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

works in all browsers for me

Sign up to request clarification or add additional context in comments.

1 Comment

Gustavo: Thanks for the advice. Unfortunately this does not seem to have resolved my problem as I have the same behavior occurring. It really seems to love my main page and not want to let me transfer anywhere.
0

Good morning! I found the cause of my problem this morning and have been able to resolve the issue. The problem I was causing is because I am using the tag (stylized by css) to display the information, with an onclick event to call my JS code to redirect. Within the tag I had the href='', thinking that the JS would override that functionality, it doesn't!

Removing the href='' from the tag resolved the issue and allowed me to redirect to the new page. A second solution is to use my php code to dynamically create the href link within the tag.

echo "<li><a onclick='viewDetails($modelID);'>" . $modelRow[1] . "<small>in stock: <value>" . $rowAvailCount . "</value> sold: <value>" . $rowSoldCount . "</value></small></a></li>";

OR

echo "<li><a href='inventorydetails.php?ModelID=" . $modelID . "'>" . $modelRow[1] . "<small>in stock: <value>" . $rowAvailCount . "</value> sold: <value>" . $rowSoldCount . "</value></small></a></li>";

I think I will go with the second example for two reasons. First, it provides the link icon when hovering (which I know I can add through css, but this is easier. Second, less JS code.

I thank you for all your help in resolving this!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.