PDA

View Full Version : About Form on Dreamweaver


neuro
03-09-2007, 06:46 AM
Hi..

I created a simple example of Index.htm and Form.htm like the one bellow:

Index.htm
http://img243.imageshack.us/img243/3931/indexal9.jpg (http://imageshack.us)

Form.htm
http://img442.imageshack.us/img442/9056/formxw9.jpg (http://imageshack.us)
when user click The "submision form", it will open Form.htm in a pop up window.

Now after user fill in the text field and click submit button, i want the name of the user appear in Index.htm (replace the "Name 1" text). This will be a real time update, so if there is another user fill in the form, then bellow the "Name 1" text there will be "Name 2".

Anybody have any ideas on how to do this?

Source ( Just Incase Needed):

Index.htm
--------------------------------------------------------------------------

<body>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><p>Please proceed to the <a href="Application.htm" target="_blank">submision form</a>
</p>
<table width="639" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="466">Name 1 </td>
</tr>
</table>
</td></tr>
</table>
</body>
--------------------------------------------------------------------------

Form.htm
--------------------------------------------------------------------------

<body>
<form id="form1" name="form1" method="post" action="">
<table width="0" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td><label>
<input name="Name" type="text" id="Name" />
</label></td>
</tr>
<tr>
<td>Addresses</td>
<td><label>
<input name="Address" type="text" id="Address" value="" />
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="Submit" value="Send" />
</label></td>
<td></td>
</tr>
</table>
</form>
</body>
--------------------------------------------------------------------------

Thx in adnvance.

davidj
03-09-2007, 08:29 AM
you are stepping into a world of programming

i would try PHP

http://www.dreamweaverclub.com/vtm

neuro
03-09-2007, 12:14 PM
ok.. I tried the "PHP - A Beginners GuidePHP - A Beginners Guide" video turotial, now i'm stuck at tutorial part 7.

When i tried to get a preview (by pressing F12 from my dreamweaver) of my "Index.php" i got this error bellow:

Parse error: syntax error, unexpected T_VARIABLE in C:\Documents and Settings\Harriman\My Documents\Web Root\test\Connections\Connection.php on line 4

Any idea?

Thanks

lux
03-09-2007, 12:32 PM
can you post your code so we can have a look

neuro
03-09-2007, 12:49 PM
K dun mind the first error, just realize i was missing this ; sign.

Now everything works :)

Thanks anyway.

neuro
03-09-2007, 06:37 PM
k.. i have second question now.

Everything works fine now, except i was expecting that once i click the submit button, i wan my "index.php" to auto refresh (so user can see the result) while "my form.php" will redirect me (on the same page) to a "Thank You" message.

My current Form.php code:
<?php
require_once("Connections/Connection.php");
///////////////////////////////////////////
$data2 = $_POST['Name'];
$data3 = $_POST['Address'];
$data4 = $_POST['Phone'];
$data5 = $_POST['Email'];
$submit = $_POST['Submit'];
///////////////////////////////////////////
if ($submit && $data2 && $data3 && $data4 && $data5){
///////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4, field5) values ('$data2', '$data3', '$data4', '$data5')");
mysql_query($query)or die(mysql_error());
///////////////////////////////////////////
}elseif($submit) {
echo "One of yout fields is empty !";
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</div>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="0" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="151">Name</td>
<td width="161">:
<label>
<input name="Name" type="text" id="Name" />
</label></td>
</tr>
<tr>
<td>Address</td>
<td>:
<input name="Address" type="text" id="Address" /></td>
</tr>
<tr>
<td>Phone Number </td>
<td>:
<label>
<input name="Phone" type="text" id="Phone" />
</label></td>
</tr>
<tr>
<td>E-mail</td>
<td>:
<label>
<input name="Email" type="text" id="Email" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>:
<input type="submit" name="Submit" value="Submit" />
<input name="submit" type="hidden" id="submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>

neuro
03-11-2007, 05:07 AM
Push.. my post to get some answer :)

lux
03-11-2007, 10:03 AM
use header() to redirect the user when a record has been successfully added to the db


$query = sprintf("INSERT into table1 (field2, field3, field4, field5) values ('$data2', '$data3', '$data4', '$data5')");
mysql_query($query)or die(mysql_error());
header('location:thankyoupage.php');

neuro
03-11-2007, 01:27 PM
do i need to create a new page for that "header ()"Thanks :)

lux
03-11-2007, 08:41 PM
sorry just re-read you message,

if you want to send the user to another page use header()

otherwise write another if statement that when true displays a thank you message much in the same way that your message telling the user that they have forgotten to fill out a field message works

neuro
03-12-2007, 09:28 PM
Its alrite.. anyway thanks for the answer.

So everything works fine now... i tried to follow the other tutorial about PHP log in video tutorial. everything works great, except the "$_session[]" part.

