PDA

View Full Version : Problem with PHP forms


Kan20
04-21-2010, 03:19 PM
Hello,

Iím creating an Admin page wherein the admin can enter the details in the MySQL database using forms. Basically, Iíve created three forms: Product (to enter Product details), Property (to enter Product Property details), and Specification (to enter Property Specification details).

I decided to display these forms one at time and have used layers for it (in Dreamweaver). Iíve used JavaScript to show/hide layers.

Problem:
My forms works fine individually (i.e. if put up in different files). But if I put them in one file then only one form is being displayed. The other form gets displayed only when the other two forms are deleted.

Hereís my Product form code:
<div id="Product">
<p align="center" class="section2">Add/Modify/Delete Product Details</p>
<p align="right"><a href="#back" class="orange">Back</a></p>
<p align="right">&nbsp;</p>
<p align="center"><?php
/************************************************** ***********************
control code for application
************************************************** ***********************/

//submit button was pressed so call the process form function
if (isset($_POST['submit']))
{
process_form();
die();
}//end if


//call the get_data function
if (isset($_GET['ID']))
{
get_data();
}//endif


//nothing chosen so list the products
if ((empty($_POST))&&(empty($_GET)))
{
list_products();
die();
}//end if


//request to add a new product so call the show_form function
if ((isset($_GET['action']))&&($_GET['action']=='add'))
{
show_form();
}//endif



/************************************************** ***********************
get the data for an individual contact
************************************************** ***********************/

function get_data()
{
//validate the ID has been passed at that it is a number
if ((empty($_GET['ID']))||(is_nan($_GET['ID'])))
{
//there was a problem so list the products again
list_products();
//kill the script
die();
}else{
//all is ok and assign the data to a local variable
$ID = $_GET['ID'];
}//end if
$sql = "select * from ttproducts where ID = $ID";
$result = conn($sql);
if (mysql_num_rows($result)==1){
//call the form and pass it the handle to the resultset
show_form($result);
}else{
$msg = "No data found for selected product";
confirm($msg);
//call the list products function
list_products();
}//end if
}//end function


/************************************************** ***********************
show the input / edit form
************************************************** ***********************/
function show_form($handle='',$data='')
{
//$handle is the link to the resultset, the ='' means that the handle can be empty / null so if nothing is picked it won't blow up

//set default values
$product_name = '';
$product_code = '';
$product_description = '';

$ID = '';
$value = 'Add'; //submit button value
$action = 'add';
$delete = 'delete'; //default form action is to delete the product from db

//set the action based on what the user wants to do
if ($handle)
{
//set form values for button and action
$action = "edit";
$value = "Update";
$delete = "delete";

//get the values from the db resultset
$row = mysql_fetch_array($handle);
$product_name = $row['product_name'];
$product_code = $row['product_code'];
$product_description = $row['product_description'];
$ID = $row['ID'];

}//end if

//error handling from the processing form function
if($data != '')
{
$elements = explode("|",$data);
$product_name = $elements[0];
$product_code = $elements[1];
$product_description = $elements[2];
$ID = $elements[3];
}
?>
<p class="section"><span class="bold">Add/Update the Product </span><br/>
<br/>
</p>
<form name="product" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?action=<?php echo $action?>">
<table width="515" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center" style="font-size:18px; font-weight:bold;"></td>
<input type="hidden" value="<?php echo $ID?>" name="ID">
</tr>
<tr>
<td colspan="2" align="center" class="headings"></td>
</tr>
<tr>
<td width="160" align="left" class="samples"><p>Product Name: </p></td>
<td width="315" class="samples"><p>
<input name="product_name" type="text" size="30" value="<?php echo $product_name?>">
</p></td>
</tr>
<tr><td></td></tr>
<tr>
<td width="160" align="left" class="samples"><p>Product Code: </p></td>
<td width="315" class="samples"><p>
<input name="product_code" type="text" size="30" value="<?php echo $product_code?>">
</p></td>
</tr>
<tr>
<td width="160" align="left" class="samples"><p>Product Description: </p></td>
<td width="315" class="samples"><p>
<input name="product_description" type="text" size="30" value="<?php echo $product_description?>">
</p></td>
</tr>

<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" class="orange" value="<?php echo $value?>">
<input name="reset" type="reset" class="orange" value="Clear Form"></td>
</tr>
</table>

</form>
<p><br/><br/></p>
<p class="section"><span class="bold">Delete the Product </span><br/>
<br/>
</p>
<form name="product" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?action=<?php echo $delete?>">
<table width="515" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center" style="font-size:18px; font-weight:bold;"></td>
<input type="hidden" value="<?php echo $ID?>" name="ID">
</tr>
<tr>
<td colspan="2" align="center" class="headings"></td>
</tr>
<tr>
<td width="160" align="left" class="samples"><p>Product Name: </p></td>
<td width="315" class="samples"><p>
<input name="product_name" type="text" size="30" value="<?php echo $product_name?>">
</p></td>
</tr>


<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" class="orange" value="<?php echo $delete?>"> </td>
</tr>
</table>

</form>
<?php
}//end function


