Jump to content

Displaying posted variables help!


twilitegxa

Recommended Posts

I need some help displaying the posted values onto a second page. I thought I was doing it right, but nothing is showing up on my second page. I have my form set to PHP_SELF to validate before sending the user to the second page, and it's working just fine. If no errors are present, the user is redirected to the second page, but it's supposed to be displaying their posted data, but it's not. What am I doing wrong?

 

<?php

/*$name = "";
$address = "";
$city = "";
$state = "";
$zip = "";
$phone = "";
$position = "";
$display_errors = "";*/ 

if (@$_POST['submitted']){
    $name = (@$_POST['name']);
    $address = (@$_POST['address']);
    $city = (@$_POST['city']);
    $state = (@$_POST['state']);
    $zip = (@$_POST['zip']);
    $phone = (@$_POST['phone']);
    $position = (@$_POST['position']);
    
    if (get_magic_quotes_gpc()){
        $name = stripslashes($name);
        $address = stripslashes($address);
        $city = stripslashes($city);
        $state = stripslashes($state);
        $zip = stripslashes($zip);
        $phone = stripslashes($phone);
        $position = stripslashes($position);
        
    }
    
    $error_msg=array();
    if ($name==""){
        $error_msg[]="Please enter your name";
    }
    
    if ($address==""){
        $error_msg[]="Please enter your address";
    }
    
    if ($city==""){
        $error_msg[]="Please enter your city";
    }
    
    if ($state==""){
        $error_msg[]="Please enter your state";
    }
    
    if ($zip==""){
        $error_msg[]="Please enter your zip code";
    }
    
    if ($phone==""){
        $error_msg[]="Please enter your phone number";
    }
    
    if ($position==""){
        $error_msg[]="Please enter your desired position";
    }
    
    if ($error_msg){
        $display_errors =  "<ul style=color:red>\n";
            foreach ($error_msg as $err){
                $display_errors .= "<li>".$err."</li>\n";
            }
        $display_errors .= "</ul>\n";
        
    }
    
    if (!$error_msg){
        //echo $name . "<br>";
        //echo $address . "<br>";
        //echo $city . "<br>";
        //echo $state . "<br>";
        header('Location: process_U2IP.php');
        exit();
    }
    
    }
    
?>
<!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=utf-8" />
<title>Forms1.php</title>
</head>
<body>
<div id="content">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<h3>Please fill out the form below and upload your resume to apply for a position within our company:</h3>

<label for="name">Name*</label><br />
<input name="name" type="text" size="20" id="name" value="<?php echo $name; ?>" /><br />

<label for="address">Address*</label><br />
<input name="address" type="text" size="20" id="address" value="<?php echo $address; ?>" /><br />

<label for="city">City*</label><br />
<input name="city" type="text" size="20" id="city" value="<?php echo $city; ?>" /><br />

<label for="state">State*</label><br />
<input name="state" type="text" size="20" id="state" value="<?php echo $state; ?>" /><br />

<label for="zip">Zip*</label><br />
<input name="zip" type="text" size="20" id="zip" value="<?php echo $zip; ?>" /><br />

<label for="phone">Phone*</label><br />
<input name="phone" type="text" size="20" id="phone" value="<?php echo $phone; ?>" /><br />

<label for="position">Position*</label><br />
<input name="position" type="text" size="20" id="position" value="<?php echo $position; ?>" /><br />

<p><i>Your information will not be sold or shared with others.</i></p>

<p style="color: red;">* denotes required field</p>

<input type="hidden" name="submitted" value="1" />

<input type="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form>

<?php echo $display_errors; ?>
</div>
</body>
</html>

 

<?php

$name = (@$_POST['name']);
$address = (@$_POST['address']);
$city = (@$_POST['city']);
$state = (@$_POST['state']);
$zip = (@$_POST['zip']);
$phone = (@$_POST['phone']);
$position = (@$_POST['position']);

$display_block = "
<table>
<tr>
<td><b>Name:</b></td>
<td>$name</td>
</tr>
<tr>
<td><b>Address:</b></td>
<td>$address</td>
</tr>
<tr>
<td><b>City:</b></td>
<td>$city</td>
</tr>
<tr>
<td><b>State:</b></td>
</tr>
</table>";

echo $display_block;
  
?> 

 