Source for Index.php
<?php
require_once("Connections/Connection.php");
session_start ();
//Catch field data
$userid = $_POST['userid'];
$passid = $_POST['passid'];
$submit = $_POST['Submit'];
if ($userid && $passid) {
/////////////////////////////////////////////
$query = sprintf ("SELECT * FROM EmployerList where Username='$userid' and Password='$passid'");
$result = @mysql_query ($query);
$rowAccount = @mysql_fetch_array ($result);
/////////////////////////////////////////////
}
if ($rowAccount) {
$_SESSION['id'] = $rowAccount['user_id'];
header ("location: adsForm.php");
exit;
} elseif ($submit) {
echo "You Enter Wrong Username or Password";
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table width="0" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="800" height="128"><div align="center">Banner Goes Here </div></td>
</tr>
<tr>
<td><table width="0" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="800" height="88"><p>Intro Text Goes here</p>
<p>In order to post an ads, please log in with your username and password.</p>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="0" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="90">Username</td>
<td width="208">:
<label>
<input name="userid" type="text" id="userid" />
</label></td>
</tr>
<tr>
<td>Password</td>
<td>:
<input name="passid" type="password" id="passid" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td> :
<label>
<input type="submit" name="Submit" value="Log In" />
<input name="submitted" type="hidden" id="submitted" value="1" />
</label></td>
</tr>
</table>
</form>
<p>If you don't have a username, please fill up this <a href="RegisterForm.php" target="_top">form</a>. </p>
<p>&nbsp;</p></td>
</tr>
<tr>
<td height="157"><table width="0" border="0" cellspacing="0" cellpadding="0">

</table>
<table width="0" border="0" cellspacing="0" cellpadding="0">
<?php
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page

$max_results = 5;
// Figure out the limit for the query based
// on the current page number.

$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM AdsList LIMIT $from, $max_results");
?>
<table width="0" border="0" cellspacing="0" cellpadding="0">

<?php do { ?>
<tr>
<td><?php echo $row['Name']; ?></td>
</tr>
<tr>
<td><?php echo $row['Contact']; ?></td>
</tr>
<tr>
<td><?php echo $row['BusinessCat']; ?></td>
</tr>
<tr>
<td><?php echo $row['Vacancy']; ?></td>
</tr>
<tr>
<td><?php echo $row['Description']; ?></td>
</tr>
<?php } while($row = @mysql_fetch_array($sql)); ?>
</table>
<?php
// Figure out the total number of results in DB:

$total_results = @mysql_result(mysql_query("SELECT COUNT(*) as Num FROM AdsList"),0);
// Figure out the total number of pages. Always round up using ceil()

$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks

echo "<center>Select a Page<br />";
// Build Previous Link

if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link

if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}

echo "</center>";

?>

</table></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>


Source for AdsForm.php
<?php
require_once("Connections/Connection.php");
session_start ();
if (! isset($_SESSION['id'])){
header ("location: Index.php");
exit;
}
$id = $_SESSION['id'];
///////////////////////////////////////////
$query = sprintf ("SELECT * FROM EmployerList where user_id='$id'");
$result = @mysql_query ($query);
$rowAccount = @mysql_fetch_array (result);
///////////////////////////////////////////
///////////////////////////////////////////
$data2 = $_POST['$id'];
$data3 = $_POST['name'];
$data4 = $_POST['contact'];
$data5 = $_POST['businesscat'];
$data6 = $_POST['vacancy'];
$data7 = $_POST['description'];
$submit = $_POST['Submit'];
///////////////////////////////////////////
if ($submit && $data3 && $data4 && $data5 && $data6 && $data7){
///////////////////////////////////////////
$query = sprintf("INSERT into AdsList (PostedBy, Name, Contact, BusinessCat, Vacancy, Description) values ('$data2', '$data3', '$data4', '$data5', '$data6', '$data7')");
mysql_query($query)or die(mysql_error());
header ("location: Index.php");
///////////////////////////////////////////
}elseif($submit) {
echo "One of yout fields is empty !";
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</div>
<div align="center">welcome <?php echo $rowAccount ['Username']; ?></div>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="0" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="151">Name</td>
<td width="161"><label>
<input name="name" type="text" id="name" size="25" maxlength="50" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Phone Number </td>
<td><label>
<input name="contact" type="text" id="contact" size="20" maxlength="20" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Business</td>
<td><label>
<input name="businesscat" type="text" id="businesscat" size="25" maxlength="30" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Vacancy</td>
<td><label>
<input name="vacancy" type="text" id="vacancy" size="25" maxlength="30" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Description</td>
<td><label>
<textarea name="description" cols="100" rows="5" id="description"></textarea>
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="Submit" value="Submit" />
<input name="submitted" type="hidden" id="submitted" value="1" />
</label></td>
</tr>
</table>
</form>
</body>
</html>


So.. right now "AdsForm.php" supposed to be accesible only for registered user, so i dun wan any user to be able to access this page using backdoor (by typing www.localhost/AdsForm (http://www.localhost/AdsForm)).

That $_session suppose to do the trick right? right now my problem even when i log in with the correct username and password, it will redirect me to Index.php as if the $_session['id'] is not set.

lux
03-12-2007, 10:54 PM
remove the space between session_start and () so it reads session_start();

neuro
03-13-2007, 04:27 AM
doenst work...

it still re-direct me back to index.php

Any other sugestion?

davidj
03-13-2007, 08:40 AM
where are you setting...


$_SESSION['id']


need to see your login script

neuro
03-13-2007, 09:15 AM
Nvm.. solve the problem.

Turn up "User_id" is suppose to be "user_name". still wondering how the video tutorial works during the preview.

Thanks again.