Archive

Archive for the ‘Programming’ Category

php: Dynamically fill a select box

December 30th, 2005 ^Lestat 6 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:

VS 2005 Launch Tour Green Bay

December 2nd, 2005 ^Lestat No comments

On November 17th I attended the Microsoft SQL server 2005 and Visual Studio 2005 Launch event in Green Bay WI.

It was a full day event. There were in my estimation about 200-250 people there.

There were the usual handful of vendors peddling their products and services, as well as their drawings (of which I always look forward to some good prizes!).

I haven’t done any programming in asp or vb. So I was interested what it could do for me.

The hotel casino had its spread of coffee, pastries, soda, juice and water for the taking. Of which I had none. The last thing I wanted to do was fall asleep.

The first session (morning – lunch) was about SQL server. Most of which I know nothing about. I would agree with my friend TDavid that it seemed geared toward large companies who have huge databases and infrastructure. The speaker, Kevin, did a pretty good job. He has a blog at http://blogs.technet.com/kevinremde

I was impressed with the reporting tools. What I did not know was that for much SQL database management that the database actually gets taken offline. With the 05 version, you can do what you need to do and it remains online(though not accessable). Including making upgrades to the server software. When you bring it back online, it will catch itself up and resynch to any other servers you have setup be it local, or remote. It was also nice to hear that XML is a 1st class data type in SQL.

When the morning session was finished, we were able to get the carrot dangled before us…. a brand new copy of SQL server 2005 and Visual Studio 2005 with 1 license key. Cool!…

I had chatted a bit with a guy I met in the morning session. I found he doesn’t live too far from me. Someday I’d like to get together with him again so we could talk shop. He was more familiar with asp. The 2 of us headed over to the casino buffet. $8 for lunch. I was being careful not to overeat. Ever eat a whole bunch of food, and try to sit for 3.5 hours listening to someone talk? ZZZzz…

Ok the second session was on the Visual Studio 2005. Before the session began we watched a video promoting thecoderoom.com, an ‘Experimental’ site where they throw some programmers in a room and assign them a project. You can watch it online, as they plan using the tools in the seminar.

Finally, some programming! His name is Jacob Cynamon. He started the session off by explaining how to get the IE7 Beta version off of one of our promo disks.


Why? Well apparently the team that put his disk together, forgot to tell him that it included a certificate that expired in October 05. So he showed us to changed the system date on our pc(the clock in the taskbar) back a few months so the program would install.

What I do like about the studio suite is the drag and drop object orientation. Really cool how you can change the properties, and have objects like login and registration forms drag/droppable. Ok cool, a wysiwyg! Seriously bloated wysiwyg, to me anyhow.

I was trying to be objectionable, and still havn’t had the time to give it a fair shake yet. At one point Jacob brought up the point that visual studio has a new built in feature called a config page. In this page you can set a lot of the applications variables and procedures. Now in my mind, I was thinking “ok, a config page? this is exciting?”. From that point on, every thing I was being shown could be done in php as well. I’m not knocking it, but the only thing I found to be real positive was the drag and drop, not too much need for design, objects. You can make fairly easy template and template systems.

I must say my interest was captivated for the afternoon. After the session, there were the prize drawings, of which I didn’t win anything. Oh well. Of course the true benifit, was that after the 2nd session, we got to get in line for our free SQL/Studio 05 software AGAIN!

What I can say, is thank you for the free software. Now I can try to play and experiment with a new language.

SQL Server 2005, Visual Studio 2005,

Categories: Computing, Internet, Programming Tags:

Get code snippets from published books

September 21st, 2005 ^Lestat No comments

I rencently found a neat site that searches for code snippets published in books. codefetch { lists 19 different programming languages to choose from.

The first querry I ran for a php script was fast, and returned many results. It’s a little confusing when you first arrive at the page. There are ads and links at the top of it.

When you scroll down, you will find snippets from many books. Along with the published snippets, there are also links to the languages main help pages regarding the querry you ran. I think it’s great!

programming, code, snippets

Categories: Computing, Programming 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: