Archive

Archive for the ‘php’ Category

php: Dynamically fill a select box

December 30th, 2005 ^Lestat 4 comments

One way to fill a select box with array elements.

<html>
<head>
<title>Select Box</title>
</head>
<body>

< ?php
$myArray = array('cat' , 'dog' , 'mouse' , 'cow');
?>

<select name="myselect">
<option value=""></option> // Sets blank entry *optional*
< ?php
foreach($myArray as $value){ // Loop through each element
  print("<option value=\"$value\">$value<option>");
}
?>
</option></select>

</body>
</html>

php, select, drop down

Categories: Programming, php Tags:

php: Save browsed page as $var and send using phpMailer

December 28th, 2005 ^Lestat 5 comments

You can store an entire page as if it were opened in a browser, including all dynamic
data. Here’s how…

(Please use caution copying/pasting. Quotes can be a problem with the current plugin)
For this example we are going to build a form and the results using phpMailer in an html format:

form.php
results.php
preview.php
sendmail.php

form.php:

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

<html>
<head>
<title>Form</title>
</head>
<body>

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

</body>
</html>

preview.php:

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

<html>
<head>
<title>Your Results</title>
</head>
<body>

< ?php
include("results.php");
?>

</body>
</html>

results.php:

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

<html>
<head>
<title></title>
<style type="text/css">
.firstname{
  font: 12px verdana green;
}

.lastname{
  font: 14px arial red;
}
</style>
</head>
<body>

< ?php print("<p class=\"firstname\">Hello $fName"); ?>
< ?php print("<p class=\"lastname\">Your last name is $fName"); ?>

</body>
</html>

And now the sendmail.php and gathering the output in a $var:

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

// call the mail class
require("class.phpmailer.php");
$mail = new PHPMailer();

// Set mail headers & connection
$mail->IsSMTP(); // tell phpMailer were using smtp
$mail->Host = "smtp.server.com"; // SMTP server
$mail->SMTPAuth =true; // turn on smtp auth
$mail->Username = "mail@domain.com"; //smtp username
$mail->Password = "password"; //smtp password
$mail->From = "user@domain.com";
$mail->FromName = "$fName";
$mail->AddAddress("fromAddress@domain.com"); // From address

$mail->Subject = "Form Results";
$mail->IsHTML(true); // tell phpMailer we're sending html email

// *** Grab Output Of HTML File as if it were opened in browser ***
ob_start();
include("results.php");
$htmlbody = ob_get_contents(); // adding output to the variable
ob_end_clean();

// *** Set the htmlbody variable in the email ***
$mail->Body = "$htmlbody";

// return results
if(!$mail->Send()){
	print("Mail was NOT sent<br />");
	print("Mailer Error: " . $mail->ErrorInfo );
} else {
	print("Thank you. Your mail was sent.");
?>

The following code is what grabs the output as if it were opened in a browser. The mail form was just an example. I’m sure there are other uses for this.

// *** Grab Output Of HTML File as if it were opened in browser ***
ob_start();
include("results.php");
$htmlbody = ob_get_contents(); // adding output to the variable
ob_end_clean();

Comments? Suggestions? See errors that should be changed? Feel free to comment.

phpMailer, php html email, php email, php get contents, php form data

Categories: Computing, Programming, php Tags:

$_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 3 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: