/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Passing variables

I’m a moderate PHP coder, but I’m not into the more complicated stuff yet. I’m taking Internet Programming and Development class right now, and we have to create this example that has two pages. The first page is a form where the user inputs two numbers and then clicks one of four radio buttons (addition, subtraction, multiplication, or division) before hitting Submit. On the results page, the two numbers are shown with the operation sign of whichever operation they chose on the previous page (i.e. addition) with the answer to the problem (i.e. 8 / 2 = 4). I’m having trouble getting it to work on the results page, and I can’t figure it out. Here’s the codes to the pages:

Form Page:
<?xml version=”1.0″ encoding=”utf-8″?>
<!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” xml:lang=”en” lang=”en”>
<head>
<title>
2910_22.php
</title>
</head>
<body>
[code here admitted, note saying author’s name (me)]
<form method=”post” action=”2910_22_get.php” name=”form1″>
Enter #1: <input type=”text” name=”num1″ /> <br />
Enter #2: <input type=”text” name=”num2″ /> <br />
<input type=”radio” name=”add” value=”add” />Addition <br /> <!–purpose: add–>
<input type=”radio” name=”add” value=”sub” />Subtraction <br /> <!–purpose: sub–>
<input type=”radio” name=”add” value=”mult” />Multiplication <br /> <!–purpose:mult–>
<input type=”radio” name=”add” value=”div” />Division <br /> <!–purpose:div–>
<input type=”submit” value=”submit” />
</form>
</body>
</html>

Results Page:
<?xml version=”1.0″ encoding=”utf-8″?>
<!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” xml:lang=”en” lang=”en”>
<head>
<title>
2910_22_get.php
</title>
</head>
<body>

[code admitted for same reason as above]
<?php
$num1=$_POST[‘num1’];
$num2=$_POST[‘num2’];
$radio=$_POST[‘add’];
if ($radio == add)
{
$y=$num1 + $num2;
echo $num1.” + “.$num2.” = “.$y;
}
elseif ($radio == sub)
{
$y=$num1 – $num2;
echo $num1.” – “$num2.” = “.$y;
}
elseif ($radio == mult)
{
$y=$num1 * $num2;
echo $num1.” X “.$num2.” = “.$y;
}
else
{
$y=$num1 / $num2;
echo $num1.” / “.$num2.” = “.$y;
}
?>
</body>
</html>

The results page, at this time, doesn’t output anything (blank page). Any help would be greatly appreciated.

to post a comment
PHP

34 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 19.2010 — Next time post all code using code and PHP tags, thank you.

See these lines?

[code=php]if ($radio == add)
elseif ($radio == sub)
elseif ($radio == mult)[/code]


You forgot to put quotes (either single or double) around add, sub and mult in each line. Those stand out, so fix those you should be okay. For extra credit you could check to make sure $_POST['SUBMIT'] is set before processing the math, add code to prevent against SQL injection, validate the form, etc. - but you got the basics down.
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 19.2010 — How do you use the code and PHP tags on here?

