iconicCreator Posted February 2, 2009 Share Posted February 2, 2009 New to PHP and taking little baby steps, thanks to the people in this forum, I'm learning what I never thought possible. Right now I'm learning to process forms with PHP. Is it possible to validate radio buttons, check boxes and list/menu with PHP. My HTML looks like this. <div class="operatingSystem"> <span class="system"> <label for="operatingSystem">Operating System.</label> </span> <span class="label_lineheight"><br /> <input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Windows Vista" />Windows Vista <input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Windows XP" />Windows XP <input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Mac OSX" />Mac <input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Linux" />Linux</span> </div> Basically what this does ask the user to select an operating system by checking a radio button. The idea is for that the user must select an operating system. And this is the select list/menu <div class="problemProduct"> <label for="product_selection"><span class="product_label">Product Name.</span></label> <select name="product_selection" id="products" class="selection"> <option value="None">-------------Select a product----------</option> <option value="Everex DVD Burner">Everex DVD Burner</option> <option value="Vidia DVD Burner">Vidia DVD Burner</option> <option value="Excerion Super Drive">Excerion Super Drive</option> <option value="Maxille Optical Multi Burner">Maxille Optical Multi Burner</option> <option value="Pavilion HD Drives">Pavilion HD Drives</option> </select> </div> I would really appreciate any tutorials, examples or ideas. Thanks very much. IC Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 2, 2009 Share Posted February 2, 2009 yes u can, here is an example of a test script that i have written that uses validation: <html> <head> <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ document.myForm.city.value = ajaxRequest.responseText; } } ajaxRequest.open("GET", "secondchoice.php", true); ajaxRequest.send(null); } //--> </script> <script type="text/javascript" src="js/ajax.js"></script> <script type="text/javascript"> var ajax = new Array(); //this will refresh citys function getCityList(sel) { var stateCode = sel.options[sel.selectedIndex].value; document.getElementById('city').options.length = 0; // Empty city select box if(stateCode.length>0){ var index = ajax.length; ajax[index] = new sack(); ajax[index].requestFile = 'mygetcity.php?stateCode='+stateCode; // Specifying which file to get ajax[index].onCompletion = function(){ createCities(index) }; // Specify function that will be executed after file has been found ajax[index].runAJAX(); // Execute AJAX function } } function createCities(index) { var obj = document.getElementById('city'); eval(ajax[index].response); // Executing the response from Ajax as Javascript code } //this will get subcatagory function getSubCategoryList(sel) { var category = sel.options[sel.selectedIndex].value; document.getElementById('subcat').options.length = 0; // Empty subcat select box if(category.length>0){ var index = ajax.length; ajax[index] = new sack(); ajax[index].requestFile = 'mygetsubcat.php?category='+category; // Specifying which file to get ajax[index].onCompletion = function(){ createSubCategories(index) }; // Specify function that will be executed after file has been found ajax[index].runAJAX(); // Execute AJAX function } } function createSubCategories(index) { var obj = document.getElementById('subcat'); eval(ajax[index].response); // Executing the response from Ajax as Javascript code } </script> <title>test</title> <link rel="stylesheet" type="text/css" href="../web1.css" /> <link rel="stylesheet" type="text/css" href="../heading.css" /> <link rel="stylesheet" type="text/css" href="../navigation.css" /> <link rel="stylesheet" type="text/css" href="directory.css" /> </head> <body> <div class="navigation" id="navigation"> <a class="navjust" href="../index.php">Home</a><br /> <br /> <a class="navjust" href="http://mail.meo2000.net">Check Mail</a><br /> <br /> <a class="navjust" href="../web/webservice.php">Web Services</a><br /> </div> <div id="justify"> <div id="h"> <h1>meo2000 listing service</h1> <h3>want to get listed, click <a href="getlisted.htm">here</a> to find out how.</h3> <h3>This directory is under construction!</h3> <br/> </div> </div> <?php Function DisplayForm($errors = null){ $error_string = $errors; //state, you could use $error_string = implode ("<br />",$errors); if you want to save performance by using an array insead. echo(' <div id="left"> <span class="error"> '.$error_string.'<br /> </span> <form method="post" action="'.$_SERVER['PHP_SELF'].'"> State: <select id="state" name="state" onchange="getCityList(this)"> <option value="">Select a state</option> <option value="Alabama">Alabama</option> <option value="North Carolina">North Carolina</option> <option value="Texas">Texas</option> </select><br /> <br /> City: <select id="city" name="city"> </select><br /> <br /> Category: <select id="category" name="category" onchange="getSubCategoryList(this)"> <option value="">Select a catagory</option> <option value="Auto">Auto</option> <option value="Hotel">Hotel</option> <option value="Restaurant">Restaurant</option> </select><br /> <br /> SubCategory: <select id="subcat" name="subcat"> </select><br /> <br /> <input type="submit" name="submit" value="Select"> </form> </div> '); } //check if form has been submitted if (!$_POST['submit']) { //if not display form DisplayForm(); } else { //form has been submitted //items selected, check that number was entered, //Declare it so we dont get notices 'undeclared variable' $error = null; //check state has been selected if (trim($_POST['state']) == "") { $error .='ERROR: Please select a state.'; } //check category has been selected elseif (trim($_POST['category']) == "") { $error .='ERROR: Please select a category.'; } //if there was an error, show it. if ($error != null){ DisplayForm($error); } else { //otherwise carry on? echo '<div id="right">'; $state = $_POST['state']; $city = $_POST['city']; $cat = $_POST['category']; $subcat = $_POST['subcat']; echo 'Here is your selections: <br />'; echo "<i>$state</i><br />"; echo "<i>$city</i><br />"; echo "<i>$cat</i><br />"; echo "<i>$subcat</i><br />"; echo '</div>'; } } ?> <div class="addspace" id="addspace"> <p class="none" id="addmargin"> your<br > add<br > could<br > be<br > filling<br > this<br > spot! </p> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 2, 2009 Share Posted February 2, 2009 here is the part that does my validation: //check state has been selected if (trim($_POST['state']) == "") { $error .='ERROR: Please select a state.'; } //check category has been selected elseif (trim($_POST['category']) == "") { $error .='ERROR: Please select a category.'; } //if there was an error, show it. if ($error != null){ DisplayForm($error); } Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 I typically have my forms post to themselves so that if validation fails I can redisplay the form with the data the user entered. It also makes it easier to handle select, radio buttons, etc. I will create an array at the top of the page and use that to create my radio buttons, select lists, etc. and the use that same list for validation. Declare at top of page $os_list = array('Windows Vista', 'Windows XP', 'Mac OS', 'Linux'); Create OS Radio Button List <div class="operatingSystem"> <span class="system"> <label for="operatingSystem">Operating System.</label> </span> <span class="label_lineheight"> <br /> <?php foreach($os_list as $os) { echo "<input name=\"operatingSystem\" type=\"radio\" class=\"system_checkBox\" value=\"$os\" />\n"; echo "$os\n"; } ?> </span> </div> Validate the radio button list <?php if (in_array($_POST['operatingSystem'], $os_list)) { $validation = true; //Do something } else { $validation = false; //Do something else } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.