Forum Moderators: coopster

Message Too Old, No Replies

If else statement

Trying to get an if else statement to work

         

Adam5000

8:02 pm on Sep 28, 2010 (gmt 0)

10+ Year Member



I'm trying to get an if else statement to work using information entered into a form. If the username entered is abc then display the word "One" on the screen. If it's anything else, display the word "Two" on the screen. I've tried it several ways but no luck. The form is displayed on the screen and appears to work but neither of the echo statements aren't displayed. Below is the code I'm using.

Help!

<html>
<head>

<title>Echo display test php</title>

</head>

<body>

<?php

function uname()
{
if ($_POST["user_name"] == abc)
echo "One";

else
echo "Two";
}

?>

<form action"display_field_2.php" type="post" onsubmit="return uname()";>

Username: <input type = "text" name = "user_name">
<input type = "submit" value = "Submit">

</form>


</body>
</html>

Matthew1980

8:57 pm on Sep 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Adam5000,

function uname(){
if ($_POST["user_name"] == "abc"){
echo "One";
}
else{
echo "Two";
}


That fixes your function, though realistically you *should* return values from functions, better coding IMO.

Also your missing the = from the action attribute, that will kill the form straight away.

I notice that your using the onsubmit="" javascript to invoke the php function, sadly, as these are two different languages, you can't use JS to call a php function that way *though* you can use php to dynamically call JS functions etc.

From what your trying to do, there are easier ways, the simplest of which is like this:-

<html>
<head>

<title>Echo display test php</title>

</head>

<body>

<?php
echo ((isset($_POST['user_name']) && !empty($_POST['user_name'])) ? strip_tags($_POST['user_name']) : '');
echo "</br>";
echo (isset($_POST['user_name']) && ($_POST['user_name'] == "abc") ? "You typed in abc! Well done" : '');
?>

<form action="" method="post" >
Username: <input type="text" name="user_name">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>


That will just print to screen what ever you input into the input box, hopefully you can see the logic in what I have given as example there.

Cheers,
MRb

rocknbil

10:08 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adam - you're not calling your function, you can't call a PHP function once the form is loaded.

<html>
<head>
<title>Echo display test php</title>
</head>
<body>
<?php
$input = (isset($_POST['user_name']))?$_POST['user_name']:null;
echo uname($input);
?>
<form action"display_field_2.php" type="post">
<!-- won't work - put this in Javascript
onsubmit="return uname()"-->
Username: <input type = "text" name = "user_name">
<input type = "submit" value = "Submit">
</form>
</body>
</html>
<?php
function uname($inp=null) {
if ($inp == abc) { return "One"; }
else { return "Two"; }
}
?>

A dissection:

Don't always count on something to be in post, like when the form first loads. Always check for it. So this

$input = (isset($_POST['user_name']))?$_POST['user_name']:null;

says "if $_POST['user_name'] exists, set it's value to $input, otherwise set it to null."

We then combine an echo with a call to the function in which we pass $input as a parameter.

echo uname($input);

A synoymous task would be

$somevar = uname($input);
echo $somevar;

The function is only moved down as a personal preference, keeps the tools out of the way. :-) Out function accepts a single parameter but understands it may be null:

function uname($inp=null) {

This

function uname($inp) {

would make the parameter required and error the first time the form loads.

We then evaluate the input value and return that value to wherever it was called, which is used to echo in this case. A similar and more terse representation using the ternary operator, combined with return:

function uname($inp=null) {
return ($inp == abc)?"One:"Two";
}

This is the exact same thing as the first example, an if/else statement, it just does it in one line.

Interesting, we're talking about return values from Javascript in your other thread. :-)

Adam5000

10:23 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



Success!

rocknbil: Thanks for your help. You're a genius! Terrific job! I've got some more things to add to the form but I've got this part working. Great coding!

Matthew: You were right. Using Javascript to call a PHP function didn't work. So I took a different approach and that did work. Thanks for your help too.