Hillary Posted March 10, 2008 Share Posted March 10, 2008 My assignment is to create a function that calculates the sum of two numbers. The function will have two parameters, one for each of the numbers to be added. After the total is computed, it should be returned and then displayed. *i already know how to pass multiple values to a function. *i already know how to add two values and store the answer in a variable. *i already know how to return a value from a function, but pretty much i can only return strings, thats all we've worked on at school. ----------------------------------------------------------------------------------------- this is the work i have done so far, i have included a few functions from my file functions.php. on the web it displays everything, it looks right with the text areas, plus, equals, when i click submit it goes to the action page... but i dont even know where to start with the action page. should i even use one? is there a way for me to get the answer to print on the same page? the goal i think is to have 2 blank text areas the user types in a value in to each and it will come up with the answer. <?php include "functions.php"; $f = form_open("sumof2answer.php"); print $f; $h = hidden(""); print $h; $t1 = text_area("val1", 1, 1); $t2 = text_area("val2", 1, 1); $a = sum($t1, $t2); print $a; $s = submit(); print $s; ?> -------------------------------------------------------------------------------------- it looks like this [___] + [___] = (submit) i dont know what to do. Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/ Share on other sites More sharing options...
corbin Posted March 10, 2008 Share Posted March 10, 2008 Errr if you know how functions work, you should know that you want something like: function add2numbers($num1, $num2) { return $num1 + $num2; //this is entirely pointless...... much easier to just do the + in //the middle of the script, but this is how I understood you were supposed to do it.... } Anyway, I don't feel like typing out a tutorial on $_POST and what not, so I'll link you instead! ;p http://www.w3schools.com/php/php_post.asp http://www.tizag.com/phpT/postget.php You could use $_GET for the form too.... Same concept, just different methods. Edit: Oh.... I was assuming you know HTML fluently, but if you're entirely new to this, you might want to check out HTML forms too. (Google) Glad to answer any questions, but I think it's more constructive for you to try to figure parts out ;p. Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/#findComment-489006 Share on other sites More sharing options...
Hillary Posted March 11, 2008 Author Share Posted March 11, 2008 thanks for your help, i am kind of new to this. i only know HTML from typing things on myspace sad to say, and i've only been learning php for about 7 weeks. so i am pretty new. i can figure out a lot of it on my own but sometimes i come to a road block and need a little assistance. we don't use POST too much in class mostly GET but as far as i know they are VERY similar if not the same?... o-well thanks again! Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/#findComment-489052 Share on other sites More sharing options...
Barand Posted March 11, 2008 Share Posted March 11, 2008 try <?php function sum($a, $b) { return $a + $b; } /** * check data has been sent */ if (isset($_GET['sub']) && !empty($_GET['a']) && !empty($_GET['b'])) { $a = $_GET['a'] ; $b = $_GET['b'] ; /** * output the value returned by the function */ echo "Result: $a + $b = " . sum($a, $b) . '<br/>'; } ?> <!-- html form No action specified so the default is itself as the action page Define method as GET. If you use POST method, change $_GET above to $_POST --> <form action="" method="GET"> <input type="text" name="a" size="5"> + <input type="text" name="b" size="5"> <input type="submit" name="sub" value="Add"> </form> POST and GET, while both methods of sending form data, are not the same. If your method is GET, you will notice that you browser address bar will contain a "querystring" after the URL E.G. thispage.php?a=1&b=2 With POST, the data is sent invisibly behind the scenes. Also, with GET you are limited to around 2Kb of data. The default limit for POST is 8Mb Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/#findComment-489827 Share on other sites More sharing options...
mjohnson025 Posted March 12, 2008 Share Posted March 12, 2008 just another note on the difference with $_POST and $_GET to piggyback on what Barand said: You'll find that the more sensitive the information is on the server, you'll want to use $_POST; it's a little more secure. (At least that's how it was taught to me ) Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/#findComment-490727 Share on other sites More sharing options...
discomatt Posted March 15, 2008 Share Posted March 15, 2008 POST is to GET as a wood fence is to a chain link. Neither are secure, whatsoever. Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/#findComment-493002 Share on other sites More sharing options...
hamza Posted June 11, 2008 Share Posted June 11, 2008 <?php function sum($a, $b) { return $a + $b; } /** * check data has been sent */ if (isset($_GET['sub']) && !empty($_GET['a']) && !empty($_GET['b'])) { $a = $_GET['a'] ; $b = $_GET['b'] ; /** * output the value returned by the function */ echo " <fieldset> <legend> Result </legend> $a + $b =".sum($a, $b)." </fieldset> "; } ?> <!-- html form No action specified so the default is itself as the action page Define method as GET. If you use POST method, change $_GET above to $_POST --> <form action="" method="GET"> <input type="text" name="a" size="5"> + <input type="text" name="b" size="5"> <input type="submit" name="sub" value="Add"> </form> Link to comment https://forums.phpfreaks.com/topic/95499-re-assignment/#findComment-563036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.