PDA

View Full Version : contradicting if statements??


deafboyzaudio
12-20-2009, 09:55 PM
Hey,
I am trying to make my form fields sticky so that user input is preserved. I have made an array $missing that the names of the missing fields will be inserted into... THen based on the existence of any variables in the $missing array I display either a red field/error or a regular one or one with the previous user input in it...

<?php if (!(isset($missing) && in_array('pic_title', $missing))){?>
<input name="pic_title" type="text" id="pic_title" tabindex="10" size="30" class="indent" maxlength="50" />
<?php }

if (isset($missing) && in_array('pic_title', $missing)){?>
<span class="textfieldRequiredState"><input name="pic_title" type="text" id="pic_title" tabindex="10" size="30" maxlength="50" /></span><span class="error"> Required</span>
<?php }

if(isset($missing)){?>
<input name="pic_title" type="text" id="pic_title" tabindex="10" size="30" maxlength="50" class="indent" <?php echo 'value="'.htmlentities($_POST['pic_title']).'"';?>/>
<?php }?>my problem is that when say the pic_title field is the only one filled out... the page refreshes and it has 2 fields... one with the previous user input and another that is blank
Now I have tested it and the 2 that are showing are the first and last ones..... If you look at the code for these two it doesnt make sense.... one shows only if (!(isset($missing) && in array('pic_title, $missing)))and the other shows if (isset($missing))What gives???? How can 2 opposites be true? If anyone has a better way of doing this let me know...

davidj
12-21-2009, 12:19 AM
i would catch 99% of errors (empty fields) through JavaScript

saves a server call

deafboyzaudio
12-21-2009, 01:43 AM
i have js in place via way of DW's spry functionality.... I was assuming most of it would be caught that way, but you always hear of the 'what if JS is disabled??' so i thought i'd be extra safe, besides these forms play a heavy role on my site.

davidj
12-21-2009, 11:06 AM
So to recap

You have a form with required fields

If the form is submitted then the script notifies of any empty required fields but also retains those values submitted previously.

Yes

deafboyzaudio
12-21-2009, 05:47 PM
yes the data is retained for the fields that are filled out. The ones not filled out get an error message...

on submit I am getting a field with the retained data and a new field like when the page first loads. (the first and last if's)

here is my script for making the $missing array and such if it helps any...
// list expected fields
$expected = array('pic_title', 'caption', 'tags', 'vehicle_year', 'make', 'model', 'setup');
// set required fields
$required = array('pic_title', 'tags');
// create empty array for any missing fields
$missing = array();
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
//take initial values and make empty string
if($setup == 'Give a brief description of your Car Audio upgrades...') {$setup = '';}
if($vehicle_year == '???') {$vehicle_year='';}
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
// go ahead only if all required fields OK
if (empty($missing)) {

davidj
12-21-2009, 06:08 PM
i will write you a class to do this

if you can wait a couple of days

deafboyzaudio
12-21-2009, 09:36 PM
i will write you a class to do this What do you mean like a class style? Most My fields have to have a class style of indent applied to them, no way of getting around it...
I was using a span class to make one red...
and then having one with the .htmlentities code....
so three different fields with ifs to choose which one to display... would be cool to make it decide what classes to apply and not have to write each option out with if statements if thats what your talking about...

davidj
12-22-2009, 07:43 AM
When I say class I'm talking about an object

OOP

A class is like a plug-in which performs a task

davidj
12-22-2009, 04:31 PM
page1 is HTML and a bit of PHP to define required fields

I have attached the files in a zip below


<?php

include("formClass.php"); //<< include class

$formObj = new formChecker; // create object

$RequiredFields = 't1,t2,t3'; // define required fields using ID value of field

if(isset($_POST[Submit])){ // test if form has been posted

$valid = $formObj->checkform($_POST,$RequiredFields); // run validate function

}

if($valid === TRUE){ // if valid is true (All required fields filled) run sql here

///************************************************** **************///
/// 'run sql here using $_POST vars to process a successful form'; ///
///************************************************** **************///

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

// place holder to display message, Javascript, and CSS if unsuccessful

echo $valid;

// This output also creates a div <div id=\"messges\"> which contains your error messages
// I have wrapped the message in this <Div so you have a hook to style and position it

?>

<form id="form1" name="form1" method="post" action="">
<p>
<input name="t1" type="text" id="t1" />
</p>
<p>
<input name="t2" type="text" id="t2" />
</p>
<p>
<input name="t3" type="text" id="t3" />
</p>
<p>
<input name="t4" type="text" id="t4" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

Page2 (formClass.php) is the CLASS



<?php

class formChecker{

function __construct(){}

function messages($k){

$mess = array(

'mess1'=>'Some of your fields are empty',
'mess2'=>'Your data has been submitted'

);

return $mess[$k];

}

function css($keys){

$style .= "#$keys{

border:solid 1px red;\n
background-color:yellow;\n

}\n\n";

return $style;

}

function checkForm($f, $r){

$req = explode(',',$r);
$javaScript = "<script>\n\n";
$javaScript .= "
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(setFields);

\n\n";

$javaScript .= "function setFields(){\n\n";

foreach($f as $keys=>$vals){

$javaScript .= "document.getElementById('$keys').value = '$f[$keys]';\n\n";

}

$javaScript .= "}";

$javaScript .= "</script>\n\n";

$style = "<style>\n\n";

foreach($req as $keys){

if(isset($f[$keys]) && $f[$keys] ==''){

$style .= formChecker::css($keys);

$valid = FALSE;

}

}

$style .= "</style>";

if($valid === FALSE){

$message = formChecker::messages('mess1');

return $style."\n\n".$javaScript."\n\n<div id=\"messges\">$message</div>";

}else{

array_pop($f);

return TRUE;

}
}
}


?>

deafboyzaudio
12-22-2009, 06:30 PM
ok i tried the form/class just as it was and at first i got a few errors about undefined variable $valid...

Changed the code on line 15 and 33 to say:
if((isset($_POST['submit'])) && ($valid === TRUE)){ // if valid is true (All required fields filled) run sql here

echo 'running sql here using $_POST vars';and
<?php if(isset($_POST['submit'])) {echo $valid;} // place holder to display message, Javascript, and CSS ?>That got rid of the errors... Im assuming there is no prob doing that.... then i tried submitting the form and correct me if im wrong but shouldnt i at least get some errors leaving it how it is? All i get is a refresh...

davidj
12-22-2009, 07:02 PM
The errors are not errors

They are warnings advising your not applying coding standards

Turn your error handling sensitivity down in your php.ini file


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Add the & ~E_NOTICE directive to stop those warnings

error_reporting = E_ALL & ~E_NOTICE

davidj
12-22-2009, 07:11 PM
correct me if im wrong but shouldnt i at least get some errors leaving it how it is? All i get is a refresh...

I have coded this place holder

///***********************************************///
/// 'run sql here using $_POST vars to process a successful form'; ///
///***********************************************///

That is your //TODO

Whether that be a SQL query and a redirect to a 'Successful' page

Thats you're call

deafboyzaudio
12-22-2009, 07:41 PM
disregard this post.... didnt see your first reply only the 2nd... I am aware that i have to put sql queries in there, i was just testing the error reporting which comes before that... I will do your suggestion with php.ini and get back

deafboyzaudio
12-22-2009, 10:25 PM
The errors are not errors
You are right, their notices... cant get all of them to dissapear by editing php.ini... oh well, should only show in testing environment anyways. Got the script working with everything and thanks a ton. now my page is much cleaner and less confusing :)
If i may ask one more question of you great php warrior... why do I get a '1' showing where the error messsage appears after a successful form action(submit to DB)... can't figure out why that shows....

davidj
12-22-2009, 11:53 PM
cant get all of them to dissapear by editing php.ini.

did you bounce the services?

wamp icon > restart all services

deafboyzaudio
12-23-2009, 12:55 AM
yes that did work, didnt think of that... I have some bad news though, everything works for the most part but only in IE 8... I went back and downloaded the original code to eliminate all other variables and only works in IE... I could of swore it used to work in FF.... ever had this before?

deafboyzaudio
12-23-2009, 01:41 AM
hey I did some testing and I noticed that JS had to be enabled in FF for the form fields to be sticky... I noticed in formClass.php that it has JS...<br />
Does this rely on JS??? It works great and everything but only with JS enabled... I need a solution for people that have JS disabled...

davidj
12-23-2009, 07:49 AM
Ok

No JavaScript version

The script will identify the fields on the form automatically but you need to do the following when defining a field

name, id and value have to be set like this using the same convention. Notice I use the 't1' in three places. You can use any naming convention as long as they are the same in all 3 instances.


<input name="t1" type="text" id="t1" value="<?php echo $_SESSION['t1']; ?>"/><?php

include("formClass.php"); //<< include class

$formObj = new formChecker; // create object

$RequiredFields = 't1,t2,t3'; // define required fields using ID value of field

if(isset($_POST[Submit])){ // test if form has been posted

$valid = $formObj->checkform($_POST,$RequiredFields); // run valid function

}

if($valid === TRUE){ // if valid is true (All required fields filled) run sql here

echo 'running sql here using $_POST vars';

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php echo $valid; // place holder to display message, Javascript, and CSS ?>

<form id="form1" name="form1" method="post" action="">
<p>
<input name="t1" type="text" id="t1" value="<?php echo $_SESSION['t1']; ?>"/>
</p>
<p>
<input name="t2" type="text" id="t2" value="<?php echo $_SESSION['t2']; ?>"/>
</p>
<p>
<input name="t3" type="text" id="t3" value="<?php echo $_SESSION['t3']; ?>"/>
</p>
<p>
<input name="t4" type="text" id="t4" value="<?php echo $_SESSION['t4']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>Modded Class

<?php

session_start();

class formChecker{

function __construct(){}

function messages($k){

$mess = array(

'mess1'=>'Some of your fields are empty',
'mess2'=>'Your data has been submitted'

);

return $mess[$k];

}

function css($keys){

$style .= "#$keys{

border:solid 1px red;\n
background-color:yellow;\n

}\n\n";

return $style;

}

function checkForm($f, $r){

$req = explode(',',$r);

foreach($f as $keys=>$vals){

$_SESSION[$keys] = $vals;

}

$style = "<style>\n\n";

foreach($req as $keys){

if(isset($f[$keys]) && $f[$keys] ==''){

$style .= formChecker::css($keys);

$valid = FALSE;

}

}

$style .= "</style>";

if($valid === FALSE){

$message = formChecker::messages('mess1');

return $style."\n\n".$javaScript."\n\n<div id=\"messges\">$message</div>";

}else{

array_pop($f);

return TRUE;

}
}
}


?>

deafboyzaudio
12-23-2009, 09:50 PM
cool im glad there is a no JS version.... Appreciate it a lot david... I won't have time to implement it today unless i can really haul on my christmas shopping/wrapping but i'll get back on here as soon as i can.
HAPPY HOLIDAYS!!

deafboyzaudio
12-29-2009, 07:50 AM
ok I got this implemented and it works great except I worked for hours trying to get it to work with drop down menus... I got as far as if($_POST['state'] == '???'){$_POST['state']='';} to take care of the initial value but could not make the drop downs sticky no matter what i tried... is this possible?

deafboyzaudio
01-15-2010, 12:07 AM
to David:
I still havent figured out how to make this work for drop downs....

Checked on adobe forums... no luck

checked on google.... close but no cigar

i can get them to validate, but not be 'sticky'...

any help.....?