PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ethoemmes
Forum Commoner
Posts: 26 Joined: Thu Aug 18, 2005 4:11 pm
Post
by ethoemmes » Wed Sep 14, 2005 12:44 pm
Hi
I am trying to prefill a form with a session variable if it is set. Basically if the variable exists I want the text box to be pre filled otherwise I want the text box to be blank.
Cant work out the syntax. Can anyone help?
Code: Select all
<INPUT TYPE=TEXT NAME="FirstName" SIZE=21 <PHP? if (isset($_SESSION['FirstName'])) {?????} ?> >
Thx
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Wed Sep 14, 2005 12:48 pm
Code: Select all
<INPUT TYPE=TEXT NAME="FirstName" SIZE=21 value="<?php echo isset($_SESSION['FirstName'])?$_SESSION['FirstName']:'';?>">
EDIT: Wrong function called (updated code)
Last edited by
hawleyjr on Wed Sep 14, 2005 1:29 pm, edited 1 time in total.
ethoemmes
Forum Commoner
Posts: 26 Joined: Thu Aug 18, 2005 4:11 pm
Post
by ethoemmes » Wed Sep 14, 2005 1:12 pm
Hi
I have tried your code but it is not transferring the variables. I have posted my full code below as well as the script where I set the session variables. Can you spot where I have gone wrong?
Code: Select all
<?php
session_start();
header("Cache-control: private");
include "config.php";
$table = "tblCountry";
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// read data from database
$result = mysql_query("SELECT * FROM $table")
or die ("Could not read data because ".mysql_error());
?>
<HTML>
<HEAD>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<meta http-equiv="Content-Language" content="en-us">
<TITLE>Registration Form</TITLE>
<link href="../stylesheets/styles.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<table width="90%" cellspacing="0" align="center" cellpadding="0" class="body">
<tr>
<td width ="20%" align="left"> <img src="../banner.gif" title="Rudi Thoemmes Rare Books" /> </td>
<td align="center" valign="middle"><span class="title">Registration Form </span></td>
</tr>
<tr>
<td align="left" valign="top"><table width="100%" border="0" cellpadding="5" cellspacing="0" class="current">
<tr>
<td><a href="index.html" class="links">Index</a></td>
</tr>
<tr>
<td>Current Catalogue</td>
</tr>
<tr>
<td>Mailing List </td>
</tr>
<tr>
<td>Company Outline </td>
</tr>
</table></td>
<td align="left">
<div align="center">
<center>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td align="center"><FORM METHOD="POST" ACTION="form.php">
<p>Please provide the following contact information:</p>
<TABLE width="505">
<TR>
<TD ALIGN="left" width="103"> <font size="2">Title</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<select name="Title" id="select">
<option value="Mr" selected>Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Doctor">Doctor</option>
<option value="Professor">Professor</option>
</select></TD>
<TD width="136" rowspan="14" class="boxborder">
<p class="small" align="center"><span lang="en-gb">To be added to
the mailing list s</span>imply fill in your details<span lang="en-gb">,
</span>check they are correct and click Submit<span lang="en-gb">.</span></p>
<p class="body" align="center"><span lang="en-gb">A field marked
with an asterisks must be completed. </span></p>
<p class="body" align="center"><span lang="en-gb">If you have any
comments please use this link</span> </p>
<p> </TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">First Name</font><span lang="en-gb"><font size="2">
* </font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="FirstName" SIZE=21 value="<?php echo is_set($_SESSION['FirstName'])?$_SESSION['FirstName']:'';?>">
</TD>
</TR>
<tr>
<TD ALIGN="left" width="103"> <font size="2">Middle Name</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="MiddleName" SIZE=21>
</TD>
</tr>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Last Name</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="LastName" SIZE=21 value="<?php echo is_set($_SESSION['LastName'])?$_SESSION['LastName']:'';?>">
</TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">E-mail</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Email" SIZE=25 value="<?php echo is_set($_SESSION['Email'])?$_SESSION['Email']:'';?>">
</TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> </TD>
<TD width="252">
</TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Organization</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Organization" SIZE=25></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Address</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Address1" SIZE=25></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Address</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Address2" SIZE=25></TD>
</TR>
<tr>
<TD ALIGN="left" width="103"> <font size="2">City</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="City" SIZE=23></TD>
</tr>
<tr>
<TD ALIGN="left" width="103"> <font size="2">State/County</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="State" SIZE=23></TD>
</tr>
<TR>
<TD ALIGN="left" width="103"> <span lang="en-gb"><font size="2">Zip</font></span><font size="2"> Code</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="PostCode" SIZE=12></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Country</font></TD>
<TD width="252">
<select name="Country">
<?php
while ($row = mysql_fetch_array($result)){
$CountryID = $row['CountryID'];
$Country = $row['Country'];
echo "<option value=\"$CountryID\">$Country</option>";
}
?>
<option value=""></option>
</select></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Telephone</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Phone" SIZE=25 MAXLENGTH=25>
</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<p>
<INPUT name="SUBMIT" TYPE=SUBMIT VALUE="Submit Form">
<INPUT name="RESET" TYPE=RESET VALUE="Reset Form">
</p>
</FORM></td>
</tr>
</table></center>
</div>
</td>
</tr>
</table>
</BODY>
</HTML>
Script where i set the variables.
Code: Select all
<?php
session_start();
header("Cache-control: private");
$Title = $_POST['Title'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$Email = $_POST['Email'];
$Comments = $_POST['Comments'];
$AddToML = $_POST['AddToML'];
$_SESSION['Title'] = $Title;
$_SESSION['FirstName'] = $FirstName;
$_SESSION['LastName'] = $LastName;
$_SESSION['Email'] = $Email;
$strMailTo = 'Edgar@rrbltd.com';
$strSubject = 'Enquiry from Website';
$strBody = "$Comments \n
Received from \n
$Title $FirstName $LastName \n
$Email
$AddToML";
if (empty($FirstName) || empty($LastName) || empty($Email)) {
header( "Location: http://www.rrbltd.com/Development/form_error.php" );
}
else {
$From = "From: \"$FirstName $LastName\" <$Email>\nX-Mailer: PHP/" . phpversion();
mail ($strMailTo, $strSubject, $strBody, $From);
if (isset($_POST['AddToML'])) {
header( "Location: http://www.rrbltd.com/Development/Regis ... rm_v1a.htm" );
}
else {
header( "Location: http://www.rrbltd.com");
}
}
?>
TIA
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Wed Sep 14, 2005 1:19 pm
Use the following to test your $_POST and $_SESSION variables:
Code: Select all
echo '<HR><PRE>'; print_r($_SESSION ); echo '</PRE>';
echo '<HR><PRE>'; print_r($_POST ); echo '</PRE>';
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Wed Sep 14, 2005 1:22 pm
The proper name of that function is
isset . (Other useful functions are
empty and
array_key_exists )
An example script could look like:
Code: Select all
$fields = array('firstname', 'lastname', 'street');
foreach($fields as $field)
{
$value = isset($_SESSION[$field]) ? htmlentities($_SESSION[$field]) : '';
echo "<input type='text' name='$field' value='$value'/>";
}
ethoemmes
Forum Commoner
Posts: 26 Joined: Thu Aug 18, 2005 4:11 pm
Post
by ethoemmes » Wed Sep 14, 2005 1:25 pm
Hi
I posted your code onto a new script and called it from my original script but no variables where printed. Any idea why the session variables are not being transferred?
TIA
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Wed Sep 14, 2005 1:28 pm
timvw wrote: The proper name of that function is
isset
OMG, how did I screw that one up....its just been one of those days....
ryanlwh
Forum Commoner
Posts: 84 Joined: Wed Sep 14, 2005 1:29 pm
Post
by ryanlwh » Wed Sep 14, 2005 1:33 pm
ethoemmes wrote: Hi
I posted your code onto a new script and called it from my original script but no variables where printed. Any idea why the session variables are not being transferred?
TIA
Do you have session_start() on the new script? A page won't pick up session variables unless you call session_start()
ethoemmes
Forum Commoner
Posts: 26 Joined: Thu Aug 18, 2005 4:11 pm
Post
by ethoemmes » Wed Sep 14, 2005 1:34 pm
Hi
Thanks for all your help guys. Unfortunately its still not working...aahhhhhh
My updated code
Code: Select all
<?php
session_start();
header("Cache-control: private");
include "config.php";
$table = "tblCountry";
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// read data from database
$result = mysql_query("SELECT * FROM $table")
or die ("Could not read data because ".mysql_error());
$Title = $_SESSION['Title'];
$FirstName = $_SESSION['FirstName'];
$LastName = $_SESSION['LastName'];
$Email = $_SESSION['Email'];
?>
<HTML>
<HEAD>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<meta http-equiv="Content-Language" content="en-us">
<TITLE>Registration Form</TITLE>
<link href="../stylesheets/styles.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<table width="90%" cellspacing="0" align="center" cellpadding="0" class="body">
<tr>
<td width ="20%" align="left"> <img src="../banner.gif" title="Rudi Thoemmes Rare Books" /> </td>
<td align="center" valign="middle"><span class="title">Registration Form </span></td>
</tr>
<tr>
<td align="left" valign="top"><table width="100%" border="0" cellpadding="5" cellspacing="0" class="current">
<tr>
<td><a href="index.html" class="links">Index</a></td>
</tr>
<tr>
<td>Current Catalogue</td>
</tr>
<tr>
<td>Mailing List </td>
</tr>
<tr>
<td>Company Outline </td>
</tr>
</table></td>
<td align="left">
<div align="center">
<center>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td align="center"><FORM METHOD="POST" ACTION="form.php">
<p>Please provide the following contact information:</p>
<TABLE width="505">
<TR>
<TD ALIGN="left" width="103"> <font size="2">Title</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<select name="Title" id="select">
<option value="Mr" selected>Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Doctor">Doctor</option>
<option value="Professor">Professor</option>
</select></TD>
<TD width="136" rowspan="14" class="boxborder">
<p class="small" align="center"><span lang="en-gb">To be added to
the mailing list s</span>imply fill in your details<span lang="en-gb">,
</span>check they are correct and click Submit<span lang="en-gb">.</span></p>
<p class="body" align="center"><span lang="en-gb">A field marked
with an asterisks must be completed. </span></p>
<p class="body" align="center"><span lang="en-gb">If you have any
comments please use this link</span> </p>
<p> </TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">First Name</font><span lang="en-gb"><font size="2">
* </font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="FirstName" SIZE=21 value="<?php echo isset($FirstName)?$FirstName:'';?>">
</TD>
</TR>
<tr>
<TD ALIGN="left" width="103"> <font size="2">Middle Name</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="MiddleName" SIZE=21>
</TD>
</tr>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Last Name</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="LastName" SIZE=21 value="<?php echo isset($LastName)?$LastName]:'';?>">
</TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">E-mail</font><span lang="en-gb"><font size="2">
*</font></span></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Email" SIZE=25 value="<?php echo isset($Email)?$Email:'';?>">
</TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> </TD>
<TD width="252">
</TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Organization</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Organization" SIZE=25></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Address</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Address1" SIZE=25></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Address</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Address2" SIZE=25></TD>
</TR>
<tr>
<TD ALIGN="left" width="103"> <font size="2">City</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="City" SIZE=23></TD>
</tr>
<tr>
<TD ALIGN="left" width="103"> <font size="2">State/County</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="State" SIZE=23></TD>
</tr>
<TR>
<TD ALIGN="left" width="103"> <span lang="en-gb"><font size="2">Zip</font></span><font size="2"> Code</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="PostCode" SIZE=12></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Country</font></TD>
<TD width="252">
<select name="Country">
<?php
while ($row = mysql_fetch_array($result)){
$CountryID = $row['CountryID'];
$Country = $row['Country'];
echo "<option value=\"$CountryID\">$Country</option>";
}
?>
<option value=""></option>
</select></TD>
</TR>
<TR>
<TD ALIGN="left" width="103"> <font size="2">Telephone</font></TD>
<TD width="252">
<INPUT TYPE=TEXT NAME="Phone" SIZE=25 MAXLENGTH=25>
</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<p>
<INPUT name="SUBMIT" TYPE=SUBMIT VALUE="Submit Form">
<INPUT name="RESET" TYPE=RESET VALUE="Reset Form">
</p>
</FORM></td>
</tr>
</table></center>
</div>
</td>
</tr>
</table>
</BODY>
</HTML>
Sorry to keep bugging everyone but as you can probably guess i'm pretty clueless when it comes to PHP.
Thanks for all your help.
Edgar
ryanlwh
Forum Commoner
Posts: 84 Joined: Wed Sep 14, 2005 1:29 pm
Post
by ryanlwh » Wed Sep 14, 2005 3:19 pm
what error(s) did you get?
i noticed this: isset($Lastname) ? $Lastname]
the extra ] after Lastname.
I don't think this is the problem but think that I should let you know.
raghavan20
DevNet Resident
Posts: 1451 Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:
Post
by raghavan20 » Wed Sep 14, 2005 3:55 pm
Code: Select all
$Title = $_SESSION['Title'];
$FirstName = $_SESSION['FirstName'];
$LastName = $_SESSION['LastName'];
$Email = $_SESSION['Email'];
echo all these values, $title, $fistname, $lastname, $email just after your assignment
also its better if you have this on top of each page when developing the application not at production