Page 1 of 1

Php Help

Posted: Thu Sep 18, 2003 4:12 pm
by lramos85
I have a problem with PHP, I have this code and it doesn't work:

<?php
$next_q = $_COOKIE["next"];
if ($next_q == ""){
//go to first question
$next_q = 1;
}
$question = "q".$next_q.".php";
header("Location:$question");
?>

this error comes out:
Notice: Undefined index: next in c:\inetpub\wwwroot\tilt\module2\quiz\quiz.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\tilt\module2\quiz\quiz.php:2) in c:\inetpub\wwwroot\tilt\module2\quiz\quiz.php on line 8

Basicly it redirects you to a page and makes a cookie, I have no Idea what the problem is.

Thank You.

Posted: Thu Sep 18, 2003 4:18 pm
by Drachlen
are you sure thats the full code? i can run that without any errors, but a few things you should do:
1. read this http://www.devnetwork.net/forums/viewtopic.php?t=1157

2. post with your PHP inside [ php] [ /php] tags, it makes it easier to read(without the space)

There must be more code, because with that, nothing is never being outputted, so the header should work..

That is all

Posted: Thu Sep 18, 2003 5:10 pm
by lramos85
No, that is the only code in quiz.php

Code: Select all

<?php
$next_q = $_COOKIE["next"];
if ($next_q == ""){
    //go to first question
    $next_q = 1;
}
$question = "q".$next_q.".php";
header("Location:$question");
?>
Thats why I'm saying that I have no Idea what is wrong.
it doesn't even redirect you, this would be the code of the next page:

Code: Select all

<?php
require_once "../../include/functions.php";
$question = 1;
//Check that question hasn't been answered
$msg = chk_question($next_question);
if ($msg != "") {
 $next_question = 1;
}
//Check that at least one response has been entered
if (count($q1) < 2 && $next_question == 2){
    $err_msg = "Please select two choices";
    $next_question = 1;
}
if (count($q1) > 2 && $next_question == 2){
    $err_msg = "Please select only two choices";
    $next_question = 1;
}
//Print and record question results
if ($next_question == 2){
    $total = count($q1);
    for($i = 0; $i < $total; $i++){
        $response .= "q1:".$q1[$i];
        if ($i < ($total - 1))
            $response .= "&";
    }
    $msg .= "<h4>Answers to Question One:</h4>";
    if ($q1[0] == "journal" && $q1[1] == "index") {
        $msg .= "<b>You are correct.</b>
        Periodical indexes 
        help you determine which journals have scholarly articles on 
        your topic.<br>";
        $correct = 2;
    } else if ($q1[0] == "journal" || $q1[1] == "journal") {
        $msg .= "<b>Well, you are half right.</b>
        Journals contain scholarly articles. The other correct answer is
        the source that indexes those journal articles.<br>";
        $correct = 1;
    } else if ($q1[0] == "index" || $q1[1] == "index") {
        $msg .= "<b>Well, you are half right.</b>
        Periodical indexes do help you locate scholarly articles. But
        magazines, newspapers and email are not the best sources for
        finding scholarly materials.<br>";
        $correct = 1;
    } else {
        $msg .= "<b>Hmmmm.</b>
        Neither of these answers are correct. Perhaps the phrase of 
        "scholarly information" was confusing. An example of scholarly 
        information is a journal article written by a person who has done 
        research in their area of expertise. Often these articles are 
        indexed by periodical indexes so you can easily find them.<br>";
        $correct = 0;
    }
    $response = $_COOKIE["response"];
    $response .= "+q1:".$q1[0]."&q1:".$q1[1];
    setcookie ("response", $response);
    setcookie ("next", "2");
    setcookie ("ans[0]", $correct);
    $msg .= "<p><div align='center'><a href='quiz.html'>Go on to question 2</a></div></p>";
}
require_once "../../include/quiz_header.html";
?>
 
