dmccabe Posted June 17, 2008 Share Posted June 17, 2008 Ok so I am getting better and better at PHP all the time (with thanks to you guys) however I have a form I created a long time ago, that is clearly coded badly and now I need to do it correctly. My main issue is error checking and and I was wondering how you guys went about this? Basically at the moment the user fills out the form and I was using if (!isset(blah)) { echo "Error: You must do this etc"; } and code like that, which is fine except when they submit the form the isset always evaluates to true. Also I want it so if they havent filled in the form correctly, it gives them the error message, and puts them back to the form, but has the values they put in already filled out. I think i know how to do this by simply using value="<?php $_POST['value'] ?>, except how does that work with Radio buttons? seeing as they already have a value? Sorry if I am rambling, if you need more specific's let me know. Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/ Share on other sites More sharing options...
jesushax Posted June 17, 2008 Share Posted June 17, 2008 use if (strlen($fieldvalue) < 0 ) {echo 'error: etc) that will check the fields lenght and if its less than 0 echo an error for yoru radio buttons youll have to use the same $_POST but use them in an if statement to choos the correct one so if the radio button posted value 1 use an if to check for value 1 then tick that radio button using checked="checked" if ($_POST["RadioButton1) == "1" {echo 'checked="checked"';} but that in the radio button html Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567091 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 The form itself would you have multiple copies of it? one for when the page first loads then another for once the submit button has been pressed? or would you simply have it so the if statements are within the form? e.g: <form> <input type="text" name="blah" value="<?php if ($_POST['blah') { echo "$_POST['blah']"; } else { echo ""; } ?>"> ? Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567101 Share on other sites More sharing options...
jesushax Posted June 17, 2008 Share Posted June 17, 2008 you dont need an if statement to retrieve the data the if statement is for error checking out your post.php or post action. <form> <input type="text" name="blah" value="<?php echo $_POST['blah']; ?>" /><? if (strlen($_POST['blah']; ) < 0 ) {echo 'error: you need to fill in text blah' this will post a value if there is one and if there isnt will write next to the textfield "you need to..." Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567115 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 Ok so that makes sense, however I am trying to use session variables instead of $_POST and I am getting a default value of <br /> if there is no session value ? <input id="element_1" name="contname" class="element text medium" type="text" maxlength="255" value="<?php echo $_SESSION['contname']; ?>"/> if (isset($_POST['contname'])) { $_SESSION['contname'] = $_POST['contname']; } so if $_SESSION['contname'] is not set then the form displays <br /> in the input field. Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567144 Share on other sites More sharing options...
jesushax Posted June 17, 2008 Share Posted June 17, 2008 if your doing a not if, then you need to add a ! try this if (!isset($_POST['contname'])) { $_SESSION['contname'] = $_POST['contname']; } Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567146 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 Also how can I clear all session variables except one? Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567147 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 if your doing a not if, then you need to add a ! try this if (!isset($_POST['contname'])) { $_SESSION['contname'] = $_POST['contname']; } that cant be right, I only want it to set the session variable if the $_POST is set. Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567148 Share on other sites More sharing options...
jesushax Posted June 17, 2008 Share Posted June 17, 2008 ahh right your last post was little confusing so if the $_POST['contname'] has text in it then set the session if it does not have any text in it do what? Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567155 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 Nothing I guess, present the form with an empty value. Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567161 Share on other sites More sharing options...
jesushax Posted June 17, 2008 Share Posted June 17, 2008 then what you have should work remember to include session_start(); at the top of your code Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567167 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 Yeah I have the session started, but for some reason when I load the form for the first time, all the input fields have a <br /> in them. <li id="li_2" > <label class="description" for="element_2">Account No * </label> <div> <input id="element_2" name="accno" class="element text medium" type="text" maxlength="255" value="<?php echo $_SESSION['accno']; ?>"/> </div> <p class="guidelines" id="guide_2"> <small>Customer account number. (MANDATORY) </small></p> </li> Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567169 Share on other sites More sharing options...
jesushax Posted June 17, 2008 Share Posted June 17, 2008 so your default values for your page have a <br /> in is this a html <br /> or the text <br /> your posted values must have a <br /> in there somewhere you could do this... that should work but really you need to find out where the <br /> is gettin put in $blah = str_replace("<br />","",$_POST['blah']); if (!strlen($blah) < 0 { echo 'value=""'; } Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567176 Share on other sites More sharing options...
dmccabe Posted June 17, 2008 Author Share Posted June 17, 2008 unfortunately this wont help as the "first" time I load the page it put's the BR in the field value, which is before I have posted any info and before I clicked submit or started the session. Here's how I have it at the moment: if (isset($_POST['accname'])) { $accname = $_POST['accname']; if (strlen($accname) < 1 ) { echo "<strong><font color='#FF0000'>Error:</font> You must specify the account name. <br /></strong>"; } else { $_SESSION['accname'] = $_POST['accname']; } } <li id="li_5" > <label class="description" for="element_5">Account Name *</label> <div> <input id="element_5" name="accname" class="element text medium" type="text" maxlength="255" value="<?php $_POST['accname'] ?>"/> </div> <p class="guidelines" id="guide_5"><small>Enter the account name. (MANDATORY)</small> </p> </li> and as I say, if I close the browser, open it again, go to the page, then instantly there is a BR in the input field. Quote Link to comment https://forums.phpfreaks.com/topic/110538-solved-error-checking/#findComment-567195 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.