I tried changing those to include double quotes and then I tried single quotes and neither of them worked. I was wondering, should I change the values of the input fields to numbers over words to get it to work? Normally I use numbers for values, but this only the 2nd hardest example we've had to do. The other example I haven't made it to yet (this is example #22, the other is #27).
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 19.2010 — Highlight and copy your code. Click code (#) or PHP icon in editor once for opening tag, paste your code, then click same icon again or closing tag. Or type in tags manually. Doing so makes it easier to read and includes colorized syntax highlighting. This helps us help you.

(Hey moderators -- post a sticky about this please!)

The advice I gave you should have resolved your issue based on your previous code. If you can't get it to work still maybe something else is wrong, or a typo, but reply back with the revised code so we can take a look.

Not sure what you mean by your last comment (numbers/values) - the input text form field can be either. If you want to restrict that type of field in HTML to allow only numbers, it's done through special Javascript coding to change this default behavior. Here is an example including how to do that, but this is more advanced of course.

-jim
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 22.2010 — Alright, thanks. What I meant by numbers/value was changing the values in the text fields ([code=html]<input type="text" name="add" value="add" />[/code]) to numbers (i.e. [code=html]<input type="text" name="add" value="1" />[/code]) over words. I wasn't sure if that would make a difference or not.

Revised code:

Form Page:
[code=html]<?xml version="1.0" encoding="utf-8"?>
<!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" xml:lang="en" lang="en">
<head>
<title>
2910_22.php
</title>
</head>
<body>
<Code omitted (name)>
<form method="post" action="2910_22_get.php" name="form1">
Enter #1: <input type="text" name="num1" /> <br />
Enter #2: <input type="text" name="num2" /> <br />
<input type="radio" name="add" value="add" />Addition <br /> <!--purpose: add-->
<input type="radio" name="add" value="sub" />Subtraction <br /> <!--purpose: sub-->
<input type="radio" name="add" value="mult" />Multiplication <br /> <!--purpose:mult-->
<input type="radio" name="add" value="div" />Division <br /> <!--purpose:div-->
<input type="submit" value="submit" />
</form>
</body>
</html>[/code]


Results Page:
[code=html]<?xml version="1.0" encoding="utf-8"?>
<!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" xml:lang="en" lang="en">
<head>
<title>
2910_22_get.php
</title>
</head>
<body>
<Code omitted (name)>
[code=php]<?php
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$radio=$_POST['add'];
if ($radio == "add")
{
$y=$num1 + $num2;
echo $num1." + ".$num2." = ".$y;
}
elseif ($radio == "sub")
{
$y=$num1 - $num2;
echo $num1." - "$num2." = ".$y;
}
elseif ($radio == "mult")
{
$y=$num1 * $num2;
echo $num1." X ".$num2." = ".$y;
}
else
{
$y=$num1 / $num2;
echo $num1." / ".$num2." = ".$y;
}
?>[/code]

</body>

</html>[/code]
Copy linkTweet thisAlerts:
@ericatekkaFeb 22.2010 — [code=html]<?xml version="1.0" encoding="utf-8"?>
<!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" xml:lang="en" lang="en">
<head>
<title>
2910_22.php
</title>
</head>
<body>
<Code omitted (name)>
<form method="post" action="2910_22_get.php" name="form1">
Enter #1: <input type="text" name="num1" /> <br />
Enter #2: <input type="text" name="num2" /> <br />
<!-- I'm making this selected as a user can simply go to the next page with no value. I suggest you add some javascript checking. -->
<input type="radio" name="add" value="1" selected="selected" />Addition <br /> <!--purpose: add-->
<input type="radio" name="sub" value="1" />Subtraction <br /> <!--purpose: sub-->
<input type="radio" name="mult" value="1" />Multiplication <br /> <!--purpose:mult-->
<input type="radio" name="div" value="1" />Division <br /> <!--purpose:div-->
<input type="submit" value="submit" name="math" />
</form>
</body>
</html>[/code]

THis what you mean? I'll do the next bit for you also.
[code=php]<?php


if(isset($_POST['math'])){
$num1=$_POST['num1'];
$num2=$_POST['num2'];


if (isset($_POST['add']))
{
$y=$num1 + $num2;
echo $num1." + ".$num2." = ".$y;
}
elseif (isset($_POST['sub']))
{
$y=$num1 - $num2;
echo $num1." - "$num2." = ".$y;
}
elseif (isset($_POST['mult']))
{
$y=$num1 * $num2;
echo $num1." X ".$num2." = ".$y;
}
elseif(isset($_POST['div']))
{
$y=$num1 / $num2;
echo $num1." / ".$num2." = ".$y;
}else{
echo "No method selected";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — In order...

It doesn't matter what value you use ("add" or "1" or whatever) so long as you other code checks for that value - I advise using a sensible naming convention so the value is easily understood when someone is reading the code. For example, maybe "operator" makes more sense in this script.

Edited because you posted a correction before I could respond:

You want something like this:

[code=html]<input type="radio" name="operator" value="add" selected />Addition <br /> <!--purpose: add-->
<input type="radio" name="operator" value="sub" />Subtraction <br /> <!--purpose: sub-->
<input type="radio" name="operator" value="mult" />Multiplication <br /> <!--purpose:mult-->
<input type="radio" name="operator" value="div" />Division <br /> <!--purpose:div-->[/code]


That sets the group name to "type" with the appropriate value.

Later, in your PHP that processes the form, get the value via $_POST['operator'] which will be set to either "add", "sub", "mult" or "div"

BTW, on the forum don't use the code/php tag more than once per block of code.

-jim
Copy linkTweet thisAlerts:
@MindzaiFeb 22.2010 — Also note that using "selected" on its own is not valid for XHTML, you need to use selected="selected".
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — @op - don't be surprised if other users point out some fixes you'll also need to make. Among them, if you want to impress your professor, anti-SQL injection techniques, form validation, making sure the math doesn't result in divide by zero or other crazy calculations, and whatever else. Bearing in mind this is a simple lesson in class, we're not going to ask you to do this for us now - but just bear in mind for the future, on a real contract or job. FYI.
Copy linkTweet thisAlerts:
@ericatekkaFeb 22.2010 — Apologies. Changed. Did not notice xHTML. Spacing out.
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 22.2010 — I tried changing my code to stuff like above, and still got nothing.

And before I go farther, what I meant with the numbers/value/etc. was THIS:
[code=html]<input type="text" name="operator" value="1" /><!--purpose: add-->
<input type="text" name="operator" value="2" /><!--purpose:sub-->[/code]

Instead of
[code=html]<input type="text" name="operator" value="add" /><!--purpose: add-->
<input type="text" name="operator" value="sub" /><!--purpose: sub-->[/code]


Here's pages as they are now. (Oh, and my teacher looked at my code and couldn't figure out why I get nothing on my results page):

Form page:
[code=html]<?xml version="1.0" encoding="utf-8"?>
<!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" xml:lang="en" lang="en">
<head>
<title>
2910_22.php
</title>
</head>
<body>
<!--code ommited-->
<form method="post" action="2910_22_get.php" name="form1">
Enter #1: <input type="text" name="num1" /> <br />
Enter #2: <input type="text" name="num2" /> <br />
<input type="radio" name="operator" value="add" />Addition <br /> <!--purpose: add-->
<input type="radio" name="operator" value="sub" />Subtraction <br /> <!--purpose: sub-->
<input type="radio" name="operator" value="mult" />Multiplication <br /> <!--purpose:mult-->
<input type="radio" name="operator" value="div" />Division <br /> <!--purpose:div-->
<input type="submit" value="submit" />
</form>
</body>
</html>[/code]


Results Page:
[code=php]<?xml version="1.0" encoding="utf-8"?>
<!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" xml:lang="en" lang="en">
<head>
<title>
2910_22_get.php
</title>
</head>
<body>
<!--code ommited-->
<?php
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$radio=$_POST['operator'];
if ($radio == "add")
{
$y=$num1 + $num2;
echo $num1." + ".$num2." = ".$y;
}
elseif ($radio == "sub")
{
$y=$num1 - $num2;
echo $num1." - "$num2." = ".$y;
}
elseif ($radio == "mult")
{
$y=$num1 * $num2;
echo $num1." X ".$num2." = ".$y;
}
else
{
$y=$num1 / $num2;
echo $num1." / ".$num2." = ".$y;
}
?>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@MindzaiFeb 22.2010 — Do you have error_reporting on and set to a suitable level?

You are probably getting a parse error on line 1 due to having short tags enabled. The XML declaration will be treated by the PHP parser as an opening PHP tag followed by code it doesn't understand. Try removing or echoing that line rather than outputting it directly.

You also have a parse error here:

[code=php]echo $num1." - "$num2." = ".$y;[/code]

This is a lesson in why developing with error_reporting on is so important. Maybe you should point that out to your teacher!
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 22.2010 — I'm afraid I don't understand what you mean by error_reporting.

And I don't start PHP in the results page until line 12 and I end it at line 36. Everything else is XHTML.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — For help on any PHP function mentioned visit php.net and type in the function name at the top.

But in your code you need to fix this mistake as Mindzai pointed out.

[code=php]echo $num1." - "$num2." = ".$y; [/code]

should be

[code=php]echo $num1." - ".$num2." = ".$y; [/code]

Ask your teacher to teach you about error reporting in PHP - it's very basic stuff so you can resolve your own errors and not stare at a blank screen as you test. After you change the above, and use the type of sensible naming convention I suggested earlier with working examples, you'll be good to go.

-jim
Copy linkTweet thisAlerts:
@MindzaiFeb 22.2010 — And I don't start PHP in the results page until line 12...[/QUOTE]

You might not think you do, but if your server is set up to allow short open tags (which it usually is by default), then trust me, you do. The XML declaration is treated by the parser as PHP code when short tags are enabled.
Copy linkTweet thisAlerts:
@esquiladoFeb 22.2010 — Are you still getting a blank page?
Copy linkTweet thisAlerts:
@esquiladoFeb 22.2010 — [code=php]elseif ($radio == sub)
{
$y=$num1 - $num2;
[B]echo $num1." - "$num2." = ".$y;[/B]
}[/code]


Your concatenation here is slightly off, you forgot the extra period between the " - " and $num2 which would result in an error. The line highlighted should read: [code=php]echo $num1." - ".$num2." = ".$y;[/code]
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — [code=php]Your concatenation here is slightly off, you forgot the extra period between the " - " and $num2 which would result in an error. The line highlighted should read: [code=php]echo $num1." - ".$num2." = ".$y;[/code][/quote]

Read reply #14 please. Covered already.
Copy linkTweet thisAlerts:
@esquiladoFeb 22.2010 — Oh sorry, is it working now? I can imagine undefined variables coming up which should be fixed by using an @ sign.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — Oh sorry, is it working now? I can imagine undefined variables coming up which should be fixed by using an @ sign.[/quote]

The OP will examine all these collective responses, make the appropriate fixes and get back to us. They're currently offline as of this writing. Thanks for helping.
Copy linkTweet thisAlerts:
@esquiladoFeb 22.2010 — Point being?
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — Huh? I'm not the OP. You asked twice if it's working yet, the first time seemed to be directed to the OP which is cool, but the second time was clearly directed to me (did you think I'm the OP? If not, how would I know if its working yet?) Then I reminded you I reminded you to avoid confusion that we need to wait for the OP to respond if all is well, noting they're offline right now, and thank you for your help so far. If you're reading more into it than that, I dunno what to say. I already thanked ya! Do you want blood?
Copy linkTweet thisAlerts:
@esquiladoFeb 22.2010 — Yes please!! LOL jking && sorry
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 22.2010 — Yes please!! LOL jking && sorry[/quote]

?
Copy linkTweet thisAlerts:
@MindzaiFeb 23.2010 — I can imagine undefined variables coming up which should be fixed by using an @ sign.[/QUOTE]

No, it should be fixed by defining the variables. Using @ is the PHP equivalent of sticking your fingers in your ears and running away.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 23.2010 — Put so eloquently and so [U]very[/U] true! :p
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 23.2010 — Alright, thanks for the help everyone. I got it to work (the error, as far as I can tell, was in line 25 on the Results Page with the missing concatenation). I'm going to talk to my teacher today about teaching us error reporting because php.net didn't really explain it well enough for me.

While I'm thinking about it, is there a way to transfer variables between a form page and the results page (and the variable isn't in an input field)? I was thinking, if it is possible, that you'd do it through the URL string, but I'm not sure if it is even possible.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 23.2010 — You could use a hidden form field, data will end up in $_POST['fieldname'] or $_GET['fieldname'] depending on form method. If form is method POST you also have the option of appending an argument to the action URL in the form, i.e. <form action="result.php?key=value" method="post"> but always protect for SQL injection of course. Beyond that you have sessions and cookies which are both well documented in this forum and plenty of tutorials out there.

-jim
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 23.2010 — Alright, thanks a lot for all the help (again). I'm going to go change this thread title to "Resolved" now.

EDIT, I just realized I [i]can't[/i] change that, for whatever reason.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 23.2010 — Next time click on Thread Tools and you'll see an option to mark as resolved.
Copy linkTweet thisAlerts:
@esquiladoFeb 23.2010 — No, it should be fixed by defining the variables. Using @ is the PHP equivalent of sticking your fingers in your ears and running away.[/QUOTE]

it works either way.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 23.2010 — Mindzai is not stating the code won't work by doing so.

What makes for well written code (i.e. not the "lazy" approach) is when its done in such a way where @ is not necessary to suppress warnings and errors and use the many powerful PHP functions as they're intended to be used. First off, in a sandbox environment you want to see all the possible errors and warnings to properly account for them and fix them. Transversely, in production versions especially including large or complex apps, suppression can be very costly. But using the proper functions in the proper way also improves code portability from server to server, and even helps you or someone else debug the code much more easily suppression can often lead to false assumptions as to what triggered the warning/error.

Also, you'll learn how to use the language constructs properly which means, in the end, better code and a better coder, hopefully. Pride for the part time PHP hobbyist and increased salary potential or the pro.

Do you honestly think we're going to advocate "lazy" programming techniques simply because PHP allows for it?

Example:

[code=php]@unlink($filespec); // Supressed
if (file_exists($filespec)) {unlink($filespec);} // Recommended procedure
[/code]

-jim
Copy linkTweet thisAlerts:
@MindzaiFeb 24.2010 — it works either way.[/QUOTE]

Cutting my nose off would stop it itching, but it doesn't mean it's as good a solution as scratching it.
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 24.2010 — Cutting my nose off would stop it itching, but it doesn't mean it's as good a solution as scratching it.[/QUOTE]
True. Just because something is the "easy" way doesn't mean it is the [i]best[/i] way to handle something.
×

Success!

Help @rainstinks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...