/************************************************** ***********************
list all the contacts in the db
************************************************** ***********************/
function list_products()
{
$y = 0; //counter

$sql = "select * from ttproducts "; //may want to add the option where clause to only take kids with an active status
$result = conn($sql);

echo "<table width='400' align='center' cellpadding='0' cellspacing='0'>
<tr><td colspan='2' align='center' style='font-size:18px; font-weight:bold;'></td></tr>
<tr><td colspan='2'> </td></tr>
<tr><td class='bold' colspan='2'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new product</a></td></tr>
<tr><td class='bold' colspan='2'> </td></tr>";

if (mysql_num_rows($result)){
//show a list of kids with name as a link to the prepopulated form with their data in it
while($rows = mysql_fetch_array($result)){

//change row background color


//build strings to make life easier
$product_name = $rows['product_name'];
$product_code = $rows['product_code'];
$product_description = $rows['product_description'];

$ID = $rows['ID'];


//echo out the row
echo "<tr style='background-color:$bgcolor;'><td><a href='".$_SERVER['PHP_SELF']."?ID=$ID'>$product_name</a></td><tr>";
$y++; //increment the counter
}//end while
echo "</table>";
}else{
//handle no results
echo "<tr><td class='orange' colspan='2' align='center'><b>No data found.</b></td></tr>";
}//endif
}


/************************************************** ***********************
add / update the contact's data
************************************************** ***********************/
function process_form()
{
$product_name = '';
$product_code = '';
$product_description = '';

$ID = '';
$action = '';
$delete = '';

//$q2 = "select * from ttproducts";
//$result2=mysql_query($q2);
//$num=mysql_numrows($result2);
//echo $num;
//$num++;
//$ID = $num;
$ID = @$_POST['ID'];
$product_name = @$_POST['product_name'];
$product_code = @$_POST['product_code'];
$product_description = @$_POST['product_description'];
$action = @$_GET['action'];
$delete = @$_GET['delete'];


//if no status is set, defaults to 0 (allow contact)




//You could add some validation of the data ( I recommend it and its a great way to get your feet wet with php )


if ($action == "add")
{
$sql = "insert into ttproducts (ID, product_name, product_description, product_code ) values('$ID', '$product_name','$product_description', '$product_code')";
$msg = "Record successfully added";
}elseif($action=="edit"){
$sql = "update ttproducts set ID = '$ID', product_name = '$product_name', product_description = '$product_description', product_code = '$product_code' where ID = $ID";
$msg = "Record successfully updated";
}
if ($action == "delete")
{
$sql = "DELETE FROM ttproducts WHERE product_name = '$product_name'";
$msg = "Record successfully deleted";
}
$result = conn($sql);
if (mysql_errno()==0)
{
confirm($msg);
list_products();
}else{
$msg = "There was a problem adding the product to the database. Error is:".mysql_error();
confirm($mag);
}//end if

}


/************************************************** ***********************
db connection function
************************************************** ***********************/
function conn($sql)
{

$host = "localhost";
$user = "root";
$pass = "";
$db = "templetree";


//echo "commnecing connection to local db<br>";

if (!($conn=mysql_connect($host, $user, $pass))) {
printf("error connecting to DB by user = $user and pwd=$pass");
exit;
}
$db3=mysql_select_db($db,$conn) or die("Unable to connect to local database");

$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());

return $result;

}//end function

/************************************************** ***********************
alert box popup confimation message function
************************************************** ***********************/
function confirm($msg)
{
echo "<script langauge=\"javascript\">alert(\"".$msg."\");</script>";
}//end function

?></p></div>

I really cannot understand why they are not working in one file. Please assist me in solving this issue.

Thank in advance

Kan20
04-21-2010, 03:20 PM
Hi,

And here's my Property form code:
<div id="Property"><?php
/************************************************** ***********************
control code for application
************************************************** ***********************/

