Be a Supporter!

PHP Problemos

  • 559 Views
  • 14 Replies
New Topic Respond to this Topic
CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
PHP Problemos 2009-11-17 18:00:29 Reply

I know HTML and CSS, I just started PHP today. I'm trying to make a quiz for my school, and I'm getting this error. I'm lost. Can someone tell me where I went wrong?
Index.html page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="results.php" method="post">

Question 1: Do you like working with your hands?<br />
<input type="radio" name="q1" value="yes" />Yes<br />
<input type="radio" name="q1" value="no" />No<br />

Question 2: Do you like technology?<br />
<input type="radio" name="q2" value="yes" />Yes<br />
<input type="radio" name="q2" value="no" />No<br />

Question 3: Do you like wood?<br />
<input type="radio" name="q3" value="yes" />Yes<br />
<input type="radio" name="q3" value="no" />No<br />

Question 4: Do you like stone?<br />
<input type="radio" name="q4" value="yes" />Yes<br />
<input type="radio" name="q4" value="no" />No<br />

Question 5: Do you like outside?<br />
<input type="radio" name="q5" value="yes" />Yes<br />
<input type="radio" name="q5" value="no" />No<br />

Question 6: Do you like hammering nails?<br />
<input type="radio" name="q6" value="yes" />Yes<br />
<input type="radio" name="q6" value="no" />No<br />

<input type="submit" value="Determine your shop" />

</form>
</body>
</html>

Results.php Page
<html>
<head>
<title>Your results</title>
<?php
$score = 0;
if ($_POST['q1'] == 'yes')
$score = $score + 1;
if ($_POST['q2'] == 'yes')
$score = $score + 1;
if ($_POST['q3'] == 'yes')
$score = $score + 1;
if ($_POST['q4'] == 'yes')
$score = $score + 1;
if ($_POST['q5'] == 'yes')
$score = $score + 1;
if ($_POST['q6'] == 'yes')
$score = $score + 1;
?>
</head>
<body>
<h1>Here is your results</h1>

<?php
echo 'Your coolness score was ' . $score . '/5<br><br>';
if ($score <= 2)
echo 'You are not very cool!';
else if ($score == 3)
echo 'Congratulations, a perfect score!';
else
echo 'Not bad, you scored average';
?>
</body>
</html>


Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-17 18:02:24 Reply

Also, It's not really an error.
Instead of echoing what it's supposed to, it echos a bunch of code.
I got this php code off this site, it works fine on the site, but it doesn't work for me.


Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-17 18:06:03 Reply

Damn, the code got all screwed up because of Newgrounds use of html for post styling.


Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
urbn
urbn
  • Member since: Jun. 10, 2007
  • Offline.
Forum Stats
Member
Level 18
Programmer
Response to PHP Problemos 2009-11-17 18:37:43 Reply

index.html should be index.php.

html extension does not support PHP unless you treat it as PHP in a .htaccess.


BBS Signature
CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-17 20:10:10 Reply

Still didn't work.
I'll give the code in the code tag this time.

Index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="results.php" method="post">

<b>Question 1: Do you like working with your hands?</b><br />
<input type="radio" name="q1" value="yes" />Yes<br />
<input type="radio" name="q1" value="no" />No<br />

<b>Question 2: Do you like technology?</b><br />
<input type="radio" name="q2" value="yes" />Yes<br />
<input type="radio" name="q2" value="no"  />No<br />

<b>Question 3: Do you like wood?</b><br />
<input type="radio" name="q3" value="yes" />Yes<br />
<input type="radio" name="q3" value="no" />No<br />

<b>Question 4: Do you like stone?</b><br />
<input type="radio" name="q4" value="yes" />Yes<br />
<input type="radio" name="q4" value="no" />No<br />

<b>Question 5: Do you like outside?</b><br />
<input type="radio" name="q5" value="yes" />Yes<br />
<input type="radio" name="q5" value="no" />No<br />

<b>Question 6: Do you like hammering nails?</b><br />
<input type="radio" name="q6" value="yes" />Yes<br />
<input type="radio" name="q6" value="no" />No<br />

<input type="submit" value="Determine your shop" />

</form>
</body>
</html>

Results.php

<html>
<head>
<title>Your results</title>
<?php
$score = 0;
if ($_POST['q1'] == 'yes')
	$score = $score + 1;
if ($_POST['q2'] == 'yes')
	$score = $score + 1;
if ($_POST['q3'] == 'yes')
	$score = $score + 1;
if ($_POST['q4'] == 'yes')
	$score = $score + 1;
if ($_POST['q5'] == 'yes')
	$score = $score + 1;
if ($_POST['q6'] == 'yes')
	$score = $score + 1;
?> 
</head>
<body>
<h1>Here is your results</h1>

