BelowZero Posted March 4, 2012 Share Posted March 4, 2012 I want to display some specific data from my database (just data from a given date). I first created a page where the user picks the date. After the date is defined it is passed to the next page where the data from that date is displayed. That page includes a form where users can edit the data. The form data is then passed to a third page for processing. All of this works fine. Now I want the user to be taken back to the previous page with the assigned date. On the processing page I am using this statement: header("Location: /teetimes/adminteetimes.php?teetimedate=$dateID"); and on the display page I'm using $dateID = $_REQUEST[teetimedate]; The display page is using a SELECT...WHERE Date='$dateID' statement and $dateID is defined in the processing page. Problem is: I'm redirected from the processing page back to the display page but the date isn't passed, so the correct data isn't being displayed. Can anyone help me pass a variable using a header?? Or show me where my logic is lacking?? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/ Share on other sites More sharing options...
l0gic Posted March 4, 2012 Share Posted March 4, 2012 Have you thought about using a session variable instead? Have a look: http://www.php.net/manual/en/session.examples.basic.php Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323919 Share on other sites More sharing options...
BelowZero Posted March 5, 2012 Author Share Posted March 5, 2012 I'm still learning and am not familiar with Sessions yet. I will look into it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323931 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2012 Share Posted March 5, 2012 There's nothing technically wrong with what you tried. What have you done to troubleshoot exactly at what point you have the expected data and at what point you don't? Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323936 Share on other sites More sharing options...
BelowZero Posted March 5, 2012 Author Share Posted March 5, 2012 I've tested the variable and got the correct result all the way until it's supposed to get passed. After the new page loads the variable is blank. Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323954 Share on other sites More sharing options...
l0gic Posted March 5, 2012 Share Posted March 5, 2012 $dateID = $_REQUEST[teetimedate]; I believe it should be: $dateID = $_REQUEST['teetimedate']; Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323956 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2012 Share Posted March 5, 2012 You likely have some code in adminteetimes.php that is clearing the value, rather than comparing the value. You would need to post enough of your code (from both pages) that reproduces the problem for anyone here to directly help. You might also have a redirect in adminteetimes.php that could be causing this or you might have some url rewriting that could be causing this (do any $_GET values work?) @l0gic, while it is true that the associative array index names are most often strings and should be quoted, php code works without quotes (produces a nasty error concerning an undefined constant and an assumption by php that you meant to put quotes around the name.) Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323968 Share on other sites More sharing options...
BelowZero Posted March 5, 2012 Author Share Posted March 5, 2012 Thanks for the reply. Here is the complete code for the display/form page: <FORM Method = "POST" action ="update_tee_times.php"> <?php include("opendatabase.php"); //$dateID = $_POST[teetimedate]; $dateID = $_REQUEST[teetimedate]; echo $dateID; $closetime = "15:30:00"; $opentime = "17:30:00"; $result = mysql_query(" SELECT Date, Player1,Player2,Player3,Player4,Player5,Time, IF( TIME(Time) BETWEEN '$closetime' AND '$opentime','FFFF66','FFFFFF') AS color,Men,Women,Guest FROM Daysheets WHERE Date ='$dateID'"); echo "<h1>"; echo date("l: F d, Y", strtotime($dateID)); echo "</h1>"; echo "<table align=center width=700 border=1>\n"; echo "<th>Time</th><th>Player 1</th><th>C</th><th>Player 2</th><th>C</th><th>Player 3</th><th>C</th><th>Player 4</th><th>C</th><th>Player 5</th><th>C</th><th>M</th><th>W</th><th>G</th><th>Clear</th>"; $i=0; while($row = mysql_fetch_array( $result )) { $dateID = $row['Date']; $timeID = $row['Time']; echo "<tr>"; echo "<td>"; echo date("g:i ", strtotime($row[Time])); echo "</td><td>"; echo "<input type='text' size='16' value= '$row[Player1]' name='player1[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[C1]' name='c1[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='16' value= '$row[Player2]' name='player2[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[C2]' name='c2[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='16' value= '$row[Player3]' name='player3[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[C3]' name='c3[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='16' value= '$row[Player4]' name='player4[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[C4]' name='c4[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='16' value= '$row[Player5]' name='player5[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[C5]' name='c5[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[Men]' name='men[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[Women]' name='women[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='text' size='1' value= '$row[Guest]' name='guests[$i]' style ='font-size: 11px; background-color:#{$row['color']};'>"; echo "</td><td>"; echo "<input type='checkbox' name='time1[$i]' value='$timeID'>"; echo "<input type='hidden' name='date[$i]' value='$dateID'>"; echo "<input type='hidden' name='time2[$i]' value='$timeID'>"; echo "</td></tr>"; $i++; } echo "</table>"; mysql_close($con); ?> <br /><br /> <input type="Submit" name="Submit" value="Update Tee Times"> </FORM> And for the processing page: <?php header("Location: /teetimes/adminteetimes.php?teetimedate=$dateID"); include("opendatabase.php"); $size = count($_POST['player1']); $i=0; while ($i < $size) { $dateID = $_POST['date'][$i]; $timeID1 = $_POST['time1'][$i]; $timeID2 = $_POST['time2'][$i]; $player1 = ($_POST['player1'][$i]); $player2 = ($_POST['player2'][$i]); $player3 = ($_POST['player3'][$i]); $player4 = ($_POST['player4'][$i]); $player5 = ($_POST['player5'][$i]); $men = ($_POST['men'][$i]); $women = ($_POST['women'][$i]); $guest = ($_POST['guests'][$i]); if (empty($timeID1)) { mysql_query(" UPDATE Daysheets SET Player1='$player1', Player2='$player2', Player3='$player3', Player4='$player4', Player5='$player5', Men='$men', Women='$women', Guest='$guest' WHERE Date ='$dateID' AND Time ='$timeID2' "); $i++; } else { mysql_query(" UPDATE Daysheets SET Player1='', Player2='', Player3='', Player4='', Player5='', Men='0', Women='0', Guest='0' WHERE Date ='$dateID' AND Time ='$timeID2' "); $i++; } } mysql_close($con) ?> Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323972 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2012 Share Posted March 5, 2012 And for the processing page:... You are executing the header() redirect as the first thing in the file. $dateID does NOT have any value in it at that time because you are not assigning a value to $dateID until about line 13 in that code. Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323975 Share on other sites More sharing options...
BelowZero Posted March 5, 2012 Author Share Posted March 5, 2012 This is where I am confused. As I understand it, the header("Location...) has to be the first line. How then do I assign a value to the variable before that? Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323977 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2012 Share Posted March 5, 2012 As I understand it, the header("Location...) has to be the first line No it doesn't. The only restriction is that it needs to occur before you send any output to the browser. However, if you are going to redirect, there's no point in sending any output to the browser since A) any output wouldn't be seen, even if it did work, and B) producing and sending any output would waste processor resources and bandwidth, even if it did work. Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323978 Share on other sites More sharing options...
BelowZero Posted March 5, 2012 Author Share Posted March 5, 2012 Ahhh, so if I just moved the redirect to the bottom of the page it should work? Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323979 Share on other sites More sharing options...
BelowZero Posted March 5, 2012 Author Share Posted March 5, 2012 Tested and working! Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/258275-passing-a-variable-in-the-redirect-statement/#findComment-1323987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.