//submit button was pressed so call the process form function
if (isset($_POST['submit']))
{
process_property_form();
die();
}//end if


//call the get_property function
if (isset($_GET['ID']))
{
get_property();
}//endif


//nothing chosen so list the property
if ((empty($_POST))&&(empty($_GET)))
{
list_property();
die();
}//end if


//request to add a new property so call the show_property_form function
if ((isset($_GET['action']))&&($_GET['action']=='add'))
{
show_property_form();
}//endif



/************************************************** ***********************
get the data for an individual contact
************************************************** ***********************/

function get_property()
{
//validate the ID has been passed at that it is a number
if ((empty($_GET['ID']))||(is_nan($_GET['ID'])))
{
//there was a problem so list the property again
list_property();
//kill the script
die();
}else{
//all is ok and assign the data to a local variable
$ID = $_GET['ID'];
}//end if
$sql = "select * from ttproperty where ID = $ID";
$result = conn_property($sql);
if (mysql_num_rows($result)==1){
//call the form and pass it the handle to the resultset
show_property_form($result);
}else{
$msg = "No data found for selected property";
confirm_property($msg);
//call the list property function
list_property();
}//end if
}//end function


/************************************************** ***********************
show the input / edit form
************************************************** ***********************/
function show_property_form($handle='',$data='')
{
//$handle is the link to the resultset, the ='' means that the handle can be empty / null so if nothing is picked it won't blow up

//set default values
$property_name = '';

$property_description = '';

$ID = '';
$value = 'Add'; //submit button value
$action = 'add';
$delete = 'delete'; //default form action is to delete the property from db

//set the action based on what the user wants to do
if ($handle)
{
//set form values for button and action
$action = "edit";
$value = "Update";
$delete = "delete";

//get the values from the db resultset
$row = mysql_fetch_array($handle);
$property_name = $row['property_name'];

$property_description = $row['property_description'];
$ID = $row['ID'];

}//end if

//error handling from the processing form function
if($data != '')
{
$elements = explode("|",$data);
$property_name = $elements[0];

$property_description = $elements[1];
$ID = $elements[2];
}
?>
<p class="section"><span class="bold">Add/Update the Property </span><br/>
<br/>
</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=<?php echo $action?>" method="post" name="property" id="property">
<table width="515" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center" style="font-size:18px; font-weight:bold;"></td>
<input type="hidden" value="<?php echo $ID?>" name="ID" />
</tr>
<tr>
<td colspan="2" align="center" class="headings"></td>
</tr>
<tr>
<td width="160" align="left" class="samples"><p>Property Name: </p></td>
<td width="315" class="samples"><p>
<input name="property_name" type="text" size="30" value="<?php echo $property_name?>" />
</p></td>
</tr>
<tr>
<td></td>
</tr>

<tr>
<td width="160" align="left" class="samples"><p>Property Description: </p></td>
<td width="315" class="samples"><p>
<input name="property_description" type="text" size="30" value="<?php echo $property_description?>" />
</p></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" class="orange" value="<?php echo $value?>" />
<input name="reset" type="reset" class="orange" value="Clear Form" /></td>
</tr>
</table>
</form>
<p><br/>
<br/>
</p>
<p class="section"><span class="bold">Delete the property </span><br/>
<br/>
</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=<?php echo $delete?>" method="post" name="property" id="property">
<table width="515" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center" style="font-size:18px; font-weight:bold;"></td>
<input type="hidden" value="<?php echo $ID?>" name="ID2" />
</tr>
<tr>
<td colspan="2" align="center" class="headings"></td>
</tr>
<tr>
<td width="160" align="left" class="samples"><p>Property Name: </p></td>
<td width="315" class="samples"><p>
<input name="property_name" type="text" size="30" value="<?php echo $property_name?>" />
</p></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" class="orange" value="<?php echo $delete?>" />
</td>
</tr>
</table>
</form>
<?php
}//end function


