FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to Input Text into a php script





Possum
Hi

I am just learning php. I can make a script that makes a choice and prints it out.


<?php
$str = 'apple';

if ($str === 'apple') {
echo "Correct?";
exit;
}

else {
echo "Wrong";
exit;
}

?>

I would like this page to have a form to input the text. In this example if the inputed field was apple the answer would be Correct..


cheers Possum..
kv
Code:


<html>

 <?php
$str = $_REQUEST['fruit'];

if ($str === 'apple') {
//echo "Correct?";
exit;
}

else {
echo "Wrong";
//exit;
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type='text' name='fruit'>
<input type='submit'>
</form>
</html>



The above code should be saved as a php file and accessed through php enabled webserver. It will give a textbox. once user types a value into textbox and presses submit button, it sends request to itself with the value user has typed in.
roboguyspacedude
Also you could do this:
Code:
 
<?php
$act = $_GET['act'];
$form = "<form action=\"?act=process\" method=\"post\"><input type=\"text\" name=\"fruit\"><br><input type=\"submit\" value=\"submit\"></form>";
switch($act){
case process:
$str = $_POST['fruit'];
if ($str == 'apple') {
echo "Correct?";
}else {
echo "Wrong";
}
break;
default:
echo $form;
break;
}
?>

or simpiler
Code:
 
<?php
$act = $_GET['act'];
if($act == 'process'){
$str = $_POST['fruit'];
if ($str == 'apple') {
echo "Correct?";
}else {
echo "Wrong";
}
}else{
?>
<form action="?act=process" method="post"><input type="text" name="fruit"><br><input type="submit" value="submit"></form>
<?
}
?>

or a little fancier:
Code:

<?php
$act = $_GET['act'];
$form = "<form action=\"?act=process\" method=\"post\"><input type=\"text\" name=\"fruit\"><br><input type=\"submit\" value=\"submit\"></form>";
switch($act){
case process:
$str = $_POST['fruit'];
if ($str == 'apple') {
echo "Correct?";
}else {
echo "Wrong<br>Try Again?<br>";
echo $form;
}
break;
default:
echo $form;
break;
}
?>

as said above, both should be put into a php file. Example: form.php
Demo 1: http://roboguy.frih.net/fun/form1.php
Demo 2: http://roboguy.frih.net/fun/form2.php
Demo 3: http://roboguy.frih.net/fun/form3.php
vln004
goto php.net and do a search on switch, break statements. easiest and most effective way to do it


something like this


Code:

<?php
switch ($_GET['variablename'])
{
case 'apple':
echo 'you chose the apple';
break;
case 'orange':
echo 'you chose the orange';
break;
default:
echo 'you didn't choose anything';
}
?>


and you can 'make your choice' by entering it straight into the url.....

filename.php?variablename=apple
gerpg
you can get the script to choose a fruit for you...

Code:

<?

$random = rand(1,4);

if ($random = 1){
$fruit = 'apple'
}
elseif ($random = 2){
$fruit = 'bananna'
}
elseif ($random = 3){
$fruit = 'lemon'
}
else{
$fruit = 'orange'
}

Echo"Today i want to eat a $fruit";

?>


the rand(1,5); says to the server generate a number between 1 and 5. then the if statements ask if the variable is equal to 1, 2, 3, 4 or 5.

Have fun.

Louis.
Possum
Thanks everyone

Ive put some of your work into a tutorial I am putting together..

http://udopage.com/udopage_117.php

Cheers Possum..
gerpg
Nice, so are you putting together a whole series of tutorials for begginers of PHP?

Louis.
Possum
I'm a php learner and am making notes as I go. Its not too much more trouble to make these notes available to others. Its my may of thanking all the people who have helped me by helping others..

Cheers Possum..
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Inserting data to MYSQL with a PHP script
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Simple PHP script not working
PHP Script needed
array check box on php script using $_POST
Need php script 50 frih$ if you do
Browser close run a php script.
PHP Script Needed for 250 frih$
Using free php script
php script for Changing file permissions & owner
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.