It's just displaying the table but with no values or blank values for the posted variables. Can anyone help?

Link to comment
Share on other sites

since you are moving variables from one page to another, the only way is to do it using POST, GET or storing in a session, or storing in a cookie and then taking it to next page.

 

in this case i would prefer a session storage and call it in the second page, once it has been validated by the first page.after validation, assign the validated variable to a session like:

$_SESSION['name']=$name

 

you must decide which of the 4 methods you want, POST may not really work, coz you are not submitting or clicking a form button, GET is unsafe, and if you have long stuff, may be unwieldy, cookies-if disabled by user you need to break your head, session is the easiest option in my opinion.

Link to comment
Share on other sites

Well, I added the session to both pages and named the session variables like you suggested, and it works! The only problem I'm getting is when I type something into my first field, name, I was trying to write a validation  that only allowed letters and spaces, and when I typed in some other characters, like $%^, it seemed to work for those, saying the error message whenever one was typed, but if I refresh the page, and/or use my reset button, they sometimes are still slipping through to the next page. Any suggestions? Here is what I have currently:

 

<?php

session_start();

$name = "";
$address = "";
$city = "";
$state = "";
$zip = "";
$phone = "";
$position = "";
$display_errors = "";

if (@$_POST['submitted']){
    $name = (@$_POST['name']);
    $address = (@$_POST['address']);
    $city = (@$_POST['city']);
    $state = (@$_POST['state']);
    $zip = (@$_POST['zip']);
    $phone = (@$_POST['phone']);
    $position = (@$_POST['position']);
    
    if (get_magic_quotes_gpc()){
        $name = stripslashes($name);
        $address = stripslashes($address);
        $city = stripslashes($city);
        $state = stripslashes($state);
        $zip = stripslashes($zip);
        $phone = stripslashes($phone);
        $position = stripslashes($position);
        
    }
    
    $error_msg=array();
    if ($name==""){
        $error_msg[]="Please enter your name";
    }
    
    if(!preg_match('/^[a-z ]*$/i', $name)){
        $error_msg[]="Name can only contain letters and spaces";
    }
    
    if ($address==""){
        $error_msg[]="Please enter your address";
    }
    
    if(!preg_match('/^[a-z0-9 ]*$/i', $address)){
        $error_msg[]="Address can only contain numbers, letters and spaces";
    }
    
    
    if ($city==""){
        $error_msg[]="Please enter your city";
    }
    
    if (!preg_match("/^\b[a-zA-Z]+\b$/", $city)){
        $error_msg[]="City can only contain letters";
    }
    
    if ($state==""){
        $error_msg[]="Please enter your state";
    }
    
    if (strlen($state)<>2){
        $error_msg[]="State can only contain 2 letters; use state abbreviation";
    }
    
    if (!preg_match("/^\b[a-zA-Z]+\b$/", $state)){
        $error_msg[]="State can only contain letters";
    }
    
    if ($zip==""){
        $error_msg[]="Please enter your zip code";
    }
    
    if (strlen($zip)<>5){
        $error_msg[]="Zip code can only contain 5 digits";
    }
    
    if(!is_numeric($zip)){
        $error_msg[]="Zip code must contain only numbers";
    }
    
    if ($phone==""){
        $error_msg[]="Please enter your phone number";
    }
    
    if (strlen($phone)<>10){
        $error_msg[]="Phone number can only contain 10 digits";
    }
    
    if(!is_numeric($phone)){
        $error_msg[]="Phone number must contain only numbers";
    }
    
    if ($position==""){
        $error_msg[]="Please enter your desired position";
    }
    
    if(!preg_match('/^[a-z0-9 ]*$/i', $position)){
        $error_msg[]="Position can only contain numbers, letters and spaces";
    }
    
    if ($error_msg){
        $display_errors =  "<ul style=color:red>\n";
            foreach ($error_msg as $err){
                $display_errors .= "<li>".$err."</li>\n";
            }
        $display_errors .= "</ul>\n";
        
    }
    
    if (!$error_msg){
        //echo $name . "<br>";
        //echo $address . "<br>";
        //echo $city . "<br>";
        //echo $state . "<br>";
        header('Location: process_U2IP.php');
        exit();
    }
    
    }
    
