Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/144643-function/
Share on other sites

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);
?> 

Link to comment
https://forums.phpfreaks.com/topic/144643-function/#findComment-759003
Share on other sites

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);
?> 

Link to comment
https://forums.phpfreaks.com/topic/144643-function/#findComment-759016
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.