<?php
echo '<b>Your coolness score was ' . $score . '/5</b><br><br>';
	if ($score <= 2)
		echo 'You are not very cool!';
	else if ($score == 3)
		echo 'Congratulations, a perfect score!';
	else
		echo 'Not bad, you scored average';
?>
</body>
</html>

Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to PHP Problemos 2009-11-18 01:11:36 Reply

You are running this off of a web/virtual server with php like (xampp) correct?

hesselbom
hesselbom
  • Member since: Jul. 19, 2008
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to PHP Problemos 2009-11-18 03:53:34 Reply

What exactly is the error? I didn't see you posting any error, just correct code. What is happening and what do you expect to happen?

CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-18 19:16:06 Reply

At 11/18/09 01:11 AM, fourthfrench wrote: You are running this off of a web/virtual server with php like (xampp) correct?

No, I'm testing this all locally. I have PHP installed.

At 11/18/09 03:53 AM, hesselbom wrote: What exactly is the error? I didn't see you posting any error, just correct code. What is happening and what do you expect to happen?

Here's what happens: I fill out the quiz, I the button to continue to the page that should show-

Here is your results
Your coolness score was you are not very cool!

Something along those lines.
But instead it shows:

Here is your results
Your coolness score was ' . $score . '/5

'; if ($score <= 2) echo 'You are not very cool!'; else if ($score == 3) echo 'Congratulations, a perfect score!'; else echo 'Not bad, you scored average'; ?>

I'm not sure where I went wrong, I'm not totally familiar with PHP.


Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
Jon-86
Jon-86
  • Member since: Jan. 30, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-18 20:09:29 Reply

Try changing it to this:

<h1>Here is your results</h1>
 
 <?php
 echo '<b>Your coolness score was ';
 	if ($score <= 2){
 		echo 'You are not very cool!';
 	}else if ($score == 3){
 		echo 'Congratulations, a perfect score!';
 	}else{
 		echo 'Not bad, you scored average';
        }
echo '</b><br><br>';
 ?>

PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature
Jon-86
Jon-86
  • Member since: Jan. 30, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-18 20:11:33 Reply

At 11/18/09 08:09 PM, Jon-86 wrote: Try changing it to this:

Bugger theirs a bug in that. Well its late and I need to sleep. But you should get the right idea from what I posted. Although your code is bloated but that dost matter since your only starting!


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature
CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-18 21:51:30 Reply

At 11/18/09 08:09 PM, Jon-86 wrote: Try changing it to this:

Still doesn't work. It comes out to:

Here is your results
Your coolness score was '; if ($score <= 2){ echo 'You are not very cool!'; }else if ($score == 3){ echo 'Congratulations, a perfect score!'; }else{ echo 'Not bad, you scored average'; } echo '

'; ?>

I tried putting else in it:

if ($_POST['q1'] == 'yes')
	$score = $score + 1
		else
		$score = $score + 0;

But that didn't work either.


Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
gapern
gapern
  • Member since: Nov. 3, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to PHP Problemos 2009-11-18 22:06:53 Reply

Move all of this crap to the body and see if it displays it as well.

<?php
$score = 0;
if ($_POST['q1'] == 'yes')
	$score = $score + 1;
if ($_POST['q2'] == 'yes')
	$score = $score + 1;
if ($_POST['q3'] == 'yes')
	$score = $score + 1;
if ($_POST['q4'] == 'yes')
	$score = $score + 1;
if ($_POST['q5'] == 'yes')
	$score = $score + 1;
if ($_POST['q6'] == 'yes')
	$score = $score + 1;
?>

I suspect it will. I don't think you have php set up properly. Your script works fine for me. Someone else asked if you had xampp, you didn't say yes or no, just that you had php installed. You should probably download it if you don't have it. If you do have it, you need to actually start the server and put the php file in the htdocs folder.

http://www.apachefriends.org/en/xampp.ht ml

CNRHKick
CNRHKick
  • Member since: May. 12, 2006
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to PHP Problemos 2009-11-19 17:55:09 Reply

At 11/18/09 10:06 PM, gapern wrote: http://www.apachefriends.org/en/xampp.ht ml

Moving it didn't work.
I'm downloading xampp now. Someone told me you could just install php and it would work fine, I didn't think you needed anything else.


Fighting for peace is like screwing for virginity.
- George Carlin

BBS Signature
RevNation
RevNation
  • Member since: Oct. 20, 2003
  • Offline.
Forum Stats
Member
Level 36
Programmer
Response to PHP Problemos 2009-11-20 12:15:52 Reply

use $score++; instead of $score = $score + 1;

RevNation
RevNation
  • Member since: Oct. 20, 2003
  • Offline.
Forum Stats
Member
Level 36
Programmer
Response to PHP Problemos 2009-11-20 12:19:09 Reply

also with your if statements, u should close them

if () {
//code
}