Jump to content

PHP Session Control


sirjames2004

Recommended Posts

 

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