Archive

Archive for the ‘php’ Category

$_SESSION() Checkbox doesn’t hold state

September 9th, 2005 ^Lestat No comments

I’ve been working on getting forms to hold their data when the back button is pushed. It’s been slow going. I thought I had it down (This entry). But when it came to checkboxes, and selects, I was in for a surprise. Most the code is pretty fundamental.
I was able to get selects to work, by making them dynamic and conditionally adding the “selected” attribute to the selected item.

Regarding what I’ve learned so far, I realize that a checkbox only sends a value if it is checked.

If it’s not checked, it doesn’t send any value at all. So I tried to create a variable based upon the condition of when it was checked. In this case I called it $flag.

Then made a session for that varaiable thinking that it would carry back over if the back button was pushed. However the session variable for it, $_SESSION['flag'] doesn’t carry back over. The box will not remember to be checked.

form.php:

< ?php
// [ START SESSION ]
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>

<html>
<body>

<form method="post" action="preview.php">

Choose #1: <input type="checkbox" name="box1"/><br />
<input type="submit" value="submit"/>

</form>

</body>
</html>

preview.php:

< ?php
// [ START SESSION ]
session_start();
header("Cache-control: private"); // IE 6 Fix.

// actions to take from buttons
if (isset($_POST["goBack"])) { // Go Back and Edit
	header("Location: form.php");
} else {
	if (isset($_POST["Send"])) { // Process the form
		header("Location: process.php");
	}
}

// Dynamically Assign Key & Value to each Post - when back button is pushed.
     It retains the form data for textboxes only it seems.
foreach($_POST as $key=>$value){
	$_SESSION[$key]=$value;
	print("$key = $value<br />");
}
?>

<html>
<body>

< ?php
if(isset($_POST['box1'])){
	$flag = 'checked';
	$_SESSION['flag'] = $flag;
	print("<font color=\"red\"><b>box is checked. flag = $_SESSION[flag]</b><br />");
} else {
	$flag = 'NOT Checked';
	$_SESSION['flag'] = $flag;
	print("<font color=\"red\"><b>box is NOT checked. flag = $_SESSION[flag]</b></font><br />");
}
?>

<form method="post" action="preview.php">

<input type="submit" name="goBack" value="Edit"/><input type="submit" name="Send" value="OK"/>

</form>

</body>
</html>

I’m still looking for a solution for this. Now that I think about it, I’m going to imagine that radio’s will have a similar issue. I will post the solution – if I find a workaround.

, , , , php

Categories: Computing, php Tags:

Retaining Form Data

August 26th, 2005 ^Lestat 2 comments

I was looking to find a way in php to retain form data. After poking around the net through various tutorials, I found a way. I can script some basic things, but am still quite a novice at php.

So here’s what i found I could do, with the help of a blog entry at scriptygoddess.

For this, I will use a $_SESSION() to help carry the data throughout the pages. This requires that the following code be placed at the top of all the pages involved:

< ?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>

Now we need the form page(with the above code in it too):

<form method="post" action="preview.php">
First Name:<input type="text" name="First_Name" value="<? print("$_SESSION[First_Name]");?/>" /><br />
Last Name:<input type="text" name="Last_Name" value="<? print("$_SESSION[Last_Name]");?/>" /><br />
<input type="submit" value="submit"/>
</form>

When submit is pushed, you can see the action takes us to a preview page(preview.php). This page will display what youve entered in the form, and give you the option to go back and edit your data, or agree, and continue processing the form.

< ?php
//preview.php

// Start session
session_start();
header("Cache-control: private"); // IE 6 Fix.

// actions to take from buttons
if (isset($_POST["goBack"])) { // Go Back and Edit
    header("Location: form.php");
} else {
    if (isset($_POST["Send"])) { // OR process the form
        header("Location: process.php");
    }
}

// Dynamically Assign Key & Value to each Post
foreach($_POST as $key=>$value){
    $_SESSION[$key]=$value;
}
?>

The key element to holding the data to fill back in the form with the data you entered lies in this bit of code:

// Dynamically Assign Key & Value to each Post
foreach($_POST as $key=>$value){
    $_SESSION[$key]=$value;
}

What this does is take all the data you entered “$_POST” and turn it into an array. It first assigns it a $key and also holds the value of it i.e. =>$First_Name. So in this example the $_POST array looks like:
array([0]=>what_you_entered_for_the_first_name[1]=>what_you_entered_for_the_last_name)

So when you hit ‘edit’ this will cause:

First Name:<input type="text" name="First_Name" value="<? print("$_SESSION[First_Name]");?/>" />

to display what was entered originally.

Categories: php Tags: