sirjames2004 Posted April 26, 2013 Share Posted April 26, 2013 Greetings. My web development instructor just got us into session control with PHP, and I need a little help. The following page is the first of what will be a set of four. What I want this page to do is check the session variables it takes in from the user, and either display an error below the form on this same page, or advance to the next page which will contain another form for the user to fill out. I got most of it down pat, but my question is, is there a way to set it up so that when the user clicks the “next” button, that it will check the data (using my is_valid() function) and either advance them to the next page or keep them there and display the appropriate error(s), depending on the results? Look at my code below, and you'll see what I mean: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Soccer Team Contact Info</title> <link rel="stylesheet" type="text/css" href="mystyle.css" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <?php session_start(); // if user clicked 'next', will copy variables to session array and local variables if(isset($_POST['name']) || isset($_POST['phone']) || isset($_POST['email'])){ $name=trim($_POST["name"]); $phone=trim($_POST["phone"]); $email=trim($_POST["email"]); $_SESSION['name'] = $name; $_SESSION['phone'] = $phone; $_SESSION['email'] = $email; } ?> <body> <h2>Please enter your contact information below:</h2> <div> <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ; ?>"> <p> <strong>Name:</strong> <span><input type="text" name="name" size="25" maxlength="25" value="<?php echo $_SESSION['name']; ?>" /></span> </p> <p> <strong>Phone:</strong> <span><input type="text" name="phone" size="12" maxlength="12" value="<?php echo $_SESSION['phone']; ?>" /></span> </p> <p> <strong>Email:</strong> <span><input type="text" name="email" size="25" maxlength="25" value="<?php echo $_SESSION['email']; ?>" /></span> </p> <p><span> <input type="submit" value="Next" /> </span><br /></p> </form> </div> <?php // final version will only display errors generated by isvalid() // success here should send me to the next page instead if(isset($_POST['name']) || isset($_POST['phone']) || isset($_POST['email'])) if(isvalid($name, $phone, $email)){ echo "<p>Success!</p>"; foreach($_SESSION as $key => $value) echo "<p>Key: $key; Value: $value</p>"; } function isvalid($name, $phone, $email){ $flag = TRUE; if(empty($name)){ echo "<p>Name cannot be empty!</p>"; $flag = FALSE; } if(!preg_match('/^[2-9][0-8][0-9]-[2-9][0-9]{2}-[0-9]{4}$/', $phone)){ echo "<p>Phone number is invalid! Please use 'xxx-xxx-xxxx' format.</p>"; $flag = FALSE; } if(!preg_match('/^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/', $email)){ echo "<p>Email is invalid!</p>"; $flag = FALSE; } return $flag; } ?> </body> </html> Also, my stylesheet in case you need it too: p {font-family: "Times New Roman"; color: blue} h1 {font-family: "Times New Roman"; color: red} h2 {font-family: "Times New Roman"; color: purple} h3 {font-family: "Times New Roman"; color: green} img {margin: 10px} hr {color: red} td {color: blue} table, th, td {border: 1px solid black; width: 1000px; color: #006633} span {float: right} div {width: 400px} Quote Link to comment Share on other sites More sharing options...
sirjames2004 Posted May 1, 2013 Author Share Posted May 1, 2013 I fugred everything out. You can close this topic. Quote Link to comment 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.