• 10Nov

    In this two-part tutorial, you will see how we can implement a simple form that we can use to select how many shirts that we want, and what kind of shirts we want.
    How Many Shirts do you want again? Part 1You need flash to play this tutorial.
    This tutorial goes over the following goals:
    Is my order in your list?
    Shirts list
    Processing text and or user input
    You can find this tutorial video on youtube here.


    How Many Shirts do you want again?
    Part 2
    You need flash to play this tutorial.
    This tutorial goes over the following goals:
    Is my order in your list?
    Shirts list
    Processing text and or user input
    Providing Errors to the user that they may have made
    You can find this tutorial video on youtube here.

    Here are the sources used in this tutorial:
    tut010.php
    <?php
    $shirts = array();
    $shirts[] = "XS";
    
    $shirts[] = "S";
    $shirts[] = "M";
    
    $shirts[] = "L";
    $shirts[] = "XL";
    
    ?>
    Here are your choices like last time:
    <?php
    print_r($shirts);
    $quant = (int)trim($_REQUEST['quant']);
    
    	$size = strtoupper(trim($_REQUEST['size']));
    
    	$color = trim($_REQUEST['color']);
    
    ?>
    <form action="?" method="post">
    <input type="hidden" value="1" name="sub" />
    
    How many shirts do you want? <input type="text" size="5" name="quant" value="<?php echo $quant; ?>" /><br />
    
    What shirt size do you want? <input type="text" size="5" name="size" value="<?php echo $size; ?>" /><br />
    
    What color do you want(up to you) <input type="text" size="20" name="color" value="<?php echo $color; ?>" /><br /><input type="submit" value="Order" /><br /></form>
    
    <?php
    //PHP Tutorial 010
    //Shirt order form
    
    if((int)$_REQUEST['sub']){
    
    	$errors = 0;
    	//now that the user has submitted we can process..
    
    	if(!in_array($size,$shirts)){
    
    		//not in the array
    		echo "You need to select a size from the above, not a number but the size name, such as XL for Extra-Large<br />\n";
    		$errors +=1;
    
    	}
    
    	if(strlen($color)<1){
    		echo "Hey, if you want an invisible shirt, that is fine with us, but you may not be satisfied with that.. So, we will not process your order.<br />";
    
    		$errors +=1;
    	}
    
    	if($errors < 1){
    
    		//Success!
    		echo "Your order was successful! ";
    	}else{
    		echo "<br />Sorry, but we could not process your order because of faulty data entry.";
    
    	}
    }
    
    ?>

    Here are all the php functions used in this tutorial:

    Posted by Kloplop321 @ 5:27 pm

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.