Jump to content

[SOLVED] Error Checking?


dmccabe

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ""; } ?>">

 

?

Link to comment
Share on other sites

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..."

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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=""';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.