mmarif4u Posted March 12, 2007 Share Posted March 12, 2007 Hi guys, i have a if statement where i put some condition if condition is true That it had to go to header page. Code: if ($session_nrpackage == '1006') { $path=header("location:include/share/message.php"); exit; } //else{ // $disabled = 'disabled="False"'; // } This is where i call it in a button. <input type="button" onclick="redirect_selection(1);" <?php echo $path?> value="Selection by Set" name="btnset" class="button_assess"> But the problem is that i cant work, and give error like header already sent. How can i remove this error, Any help will be appriciated. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
kthxbai2u Posted March 12, 2007 Share Posted March 12, 2007 OMG i have perfect solution for you Its JavaScript redirect, i made a function so you just gotta copy it to your functions.php or add it in with the rest of your functions, and call it whenever and then you dont have to worry about changing the headers after they been sent! works wonders (unless javascript is disabled) function redirect ($url){ echo "<script> <!-- window.location= '$url' //--> </script>"; } Example of use: redirect("index.php?page=news&action=view"); Edit: YAY! I helped someone! Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 12, 2007 Author Share Posted March 12, 2007 This is not the sol of my problem. I think u did not see the condition. Quote Link to comment Share on other sites More sharing options...
kthxbai2u Posted March 12, 2007 Share Posted March 12, 2007 Uhhm... What condition are you speak of? In English please? lol. Dude, your basically redirecting someone to a different page by modifying the headers when your if statement is true. If the headers have already been sent, it will bitch about it, and theres no way to fix it other than to not send the headers till AFTER that code is executed. OR you could add that function to your list, and do this: if ($session_nrpackage == '1006') { redirect("include/share/message.php"); exit; } //else{ // $disabled = 'disabled="False"'; // } Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 12, 2007 Author Share Posted March 12, 2007 Its ok but i have a button on that page when the user click it, it will go to message.php page. Quote Link to comment Share on other sites More sharing options...
kthxbai2u Posted March 12, 2007 Share Posted March 12, 2007 And thats exactly what that code i posted would do. It redirects you, but not by using headers, because in your case, the headers are already sent. It does it by javascript. If you make it call a function after pressing a button, then execute the redirect function, if $session_nrpackage == '1006' then it will redirect you without any error message saying the headers have been sent. By the way, you missed this thread: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 12, 2007 Author Share Posted March 12, 2007 Yeh u r right here, I know this way but the problem is that when i go to that page it automatically redirects me to message.php. I dont want this bcoz i have another page from where when i click that link or button it iwll go to message.php page. So i think u understand what i mean and waht i want. Now i try <a href> method but no luck. Complete code is here: <?php $sql="select t_level.cdlevel,t_level.nmlevel from p_access_level inner join t_level on p_access_level.cdlevel_access = t_level.cdlevel where p_access_level.cdlevel=$session_cdlevel and t_level.cdlangu=$session_cdlangu order by cdlevel desc"; $level_arr = db_select_parameter($sql); $sql="select t_subject.cdsubject,t_subject.nmsubject from p_level_subject inner join t_subject on p_level_subject.cdsubject = t_subject.cdsubject where p_level_subject.cdlevel=$session_cdlevel and t_subject.cdlangu=$session_cdlangu"; $subject_arr = db_select_parameter($sql); $tyexercise=4; // for randomise $tyexercise_enc = encrypt_str($tyexercise); $query = "SELECT nrpackage FROM m_package where nrpackage='$session_nrpackage'"; $result = mysql_query($query) or die(mysql_error()); //$ic=$row->$session_nrpackage; if ($session_nrpackage == '1006') { $path = "<a href='include/share/message.php'></a>"; } //else{ // $disabled = 'disabled="False"'; // } ?> <script language="JavaScript"> function redirect_selection(action){ var sel_action = ""; var location; var cdlevel_enc = document.selection.cdlevel_enc.value ; var cdsubject_enc = document.selection.cdsubject_enc.value ; var tyexercise_enc = document.selection.tyexercise_enc.value ; //1-set,2-topic,3-difficulty,4-randomise if(action == 1){ sel_action = "std_rev_set_sel.php"; }else if( action==2 ){ sel_action = "std_rev_topic_sel.php"; }else if( action==3 ){ sel_action = "std_rev_diff_sel.php"; }else if( action == 4 ){ sel_action = "std_rev_exercise.php"; } if(action <= 3 ){ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc ; }else{ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc + "&tyexercise_enc=" + tyexercise_enc; } document.location = location; } </script> <br> <br> <form name="selection" method="get"> <INPUT TYPE="HIDDEN" name="tyexercise_enc" value="<?php echo $tyexercise_enc; ?>"> <table align="center" border="0" width="714" cellpadding="0" cellspacing="0" bgcolor="#5168dd"> <tr> <td height="10"></td> </tr> <tr> <td align="center"><font class="assesstitle">Revision Class</font></td> </tr> <tr> <td align="center"><font class="assess_sub">Please select your level and subject</font></td> </tr> <tr> <td height="10"></td> </tr> </table> <table align="center" border="0" width="714" cellpadding="0" cellspacing="0" bgcolor="#93a4ff"> <tr> <td height="10" width="30%"></td> <td width="20%"></td> <td width="50%"></td> </tr> <tr> <td></td> <td><font class="tabletxt">Select Class Level</font></td> <td><?php echo input_combox($level_arr,"cdlevel_enc","enc"); ?></td> </tr> <tr> <td></td> <td><font class="tabletxt">Select Subject</font></td> <td><?php echo input_combox($subject_arr,"cdsubject_enc","enc"); ?></td> </tr> <tr> <td height="10"></td> <td></td> <td></td> </tr> </table> <table align="center" border="0" width="714" cellpadding="0" cellspacing="0"> <tr> <td height=20></td> </tr> <tr> <td align="center"> <input type="button" onclick="redirect_selection(1);" <?php echo $path ?> value="Selection by Set" name="btnset" class="button_assess"> <input type="button" onclick="redirect_selection(4);" <?php echo $disabled ?> value="Random Selection" name="btnrandom" class="button_assess"> <input type="button" onclick="redirect_selection(3);" <?php echo $disabled ?> value="Selection by Difficulty" name="btndiff" class="button_assess"> </td> </tr> <tr> <td height=10></td> </tr> <tr> <td align="center"> <input type="button" onclick="redirect_selection(2);" value="Selection by Topic" name="btntopic" class="button_assess"> </td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
skali Posted March 12, 2007 Share Posted March 12, 2007 headers already sent messages can be suppressed by calling ob_start() function at the top of your page after opening <?php tag. Quote Link to comment Share on other sites More sharing options...
kthxbai2u Posted March 12, 2007 Share Posted March 12, 2007 headers already sent messages can be suppressed by calling ob_start() function at the top of your page after opening <?php tag. wow... i didnt know that... lol i asked millions of times if its possible to supress those and noone told me. Does it still redirect? or does it just supress the error message? Quote Link to comment Share on other sites More sharing options...
skali Posted March 12, 2007 Share Posted March 12, 2007 yes it will allow redirection, won't redirect itself.. Actually it buffers the data and does not send anything to browser right away but waits for the code to flush the data or when the page ends. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 12, 2007 Author Share Posted March 12, 2007 I think i am going wrong bcoz header,redirect,<a href>. These did not solve my problem. Simple header or redirect i know but here i have to see the page, when user clicks the button,it redirect the user to message.php page. At the bottom of full code u can see the button tags. Also i want to implement the condition 1st in if statement. Quote Link to comment Share on other sites More sharing options...
skali Posted March 12, 2007 Share Posted March 12, 2007 Process your condition and set $path in it then use the code below for your button. <input type="button" onclick="location.href='<?php echo $path?>'" value="Selection by Set" name="btnset" class="button_assess"> Quote Link to comment Share on other sites More sharing options...
kthxbai2u Posted March 12, 2007 Share Posted March 12, 2007 oooooo... im going to start using that cause my page wont redirect if javascript is disabled. Buddy, i may have a solution for you, i think i know what your trying to do. Your trying to make that button send to a page, its onclick function calls a javascript that redirects based on a variable passed to it (of integer type) Basically, do this: change this if ($session_nrpackage == '1006') { $path = "<a href='include/share/message.php'></a>"; } to this: if ($session_nrpackage == '1006') { $num = 5; } else { $num = 1; } and then change this: <script language="JavaScript"> function redirect_selection(action){ var sel_action = ""; var location; var cdlevel_enc = document.selection.cdlevel_enc.value ; var cdsubject_enc = document.selection.cdsubject_enc.value ; var tyexercise_enc = document.selection.tyexercise_enc.value ; //1-set,2-topic,3-difficulty,4-randomise if(action == 1){ sel_action = "std_rev_set_sel.php"; }else if( action==2 ){ sel_action = "std_rev_topic_sel.php"; }else if( action==3 ){ sel_action = "std_rev_diff_sel.php"; }else if( action == 4 ){ sel_action = "std_rev_exercise.php"; } if(action <= 3 ){ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc ; }else{ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc + "&tyexercise_enc=" + tyexercise_enc; } document.location = location; } </script> To this: <script language="JavaScript"> function redirect_selection(action){ var isexercise = false; var sel_action = ""; var location; var cdlevel_enc = document.selection.cdlevel_enc.value ; var cdsubject_enc = document.selection.cdsubject_enc.value ; var tyexercise_enc = document.selection.tyexercise_enc.value ; //1-set,2-topic,3-difficulty,4-randomise if(action == 1){ sel_action = "std_rev_set_sel.php"; }else if( action==2 ){ sel_action = "std_rev_topic_sel.php"; }else if( action==3 ){ sel_action = "std_rev_diff_sel.php"; }else if( action == 4 ){ sel_action = "std_rev_exercise.php"; isexercise = true; } else if( action == 5 ) { sel_action = "include/share/message.php" } if (isexercise) { location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc + "&tyexercise_enc=" + tyexercise_enc; else{ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc ; } document.location = location; } </script> and lastly change this: <input type="button" onclick="redirect_selection(1);" <?php echo $path ?> value="Selection by Set" name="btnset" class="button_assess"> to this: <input type="button" onclick="redirect_selection(<?php echo $num ?>);" value="Selection by Set" name="btnset" class="button_assess"> skali's idea might be quicker and easier lol. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 13, 2007 Author Share Posted March 13, 2007 Thanks kthxbai2u and skali for ur time and help. I made some changes in that and its working now fine.. Change are as: <script language="JavaScript"> function redirect_selection(action){ var sel_action = ""; var location; var cdlevel_enc = document.selection.cdlevel_enc.value ; var cdsubject_enc = document.selection.cdsubject_enc.value ; var tyexercise_enc = document.selection.tyexercise_enc.value ; //1-set,2-topic,3-difficulty,4-randomise,5-message if(action == 1){ sel_action = "std_rev_set_sel.php"; }else if( action==2 ){ sel_action = "std_rev_topic_sel.php"; }else if( action==3 ){ sel_action = "std_rev_diff_sel.php"; }else if( action == 4 ){ sel_action = "std_rev_exercise.php"; isexercise = true; } else if( action == 5 ) { sel_action = "message.php"; } if(action <= 3 ){ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc ; }else{ location =sel_action + "?cdlevel_enc=" + cdlevel_enc + "&cdsubject_enc=" + cdsubject_enc + "&tyexercise_enc=" + tyexercise_enc; } document.location = location; } </script> Quote Link to comment 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.