PDA

View Full Version : Need help with form in HTML


Jaynesh
01-24-2006, 02:11 PM
Im lerning php and just experimenting and messing around with a few stuff.

Ive written this code in dreamweaver ( Thanx for you tutorial MindTrix, https://vborg.vbsupport.ru/showthread.php?t=59914&page=1&highlight=elseif ) :)

Heres the code:


<?php

$name = "Jaynesh";

if ( $name == "Bob" )

{

print "Yes my name is $name";

}
elseif ( $name == "Johnny" )
{
print "Yes, My name is Johnny";
}
elseif ( $name == "Mark" )
{
print "Yes, My name is mark";
}
elseif { $name == "Sarah" )
{
print "Yes, My name is sarah";
}
else {
print "No, My name is $name";
}
?>


How can i make it so i can select a name from a form instead of manually putting it in the php file.

e.g Imagine there is a form on a page, and when the user types the name Mark it will say Yes, My name is mark And if a user types a name which is not coded in the form it will say No, My name is Jaynesh.

harmor19
01-24-2006, 02:38 PM
let's say you had a form with the field
<input type="text" name="username" />


Use this code

<?php
$names = array('Andrew', 'John', 'Mark');
if( in_array($_POST['username'], $names) )
{
echo "Yes, my name is ".$_POST['username']."";
}
else
{
echo "No, my name is ".$_POST['username']."";
}
?>

Jaynesh
01-24-2006, 03:12 PM
Cheers :) worked great :)

Cole2026
01-26-2006, 01:11 AM
Im lerning php and just experimenting and messing around with a few stuff.

Ive written this code in dreamweaver ( Thanx for you tutorial MindTrix, https://vborg.vbsupport.ru/showthread.php?t=59914&page=1&highlight=elseif ) :)

Heres the code:


<?php

$name = "Jaynesh";

if ( $name == "Bob" )

{

print "Yes my name is $name";

}
elseif ( $name == "Johnny" )
{
print "Yes, My name is Johnny";
}
elseif ( $name == "Mark" )
{
print "Yes, My name is mark";
}
elseif { $name == "Sarah" )
{
print "Yes, My name is sarah";
}
else {
print "No, My name is $name";
}
?>


How can i make it so i can select a name from a form instead of manually putting it in the php file.

e.g Imagine there is a form on a page, and when the user types the name Mark it will say Yes, My name is mark And if a user types a name which is not coded in the form it will say No, My name is Jaynesh.

I know this isn't what you asked, but for future reference you should use the case and switch commands in case of an if that long.

<?php

$name = "Jaynesh";

switch ( $name )
{
case "Bob":
case "Johnny":
case "Mark":
case "Sarah":
print "Yes, My name is $name";
break;
default:
print "No, My name is $name";
}
?>

Jaynesh
01-26-2006, 06:43 AM
Okay thankyou :)

How will i use that with a form?