html - PHP form validation (creating a dynamic link) -
stuck on part of lab, asking create dynamic link 'uncheck' values database. how go doing this? reads following:
"revise last program there dynamic link @ end of each record says, "cancel". when clicked, should "uncheck" both monday , tuesday (that is, change values store in db represent "unchecked" both monday , tuesday" (how represent variable?)
i receiving undefined variable warnings when page loads. appreciated in pointing me in right direction. thank you!
my code .php file:
<?php if ($_post){ $checkerr = $firsterr = $lasterr = $orgerr = $emailerr = $phoneerr = ""; $title=$_post['title']; $firstname=$_post['firstname']; $lname=$_post['lastname']; $organization=$_post['organization']; $email=$_post['email']; $phone=$_post['phone']; $monday=$_post['monday']; $tuesday=$_post['tuesday']; $size = $_post['t-shirt']; if (empty($_post["firstname"])) { $firsterr = "first name required"; }else { $firstname=$_post['firstname']; } if (empty($_post["lastname"])) { $lasterr = "last name required"; }else { $lastname=$_post['lastname']; } if (empty($_post["organization"])) { $orgerr = "organization required"; }else { $organization=$_post['organization']; } if (empty($_post["email"])) { $emailerr = "email required"; }else { $email=$_post['email']; } if (empty($_post["phone"])) { $phoneerr = "phone number required"; }else { $phone=$_post['phone']; } } $dbserver = "142.204.000.06; $uid = "dummy"; $pw = "dummy"; $dbname = "dummy"; //connect mysql server , our link_identifier $link = mysqli_connect($dbserver, $uid, $pw, $dbname) or die('could not connect: ' . mysqli_error($link). "<br>"); $sql_query = 'insert lab3 set title="'. $title .'",firstname="' . $firstname . '", lastname="' . $lastname . '", organization="'. $organization .'",phone="'. $phone . '", email="'. $email .'", attending_mon="'. $monday .'", attending_tue="'. $tuesday .'", size="'. $size .'"' ;echo "<br>"; $result = mysqli_query($link, $sql_query) or die('error query '. mysqli_error($link)). '<br>'; $sql_query = "select * lab3"; $result = mysqli_query($link, $sql_query) or die('error query '. mysqli_error($link)). '<br>'; ?> <table border="1"> <tr> <th>title</th><th>first name</th><th>last name</th> <th>organization</th> <th>email</th><th>phone number</th><th> day attanding </th><th>t-shirt size</th> <?php while($row = mysqli_fetch_assoc($result)) { ?> <tr> <td><?php print $row['title']; ?></td> <td><?php print $row['firstname']; ?></td> <td><?php print $row['lastname']; ?></td> <td><?php print $row['organization'];?></td> <td><?php print $row['email']; ?></td> <td><?php print $row['phone']; ?></td> <td><?php print $row['attending_mon']; ?> <?php print $row['attending_tue']; ?> </td> <td><?php print $row['size']; ?></td> </tr> <?php }//ending while loop echo"</table>\n"; mysqli_free_result($result); mysqli_close($link); ?> <html> <head> <title>fsoss registration</title> </head> <body> <h1>fsos registration</h1> <form method="post" > <table> <tr> <td valign="top">title:</td> <td> <table> <tr> <td><input type="radio" name="title" value="mr" <?php if ($_post['title'] == 'mr') {echo "checked";} ?>>mr</td> </tr> <tr> <td><input type="radio" name="title" value="mrs" <?php if ($_post['title'] == 'mrs') {echo "checked";} ?> >mrs</td> </tr> <tr> <td><input type="radio" name="title" value="ms" <?php if ($_post['title'] == 'ms') {echo "checked";} ?> >ms</td> </tr> </table> </td> </tr> <tr> <td>first name:</td> <td><input name="firstname" type="text" value="<?php if(isset($_post['firstname'])){echo $_post['firstname'];} ?>"><?php if(isset($_post['submit'])){ echo $firsterr; }?></td> </tr> <tr> <td>last name:</td> <td><input name="lastname" type="text" value="<?php if(isset($_post['lastname'])){echo $_post['lastname'];} ?>"><?php if(isset($_post['submit'])){ echo $lasterr; }?></td> </tr> <tr> <td>organization:</td> <td><input name="organization" type="text" value="<?php if(isset($_post['organization'])){echo $_post['organization'];} ?>"><?php if(isset($_post['submit'])){ echo $orgerr; }?></td> </tr> <tr> <td>email address:</td> <td><input name="email" type="text" value="<?php if(isset($_post['email'])){echo $_post['email'];} ?>"><?php if(isset($_post['submit'])){ echo $emailerr; }?></td> </tr> <tr> <td>phone number:</td> <td><input name="phone" type="text" value="<?php if(isset($_post['phone'])){echo $_post['phone'];} ?>"><?php if(isset($_post['submit'])){ echo $phoneerr; }?></td> </tr> <tr> <td>days attending:</td> <td> <input name="monday" type="checkbox" value="monday" <?php if (isset($_post['monday'])) {echo "checked";}?>>monday <input name="tuesday" type="checkbox" value="tuesday" <?php if (isset($_post['tuesday'])) {echo "checked";}?>>tuesday <td/> </tr> <tr> <td>t-shirt size:</td> <td> <select name="t-shirt"> <option>--please choose--</option> <option name="small" value="s" <?php if($_post['t-shirt'] == 's'){echo('selected');} ?> >small</option> <option value="m" <?php if($_post['t-shirt'] == 'm'){echo('selected');} ?>>medium</option> <option value="l" <?php if($_post['t-shirt'] == 'l'){echo('selected');} ?>>large</option> <option value="xl" <?php if($_post['t-shirt'] == 'xl'){echo('selected');} ?>>x-large</option> </select> </td> </tr> <tr><td><br></td></tr> <tr> <td></td> <td><input name="submit" type="submit"></td> </tr> </form> </body> </html>
hi getting undefind
variavble
warning beacause first time page loads
without form
values
not have $_post
variable check isset
. stop warnings change if($_post)
if(isset($_post))
or if(isset($_post) && !empty($_post))
. wii prefer second option
hi have checked code , done myself please follow steps done if find fulfil need
i have created table in database query
create table if not exists `lab` ( `id` int(4) unsigned not null auto_increment, `title` varchar(20) default null, `firstname` varchar(20) not null default '', `lastname` varchar(255) not null, `organization` varchar(111) not null, `phone` int(10) not null, `email` varchar(30) not null, `attending_mon` char(1) not null, `attending_tue` char(1) not null, `size` varchar(30) not null, primary key (`id`) )
and use code achieve task
index.php <?php $dbserver = "localhost"; $uid = "root"; $pw = ""; $dbname = "project"; //connect mysql server , our link_identifier $link = mysqli_connect($dbserver, $uid, $pw, $dbname) or die('could not connect: ' . mysqli_error($link). "<br>"); if(isset($_request['did'])){ $did= $_request['did']; if($did) {//if want delete record use //mysqli_query($link,"delete lab id='$did'"); // if want set n in days select mysqli_query($link,"update lab set attending_mon ='n' , attending_tue = 'n' id='$did'"); }} if(isset($_post) && !empty($_post)){ $checkerr = $firsterr = $lasterr = $orgerr = $emailerr = $phoneerr = ""; $title=$_post['title']; $firstname=$_post['firstname']; $lastname=$_post['lastname']; $organization=$_post['organization']; $email=$_post['email']; $phone=$_post['phone']; $monday=isset($_post['monday']) ? 'y' : ''; $tuesday=isset($_post['tuesday']) ? 'y' : ''; $size = $_post['t-shirt']; if(empty($monday) && empty($tuesday)) { $checkerr = "day required"; } if (empty($firstname)) { $firsterr = "first name required"; } if (empty($lastname)) { $lasterr = "last name required"; } if (empty($organization)) { $orgerr = "organization required"; } if (empty($email)) { $emailerr = "email required"; } if (empty($phone)) { $phoneerr = "phone number required"; } } if(isset($checkerr) && $checkerr == '' && $firsterr == '' && $lasterr == '' && $orgerr == '' && $emailerr == '' && $phoneerr == ""){ $sql_query = 'insert lab set title="'. $title .'",firstname="' .$firstname . '",lastname="' . $lastname . '", organization="'. $organization .'",phone="'. $phone . '", email="'. $email .'", attending_mon="'. $monday .'", attending_tue="'. $tuesday .'", size="'. $size .'"' ;echo "<br>"; //echo $sql_query;die; $result = mysqli_query($link, $sql_query) or die('error query '. mysqli_error($link)). '<br>'; $_post = array(); } $sql_query = "select * lab"; $result = mysqli_query($link, $sql_query) or die('error query '. mysqli_error($link)). '<br>'; ?> <table border="1"> <tr> <th>title</th><th>first name</th><th>last name</th> <th>organization</th> <th>email</th><th>phone number</th><th> day attanding </th><th>t-shirt size</th><th>action</th> <?php while($row = mysqli_fetch_assoc($result)) { ?> <tr> <td><?php print $row['title']; ?></td> <td><?php print $row['firstname']; ?></td> <td><?php print $row['lastname']; ?></td> <td><?php print $row['organization'];?></td> <td><?php print $row['email']; ?></td> <td><?php print $row['phone']; ?></td> <td><?php print $row['attending_mon'] == 'y' ? 'monday': '' ?> <?php print $row['attending_tue'] == 'y' ? 'tuesday': '' ?> </td> <td><?php print $row['size']; ?></td> <td><a href="index.php?did=<?php echo $row['id'];?>">cancil</a></td> </tr> <?php }//ending while loop echo"</table>\n"; mysqli_free_result($result); mysqli_close($link); ?> <html> <head> <title>fsoss registration</title> </head> <body> <h1>fsos registration</h1> <form method="post" > <table> <tr> <td valign="top">title:</td> <td> <table> <tr> <td><input type="radio" name="title" value="mr" <?php if(isset($_post['title']) && $_post['title'] == 'mr'){echo "checked"; }?> >mr</td> </tr> <tr> <td><input type="radio" name="title" value="mrs" <?php if(isset($_post['title']) && $_post['title'] == 'mrs'){echo "checked"; }?>>mrs</td> </tr> <tr> <td><input type="radio" name="title" value="ms" <?php if(isset($_post['title']) && $_post['title'] == 'ms'){echo "checked"; }?>>ms</td> </tr> </table> </td> </tr> <tr> <td>first name:</td> <td><input name="firstname" type="text" value="<?php if(isset($_post['firstname'])){echo $_post['firstname'];} ?>"><?php if(isset($_post['submit'])){echo $firsterr; }?></td> </tr> <tr> <td>last name:</td> <td><input name="lastname" type="text" value="<?php if(isset($_post['lastname'])){echo $_post['lastname'];} ?>"><?php if(isset($_post['submit'])){ echo $lasterr; }?></td> </tr> <tr> <td>organization:</td> <td><input name="organization" type="text" value="<?php if(isset($_post['organization'])){echo $_post['organization'];} ?>"><?php if(isset($_post['submit'])){ echo $orgerr; }?></td> </tr> <tr> <td>email address:</td> <td><input name="email" type="text" value="<?php if(isset($_post['email'])){echo $_post['email'];} ?>"><?php if(isset($_post['submit'])){ echo $emailerr; }?></td> </tr> <tr> <td>phone number:</td> <td><input name="phone" type="text" value="<?php if(isset($_post['phone'])){echo $_post['phone'];} ?>"><?php if(isset($_post['submit'])){ echo $phoneerr; }?></td> </tr> <tr> <td>days attending:</td> <td> <input name="monday" type="checkbox" value="monday"<?php if (isset($_post['monday'])) {echo "checked";}?>>monday <input name="tuesday" type="checkbox" value="tuesday"<?php if (isset($_post['tuesday'])) {echo "checked";}?>>tuesday <?php if(isset($_post['submit'])){ echo $checkerr; }?><td/> </tr> <tr> <td>t-shirt size:</td> <td> <select name="t-shirt"> <option>--please choose--</option> <option value="s" <?php if(isset($_post['t-shirt']) && $_post['t-shirt'] == 's'){echo('selected');} ?> >small</option> <option value="m"<?php if(isset($_post['t-shirt']) && $_post['t-shirt'] == 'm'){echo('selected');} ?>>medium</option> <option value="l"<?php if(isset($_post['t-shirt']) && $_post['t-shirt'] == 'l'){echo('selected');} ?>>large</option> <option value="xl"<?php if(isset($_post['t-shirt']) && $_post['t-shirt'] == 'xl'){echo('selected');} ?>>x-large</option> </select> </td> </tr> <tr><td><br></td></tr> <tr> <td></td> <td><input name="submit" type="submit" value="submit"></td> </tr> </form>
Comments
Post a Comment