Jump to content

[SOLVED] Can i do it like this...but


mmarif4u

Recommended Posts

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.

Link to comment
Share on other sites

OMG i have perfect solution for you :D

 

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! :D

Link to comment
Share on other sites

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"';
  // }

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.