<h2>Question One:</h2>
<h3 align="CENTER">
Imagine you have an assignment to write a paper based on scholarly<br>
information. Which would be the most appropriate sources to use? 
(Choose two.)</h3>
<form method="POST" action="q1.php">
<div align="CENTER">
<table border="0" width="60%" cellspacing="2" cellpadding="2">
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "journal"):
        print "<input type="checkbox" name="q1[]" value="journal" CHECKED>";
    else: 
        print "<input type="checkbox" name="q1[]" value="journal">";
    endif;
    ?>
    A. Journal
  </tr>
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "Magazine" || $q1[1] == "Magazine" ):
        print "<input type="checkbox" name="q1[]" value="Magazine" CHECKED>";
    else: 
        print "<input type="checkbox" name="q1[]" value="Magazine">";
    endif;
    ?>
    B. Magazine
  </tr>
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "Newspaper" || $q1[1] == "Newspaper" ):
        print "<input type="checkbox" name="q1[]" value="Newspaper" CHECKED>";
    else: 
        print "<input type="checkbox" name="q1[]" value="Newspaper">";
    endif;
    ?>
    C. Newspaper
  </tr>
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "index" || $q1[1] == "index" ):
        print "<input type="checkbox" name="q1[]" value="index" CHECKED>";
    else: 
        print "<input type="checkbox" name="q1[]" value="index">";
    endif;
    ?>
    D. Periodical index
  </tr>
  <tr>
    <td>
    <?php
    if ($q1[1] == "email" ):
        print "<input type="checkbox" name="q1[]" value="email" CHECKED>";
    else: 
        print "<input type="checkbox" name="q1[]" value="email">";
    endif;
    ?>
          E. Email 
      </tr>
  </table>
<p>
<input type="hidden" name ="next_question" value="2">
<?php 
    if ($msg == ""){ 
        //is msg != "" then the form has been submitted
        print "<input type="submit" value="submit">";
    } 
?>
</p>
</div>
</form>
<?php
//Print error message or results 
if ($err_msg != ""){
    print "
    <script language="JavaScript">
    <!--
    onLoad=window.alert("$err_msg")
    -->
    </script>";
}
print "$msg";
require_once "../../include/quiz_footer.html";
?>
I would really appreciates anyone's help, I need to have this done for friday if possible, is there anyway I can upload the files here?

Posted: Thu Sep 18, 2003 6:09 pm
by Unipus
I made one very simple change and it works for me.

Code: Select all

<?php 
$next_q = $_COOKIE["next"]; 
if (!$next_q){ 
    //go to first question 
    $next_q = 1; 
} 
$question = "q".$next_q.".php"; 
header("Location:$question"); 
?>

Posted: Thu Sep 18, 2003 6:10 pm
by JAM
Can't spot this...
Can you display what you see if you:

Code: Select all

// change from
$next_q = $_COOKIE["next"]; 
// to 
$next_q = $_COOKIE['next'];  // note the change to single quotes
print_r($_COOKIE);
exit;
On a sidenote, I personally prefer using isset() and/or empty() when checking values, as:

Code: Select all

if (!isset($next_q)){ 
    //go to first question 
    $next_q = 1; 
}

I get this:

Posted: Fri Sep 19, 2003 11:45 am
by lramos85
This is what I get for code:

Code: Select all

<?php
$next_q = $_COOKIE['next'];  // note the change to single quotes 
print_r($_COOKIE); 
exit; 
?>
Error:
Notice: Undefined index: next in c:\inetpub\wwwroot\tilt\module1\quiz\quiz.php on line 2
Array ( [ASPSESSIONIDASBSDRBT] => KIAHOIJBPIBHELBFBGNMPELP )
What Could be the problem with the code I have? could it be PHP version or something?

I tried both

Posted: Fri Sep 19, 2003 11:50 am
by lramos85
I tried all the things you guys told me and still nothing, I'm getting really frustrated, can anyone tell me a different code that would do the same thing? I would really apreciate any help. Thank You.

I currently get this error:
Notice: Undefined index: next in c:\inetpub\wwwroot\tilt\module1\quiz\quiz.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\tilt\module1\quiz\quiz.php:2) in c:\inetpub\wwwroot\tilt\module1\quiz\quiz.php on line 8

Posted: Fri Sep 19, 2003 12:39 pm
by JAM
Notice: Undefined index: next in c:\inetpub\wwwroot\tilt\module1\quiz\quiz.php on line 2
Array ( [ASPSESSIONIDASBSDRBT] => KIAHOIJBPIBHELBFBGNMPELP )
...tells you that you have not set $_SESSION['next'] anywhere. So you could try and retype the code mentioned earlier, with something similiar to:

Code: Select all

if (isset($_SESSION['next'])) {
    $foo = $_SESSION['next'];
} else {
    $foo = 1;
}
Good luck.