Help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DJJester
Forum Newbie
Posts: 8
Joined: Sun Apr 06, 2008 6:31 am

Help!

Post by DJJester »

Hi gang,

I'm on a deadline and I'm getting a little bit desperate now... It's a simple script but there's something funny going on... Here's the code:
__________________________
<?php
// this starts the session
session_start();
?>
<HTML>
<HEAD>
<link rel="stylesheet" type="Text/css" href="/stdtheme.css" />
</HEAD>
<?php
If (isset($page)) {
If ($page > 2) {
$to = "<email>";
$subject = "<subject>";
$txt = $_SESSION["answers"];
$header = "<from>";
mail($to,$subject,$txt,$headers);
echo "Thanks for participating. Your results have now been emailed.";
exit("Goodbye");
}

elseif ($page > 1) {
$questions = split(':', $_POST["QuestionID"]);
foreach ($questions as $id) {
$_SESSION["answers"] .= $id . ":" . $_POST['$id'] . "#";
}
}

writeQandA($page);
echo '<A HREF=<website>?page=' . ($page + 1) . '>';
echo "Next Page</A>";
}
else {
echo "Welcome to the Shout! Survey. <BR><BR>";
echo '<A HREF=<website>?page=1>';
echo 'Begin!<' . chr(47) . 'A>';
}
function writeQandA($page)
{
echo '<FORM Action=<website> Method=POST>';
If ($page = 1) {
echo 'Question 1: ';
echo 'This will be multiple choice, on page 1';
echo '<HR>';
echo '<TABLE>';
echo '<TR>';
echo '<TD>';
echo '1:';
echo '<input type=checkbox name=0 value=1>';
echo 'First answer to multiple';
echo '<' . chr(47) . 'TD>';
echo '<TD>';
echo '2:';
echo '<input type=checkbox name=0 value=2>';
echo 'Second Answer to multiple';
echo '<' . chr(47) . 'TD>';
}
If ($page = 2) {
echo 'Question 2: ';
echo 'This will be a single, on page 2';
echo '<HR>';
echo '<TABLE>';
echo '<TR>';
echo '<TD>';
echo '1:';
echo '<input type=radio name=1 value=1>';
echo 'This is the answer to the single q.';
echo '<' . chr(47) . 'TD>';
echo '<TD>';
echo '2:';
echo '<input type=radio name=1 value=2>';
echo 'This is the next answer to the single';
echo '<' . chr(47) . 'TD>';
}
echo '<' . chr(47) . 'TABLE>';
echo '<input type=hidden name=QuestionID value=1:3>';
echo '<' . chr(47) . 'FORM>';
}
?>
</HTML>
__________________________

Here's the problem; firstly that the page runs fine to give you the first intro, then on the next page to run the function writeQandA.

However, it shows each $page condition, so, it shows everything from the function even when I have echoed inside the function '$page' and it shows the right number from the url.

The other issue is the email it sends after the last one, it doesn't send the values from the question ids in the hidden input box, and the values from the input/checkboxes...

Thanks loads in advance to some help!!!

Jess.
Last edited by DJJester on Mon Apr 07, 2008 6:33 am, edited 2 times in total.
DJJester
Forum Newbie
Posts: 8
Joined: Sun Apr 06, 2008 6:31 am

Re: Help!

Post by DJJester »

Ok, well, I fixed the first part with this way (as I had seen that it obviously worked for the section to see if $page was set):

If $page > 0 && $page < 2 (ie, equals one) and same for the other >1 && <3

But why would '=' not work?

I still have the issue where it sends a string of ':#' empty of the values of selected radio/checkboxes and the ID of each question (from the hidden value). In other words, trying to have it so it emails Qid:Aid#Qid:Aid# (QuestionID:AnswerID#) so if someone gave me a hand there, they'd be a personal hero!

Cheers,
Jess.
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Help!

Post by N1gel »

Sorry I can't read through and help you with all of your problems.

But just as a quick response your = isn't working because you need to use a logical equals ==

Code: Select all

 
If ($page == 1) 
 
DJJester
Forum Newbie
Posts: 8
Joined: Sun Apr 06, 2008 6:31 am

Re: Help!

Post by DJJester »

Ahhh, well appreciated.

I'm a little scared to change it now - it's working, not too messy as a work around, I'm happy there. :)

But yeah, the other one is a doosy... And I'm running out of time...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Help!

Post by onion2k »

Wow. This thread is like an exercise in how not to post on PHPDN.
2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
All users of any level are restricted from bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not receive a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
And you've not used tags around your code.

Plus, somewhat offtopic, but adding things like "I'm in a hurry" to your post makes people like me less likely to help you. Why should we have the stress of your deadline? It's not our problem.
DJJester
Forum Newbie
Posts: 8
Joined: Sun Apr 06, 2008 6:31 am

Re: Help!

Post by DJJester »

Ah, you are right onion, I'm sorry - a little stressed but really, I should have seen it from the same perspective.

As for the bumping, I would not have posted if not to say that I had resolved one out of 2 issues. But, on the other hand, it was bumped as someone had beaten me to writing a post with a solution.

This post is not only an apology, but with purpose. Getting the information submitted now (forgot the submit button for one thing, always helps), but trying to cycle the $_POST values of each form element. After much research, I have got a few possible solutions, but none of which seem to work.

1) This gets the elements, but not the values:

Code: Select all

 
$i = 0;
$questions = split('-', $QuestionID);
foreach ($questions as $id) {
$_SESSION["answers"] .= $id . ":" . $_POST[$i] . "#";
$i++;
 
2) This gives me nothing at all (as detailed in the PHP manual from someone):

Code: Select all

 
foreach ($_POST as $key => $value) {
$_SESSION["answers"] .= $key . ':' . $value . '#';
}
 
 
I can't use $_POST["element"] as they will be completely dynamic and constructed different every time so unless I can use $_POST[$var] then I'm out of ideas.

Thanks again,
Jess.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Help!

Post by onion2k »

You can use $_POST[$var].
Post Reply