?>
<!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=utf-8" />
<title>KulaE_WDP4451_U2IP</title>
</head>
<body>
<div id="content">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<h3>Please fill out the form below and upload your resume to apply for a position within our company:</h3>

<table>
<tr>
<td><label for="name"><b>Name*</b></label></td>
<td><input name="name" type="text" size="20" id="name" value="<?php echo $name; ?>" /></td>
</tr>
<tr>
<td><label for="address"><b>Address*</b></label></td>
<td><input name="address" type="text" size="20" id="address" value="<?php echo $address; ?>" /></td>
</tr>
<tr>
<td><label for="city"><b>City*</b></label></td>
<td><input name="city" type="text" size="20" id="city" value="<?php echo $city; ?>" /></td>
</tr>
<tr>
<td><label for="state"><b>State*</b></label></td>
<td><input name="state" type="text" size="20" id="state" value="<?php echo $state; ?>" /></td>
</tr>
<tr>
<td><label for="zip"><b>Zip*</b></label></td>
<td><input name="zip" type="text" size="20" id="zip" value="<?php echo $zip; ?>" /></td>
</tr>
<tr>
<td><label for="phone"><b>Phone*</b></label></td>
<td><input name="phone" type="text" size="20" id="phone" value="<?php echo $phone; ?>" /></td>
</tr>
<tr>
<td><label for="position"><b>Position*</b></label></td>
<td><input name="position" type="text" size="20" id="position" value="<?php echo $position; ?>" /></td>
</tr>
<tr>
<td><b>Upload Resume:</b></td>
<td><input type="file" name="file" id="file" /> </td>
</tr>
<tr>
<td colspan="2"><p><i>Your information will not be sold or shared with others.</i></p></td>
</tr>
<tr>
<td colspan="2"><p style="color: red;">* denotes required field</p></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="hidden" name="submitted" value="1" />

<input type="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>

<?php echo $display_errors; ?>
</div>
</body>
</html>

 

<?php

session_start();

$name = $_SESSION['name'];
$address = $_SESSION['address'];
$city = $_SESSION['city'];
$state = $_SESSION['state'];
$zip = $_SESSION['zip'];
$phone = $_SESSION['phone'];
$position = $_SESSION['position'];

$display_block = "
<table>
<tr>
<td><b>Name:</b></td>
<td>$name</td>
</tr>
<tr>
<td><b>Address:</b></td>
<td>$address</td>
</tr>
<tr>
<td><b>City:</b></td>
<td>$city</td>
</tr>
<tr>
<td><b>State:</b></td>
<td>$state</td>
</tr>
<tr>
<td><b>Zip Code:</b></td>
<td>$zip</td>
</tr>
<tr>
<td><b>Phone Number:</b></td>
<td>$phone</td>
</tr>
<tr>
<td><b>Position Desired:</b></td>
<td>$position</td>
</tr>
</table>";

echo $display_block;
  
?> 

 

Link to comment
Share on other sites

1.well, to rigidly control the inputs, you must use regular expressions to validate, which u already seem to be using..so check for the exact thing u need,,,...i use expresso to validate and play on regex..

 

2.once it validates, only then, assign it to the session to take it forward..else mk that session variable=""

and remember to unset the session variable when u begin a new iteration....because sessions are fluid, in a sense that the value wont change until u reset or re-assign or close browser...

let me know...

Link to comment
Share on other sites

Hi all,

 

Although this is solved I cannot help but wonder why your using the error suppressor (@) on your $_POST array, when surely you WANT to know of any errors coming through from $_POST data?? Admittedly there are some functions that php people recommend that you do that to (I think they are come XML functions, but that's off the top of my head) But other wise, isn't it better to have a script that isn't erroneous.. JMO 

 

Either that or I have totally missed the point...

 

Cheers,

Rw

Link to comment
Share on other sites

I wasn't sure why my code was using the @ symbol. I was using part of this beginning code from a tutorial in a book, so I didn't know what it was being used for exactly. Originally, this code was validating on the same page and using the PHP_SELF as the action, but then I was having some trouble, so i changed it to validate on the second page. I was having trouble validating the file upload on the same page and that's why I had to change it, plus I was having issues with my data not resetting properly and other things that I mentioned earlier. Could you explain that @ symbol thing some and tell me when it's supposed to be used, how, and why?

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.