/************************************************** ***********************
list all the contacts in the db
************************************************** ***********************/
function list_property()
{
$y = 0; //counter

$sql = "select * from ttproperty "; //may want to add the option where clause to only take kids with an active status
$result = conn_property($sql);

echo "<table width='400' align='center' cellpadding='0' cellspacing='0'>
<tr><td colspan='2' align='center' style='font-size:18px; font-weight:bold;'></td></tr>
<tr><td colspan='2'> </td></tr>
<tr><td class='bold' colspan='2'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new property</a></td></tr>
<tr><td class='bold' colspan='2'> </td></tr>";

if (mysql_num_rows($result)){
//show a list of kids with name as a link to the prepopulated form with their data in it
while($rows = mysql_fetch_array($result)){

//change row background color


//build strings to make life easier
$property_name = $rows['property_name'];

$property_description = $rows['property_description'];

$ID = $rows['ID'];


//echo out the row
echo "<tr style='background-color:$bgcolor;'><td><a href='".$_SERVER['PHP_SELF']."?ID=$ID'>$property_name</a></td><tr>";
$y++; //increment the counter
}//end while
echo "</table>";
}else{
//handle no results
echo "<tr><td class='orange' colspan='2' align='center'><b>No data found.</b></td></tr>";
}//endif
}


/************************************************** ***********************
add / update the contact's data
************************************************** ***********************/
function process_property_form()
{
$property_name = '';

$property_description = '';

$ID = '';
$action = '';
$delete = '';

//$q2 = "select * from ttproperty";
//$result2=mysql_query($q2);
//$num=mysql_numrows($result2);
//echo $num;
//$num++;
//$ID = $num;
$ID = @$_POST['ID'];
$property_name = @$_POST['property_name'];

$property_description = @$_POST['property_description'];
$action = @$_GET['action'];
$delete = @$_GET['delete'];


//if no status is set, defaults to 0 (allow contact)




//You could add some validation of the data ( I recommend it and its a great way to get your feet wet with php )



if ($action == "add")
{
$sql = "insert into ttproperty (ID, property_name, property_description) values('$ID', '$property_name','$property_description')";
$msg = "Record successfully added";
}elseif($action=="edit"){
$sql = "update ttproperty set ID = '$ID', property_name = '$property_name', property_description = '$property_description' where ID = $ID";
$msg = "Record successfully updated";
}
if ($action == "delete")
{
$sql = "DELETE FROM ttproperty WHERE property_name = '$property_name'";
$msg = "Record successfully deleted";
}
$result = conn_property($sql);
if (mysql_errno()==0)
{
confirm_property($msg);
list_property();
}else{
$msg = "There was a problem adding the property to the database. Error is:".mysql_error();
confirm_property($mag);
}//end if

}


/************************************************** ***********************
db connection function
************************************************** ***********************/
function conn_property($sql)
{

$host = "localhost";
$user = "root";
$pass = "";
$db = "templetree";


//echo "commnecing connection to local db<br>";

if (!($conn_property=mysql_connect($host, $user, $pass))) {
printf("error connecting to DB by user = $user and pwd=$pass");
exit;
}
$db3=mysql_select_db($db,$conn_property) or die("Unable to connect to local database");

$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());

return $result;

}//end function

/************************************************** ***********************
alert box popup confimation message function
************************************************** ***********************/
function confirm_property($msg)
{
echo "<script langauge=\"javascript\">alert(\"".$msg."\");</script>";
}//end function

?></div>

davidj
04-22-2010, 08:17 AM
try removing the die();

Kan20
04-23-2010, 07:47 AM
I removed layers and used table instead, as I had to click the links every time to display the forms.

I've put both forms in a table. As you said, I removed all the die(); functions from the forms. Now it displays both the lists i.e., Product and Property lists (previously only one list used to display). But here's the problem:

When I click on "Add new product" link, both the forms (product as well as property) gets displayed. I want it to display only one form at a time. Meaning: If i click on "Add new product" link only Product form should be displayed, and if I click on "Add new property" link then only property form should be displayed.
When I hit submit after filling the Product form the JavaScript message "The product has been added" appears twice.
When I click on any listed Product (to update or delete) it says "No data found for selected property" and the Product form opens only when I click on ok, and vice versa for the Property form.
All the javascript messages appears twiceWhat should I do?

davidj
04-23-2010, 02:45 PM
Did you code this yourself

Kan20
04-24-2010, 12:53 PM
No, I got it from net long back and now I dun remember from where.:(


~ Kan

davidj
04-26-2010, 07:02 AM
contact me through pm for further help

edbr
04-26-2010, 07:10 AM
pm is not working david

Kan20
04-28-2010, 01:56 PM
Hi,

pm is not working. What to do?

Corrosive
04-28-2010, 02:03 PM
You can still e-mail him. Click his name and select 'send e-mail message'.

domedia
04-28-2010, 03:48 PM
Or just keep it in the forum..?

Kan20
04-29-2010, 02:09 PM
I cannot find "send an email option."