nemiux Posted February 10, 2009 Share Posted February 10, 2009 Hi, i need to learn function with $_post, but here what happens: page one: <HTML> <HEAD></HEAD> <title> Funkcija </title> <BODY bgcolor="black" text="white"> <form type="post" action="/funkcija.php"> Lets look, does this number can be divided from 2. <br> Your number: <input type="text" name="a"> <br> <input type="submit" value="Lets check!"> </BODY> </HTML> Everything was ok here, result is simple, you should know, but now the problem <HTML> <HEAD></HEAD> <title> Rezultatas </title> <BODY bgcolor="black" text="white"> <h3 align="center"> <B> Your result</B> </h3> <BR> <BR> <?php $a = $_post [a]; function lygu ($a) { if ($a % 2 == 0) { echo "Number can be divided"; } else { echo "Number cannot be divided"; } echo lygu ($a); } ?> <a href="/index.php"> try again?? <br> </BODY> </HTML> When i click submit in first page, this is my result: "Your result try again??" P.S. I must use funtion at this, if something's wrong, please rewrite command and make bold/italic text where it has changed. Thank you very much. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/ Share on other sites More sharing options...
coder_ Posted February 10, 2009 Share Posted February 10, 2009 <?php $a = $_POST['a']; function lygu ($a) { if ($a % 2 == 0) { echo "Number can be divided"; } else { echo "Number cannot be divided"; } echo lygu ($a); } ?> try this... You access a element inside an array by putting a key into a string... Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-758994 Share on other sites More sharing options...
nemiux Posted February 10, 2009 Author Share Posted February 10, 2009 doesn't help, its like function lygu ($a) { if ($a % 2 == 0) { echo "Number can be divided"; } else { echo "Number cannot be divided"; } doesn't exist... Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-758996 Share on other sites More sharing options...
JonnoTheDev Posted February 10, 2009 Share Posted February 10, 2009 $_post should be $_POST Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-758997 Share on other sites More sharing options...
nemiux Posted February 10, 2009 Author Share Posted February 10, 2009 rewriten to: <?php $a = $_POST ['a']; ..... Didnt help, still result is text and href to index.php Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-758999 Share on other sites More sharing options...
nemiux Posted February 10, 2009 Author Share Posted February 10, 2009 Look at command closely and rewrite what's wrong, function has 2 be used, u can change its position, thank you . Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-759000 Share on other sites More sharing options...
JonnoTheDev Posted February 10, 2009 Share Posted February 10, 2009 You are calling your function within the scope of the function itself and doubling up on your echo. Check your braces carefully. Should be: <?php function lygu ($a) { if ($a % 2 == 0) { echo "Number can be divided"; } else { echo "Number cannot be divided"; } } $a = $_POST['a']; lygu ($a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-759003 Share on other sites More sharing options...
nemiux Posted February 10, 2009 Author Share Posted February 10, 2009 Here's the problem this time: Everything works, but it doesn't matter do i enter 2, 3, 5, it still writes number CAN be divided What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-759010 Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 Here's the problem this time: Everything works, but it doesn't matter do i enter 2, 3, 5, it still writes number CAN be divided What is the problem? Everything can be divided as long as it is not trying to be divided by 0. You mean to say, is the passed in number divisible by 2? <?php function lygu ($a) { if (($a % 2) == 0) { // parans here around the first portion echo "Number is divisible by two."; } else { echo "Number is not divisible by two."; } } $a = $_POST['a']; lygu ($a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-759016 Share on other sites More sharing options...
JonnoTheDev Posted February 10, 2009 Share Posted February 10, 2009 Check that $a is of the correct value before running into the function. It must have a value of 0 if that is your result. Are you familiar with HTML? You haven't got a closing FORM tag! Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-759022 Share on other sites More sharing options...
nemiux Posted February 10, 2009 Author Share Posted February 10, 2009 Sorry, stupid mistake, everything is right, but it still doesn't work. I'll look 2morrow, its evening in my time and i cant think normally Quote Link to comment https://forums.phpfreaks.com/topic/144643-function/#findComment-759028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.