Javascript / PHP Redirect

Hi fellow programmers!

I am working on a personal project (mainly for learning php / javascript) and ran into a redirect problem when clicking on a link. I have a slightly strange situation on the tab that I created, and I think this may be the cause of my problem.

I am trying to allow the user to click (which is because css made it look like a normal one) in order to redirect them to a new page with more detailed information. I THINK that the second tag on my page is what pushes me away because I have a form.

I tried many different things like window.location.href = ", location.href =" ", document.location =" "etc. But the same thing always happens. I can get both warnings, so I know that get into my JavaScript (even when I put it in my own .js file).

In any case, advice / help will be very helpful. Also, if anyone has a suggestion for cleaning up this code, it will also be really 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> 

+4
source share
2 answers

window.location.href cannot start a reboot in some browsers and cases. You must add a reboot after

like this:

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

But some browsers delay milliseconds to complete the installation of location.href. In this case, window.location.reload (true) may complete before this.

So add a timeout to reload:

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

works in all browsers for me

+2
source

Good morning! This morning I found the cause of my problem and was able to solve the problem. The problem I caused is that I use a tag (stylized css) to display information, with an onclick event, to trigger my JS code for redirection. I had href = '' in the tag, believing that JS will override this functionality, but it is not!

Removing href = '' from the tag resolved the issue and allowed me to redirect to a new page. The second solution is to use my PHP code to dynamically create the href link inside 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. Firstly, it provides a link icon on hover (which, as I know, I can add via css, but this is simpler. Secondly, there is less JS code.

I thank you for your help in resolving this!

0
source

All Articles