March 3, 200917 yr on a form i making i need to set some required fields this is above the doctype <?php // list expected fields $expected = array('name', 'surname', 'email', ); // set required fields $required = array('name', 'email'); // create empty array for any missing fields $missing = array(); ?> this is above the form <?php if ($_POST && isset($missing)) {?> <p class="warning">Please complete the missing item(s) indicated.</p> <?php } elseif ($_POST && $mailSent) { ?> <p><strong>Your message has been sent. Thank you for your feedback.</strong></p> <?php } ?> all of the above is working fine, but i wanted to indicte which fields in the form needed to be filled <p> <label for="name">First Name: <?php if (isset($missing) && in_array('name', $missing)) { ?> <strong>Please enter your name</strong><?php } ?> </label> </p> and <p> <label for="email">E-mail: <?php if (isset($missing) && in_array('email', $missing)) { ?> <strong>Please enter your E-mail</strong><?php } ?> </label> </p> its all code i've lifted from the internet however those 2 last bits are not working, anybody know what i'm doing wrong thanks Seth
March 3, 200917 yr <p> <label for="email">E-mail: <?php if (isset($missing) && in_array('email', $missing)) { ?> <strong>Please enter your E-mail</strong><?php } ?> </label> </p> i would change that to $missing = array(); // start an array somewhere // we have error so set an array with the error text $missing['email'] = '<strong>Please enter your E-mail</strong>'; <p> <label for="email">E-mail: <?php if (isset($missing['email'])) { echo $missing['email'];} ?> </label> </p> ^^^ i know thats not what you asked but couldnt help it ^^^ you need to check the missing array , use <pre><?=var_dump($missing);?></pre> so you can see the actual array data its working from, fingers crossed the problem should be quite obvious.
March 3, 200917 yr Author thanks spider the top part of that code is it to replace anything thats already there or is it an addition, also am i right in putting it above the doctype also what is <pre><?=var_dump($missing);?></pre>, i put it in the page but couldnt see any difference does php get easier, i can read and understand html but php completely baffles me
March 5, 200917 yr lol, yeah php gets easyer much easyer! i can recommend a few very good books, books deffo help ill run through what that code does, <pre></pre> formats the text:- from w3schools The <pre> tag defines preformatted text. Text in a pre element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks. the php tag is a shorthand tag, witch some programmers moan at me for using as its not backwards compatable, but i think sod it the below code lines do the same but one takes fractionally less time to process // non shorthand <?php echo 'hello';?> // shorthand <?='hello';?> i think the 2nd just makes the code cleaner. var_dump(); will print/echo the value of the variable holding a string or array. i have made a quick page to show you the output of the function, also it must be placed between the <body> tags to render in the browser. Var Dump Example and to answer the other question the top part is a security thing, but force of habit now, its to make sure that the variable is clean from other data, an example would be with a key/value array(associative) i use in one of my apps, i loop through an array that holds data for a sql query $var['table_name']='value to insert/update'; now what if somehow a hacker managed to add a extra array key/value in the script, it would loop through the whole array performing the changes i want but also what they want, if it was a user table update they could update the userlevel giving god access to the system. you only need todo it though if that variable has contact with user input or "tainted" data, but as a rule all data is tainted untill proven otherwise... generally speaking
Create an account or sign in to comment