siabanie Posted January 25, 2011 Share Posted January 25, 2011 I wonder if anyone can help me using session. I have two forms (create.php and modify.php) and I would like to make it one form by using function 'session'. I tried to register the variables before using them but am not sure the way I declare them is correct or not. Please assist. <?php session_start(); //Register our session variables session_register('id); session_register('name'); session_register('surname'); session_register('dept'); //Store our posted values in the session variables $_SESSION['name'] = $_POST['id']; $_SESSION['name'] = $_POST['name']; $_SESSION['surname'] = $_POST['surname']; $_SESSION['dept'] = $_POST['dept']; .... .... ?> Here are the original code before using 'session' the modify.php is about the same only the SQL query need to be added but I don't know how we can use session so that we only need one form instead of two. I hope someone can help me out. Thanks. create.php <?php $id = ""; $name = ""; $surname = ""; $dept= ""; if (isset($_POST['submit'])) { $id = $_POST['id']; $name = $_POST['name']; $surname = $_POST['surname']; $dept = $_POST['dept']; } ?> <html ><head></head><body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div align="center"> <table width="300" border="1"> <h1> Modifying A User </h1> <tr> <th scope="row">Name</th> <td><input type="text" name="name" value="<?php echo $name;?>" /></td> </tr> <tr> <th scope="row">Surname</th> <td> <input type="text" name="surname" value="<?php echo $surname; ?>" /></td> </tr> <tr> <th scope="row">Department</th> <td><input type="text" name="dept" value="<?php echo $dept_name; ?>" /></td> </tr> </table> <br/> <a href="index.php"> <input type="button" name="back" value="Back" /></a> <input type="submit" name="submit" value="Modify"/> </div> </form> </body></html> Link to comment https://forums.phpfreaks.com/topic/225646-using-session/ Share on other sites More sharing options...
AbraCadaver Posted January 25, 2011 Share Posted January 25, 2011 It doesn't appear to me that you need to use a session for this. You can have one form and pass it a value in the link that is clicked to get it to tell whether it is create or modify. Based on that run a SELECT query or not and create the form with a submit button with a value of either Add or Save. Then when submitted check which submit was clicked and run the appropriate query, INSERT or UPDATE. Link to comment https://forums.phpfreaks.com/topic/225646-using-session/#findComment-1165074 Share on other sites More sharing options...
siabanie Posted January 25, 2011 Author Share Posted January 25, 2011 Hi AbraCadaver, The thing is I have almost similar pages for create.php and modify.php and I would like to use SESSION instead of $_GET and $_POST so I can make these two forms into one only.. Here are both of the codes: modify.php <?php // exit; require 'includes/application_top.php'; $name = ""; $surname = ""; $add = ""; $dept= ""; $mobile = ""; if (!isset($_POST['submit'])) { $q = "SELECT * FROM persons WHERE ID = $_GET[id]"; $result = mysql_query($q) or die (mysql_error()); $row = mysql_fetch_array($result); $name = $row['name']; $surname = $row['surname']; $add = $row['address']; $dept = $row['dept_id']; $mobile = $row['mobile']; } if (isset($_POST['submit'])) { $name = $_POST['name']; $surname = $_POST['surname']; $add = $_POST['add']; $dept = $_POST['dept']; $mobile = $_POST['mobile']; $errormsg = ""; if($name == "") $errormsg = $errormsg. "Name<br/ >"; if ($surname == "") $errormsg = $errormsg. "Surname Please <br/ >"; if ($mobile != "" && !is_numeric ($mobile)) $errormsg = $errormsg. "Mobile No? <br/ >"; if ($errormsg != "") echo "Please fill the blank info: <br/ > $errormsg"; else { $input_field = ""; foreach ($_POST as $key => $val) { if ($key != 'submit') { $input_field.= "<input type = 'hidden' name = '$key' value = '$val'/>"; } } ?> <!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>Create Confirm</title> </head> <body onload='document.modifyConfirmation.submit();'> <!--a href="deleteUser.php?id=<?php echo $id;?>">Delete this user</a--> <form name="modifyConfirmation" action="confirmModify.php" method="post"> <!--form name="modifyConfirmation" action="confirmModify.php?id=<?php echo $id;?>" method="post"--> <?php echo $input_field;?> </form> Submitting......! </body> </html> <?php exit; } } ?> <!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>Modify Document</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div align="center"> <table width="300" border="1"> <h1> Modifying A User </h1> <tr> <th scope="row">Name</th> <td><input type="text" name="name" value="<?php echo $name;?>" /></td> </tr> <tr> <th scope="row">Surname</th> <td> <input type="text" name="surname" value="<?php echo $surname; ?>" /></td> </tr> <tr> <th scope="row">Address</th> <td><input type="text" name="add" value="<?php echo $add; ?>" /></td> </tr> <tr> <th scope="row">Department</th> <td> <select name="dept"> <option value="">Select..</option> <?php $data = mysql_query ("SELECT * FROM dept ORDER BY `id` DESC") or die (mysql_error()); echo $data; while($row_dept = mysql_fetch_array( $data )) { ?> <option value="<?php echo $row_dept['id'] ;?>" <?php if($row_dept['id']==$dept){echo ' selected="selected"';}?>> <?php echo $row_dept['dept_name'] ;?> </option> <?php } ?> </select> </td> </tr> <tr> <th scope="row">Mobile</th> <td><input type="text" name="mobile" value="<?php echo $mobile; ?>" /></td> </tr> </table> <br/> <a href="index.php"> <input type="button" name="back" value="Back" /></a> <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"> <input type="submit" name="submit" value="Modify"/> </div> </form> </body> </html> [b]create.php[/b] [code=php:0] <?php require 'includes/application_top.php'; $name = ""; $surname = ""; $add = ""; $deptid= ""; $mobile = ""; if (isset($_POST['submit'])) { $name = $_POST['name']; $surname = $_POST['surname']; $add = $_POST['add']; $dept = $_POST['dept']; $mobile = $_POST['mobile']; $errorstring = ""; //default value of error string if($name == "") $errorstring = $errorstring."Name<br/>"; if($surname == "") $errorstring = $errorstring."Surname<br/>"; if($mobile != "" && !is_numeric($mobile)) $errorstring = $errorstring."Mobile Should be in numeric<br/>"; if ($errorstring != "") echo "Please fill out the following fields: <br/> $errorstring"; else { $input_field = ""; foreach ($_POST as $key => $val) { if ($key != "submit") { $input_field .= "<input type='hidden' name = '$key' value = '$val' />"; } } ?> <!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>Create Confirm</title> </head> <body onload='javascript:document.createConfirmation.submit();'> <form name="createConfirmation" action="confirmCreate.php" method="post"> <?php echo $input_field;?> </form> Submitting... </body> </html> <?php exit; } } ?> <!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>Create Document</title> <style type="text/css"> <!-- .style2 {font-size: 36px} --> </style> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div> <div align="center"> <table width="300" border="1"> <h1> Create User </h1> <tr> <th><strong>Name * </strong></th> <td><input type="text" name="name" value="<?php echo $name;?>" /> </td> </tr> <tr> <th><strong>Surname * </strong></th> <td><input type="text" name="surname" value="<?php echo $surname;?>" /> </td> </tr> <tr> <th><strong>Address</strong></th> <td><input type="text" name="add" value="<?php echo $add;?>" /> </td> </tr> <tr> <th><strong>Department</strong></th> <td> <select name="dept"> <option value="">Select...</option> <?php $data = mysql_query ("SELECT * FROM dept ORDER BY `id` DESC") or die (mysql_error()); while($row = mysql_fetch_array( $data )) { ?> <option value="<?php echo $row['id'];?>"> <?php echo $row['dept_name'] ;?></option> <?php } ?> </select> </td> </tr> <tr> <tr> <th><strong>Mobile</strong></th> <td><input type="text" name="mobile" value="<?php echo $mobile;?>" /> </td> </tr> <tr> <tr> <td colspan="2">* required</td> </tr> </table> <br /> <a href="index.php"> <input type="button" name="back" value="Back" /></a> <input type="reset" name="reset" value="Reset" /> <input type="submit" name="submit" value="Create"/> </div> </div> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/225646-using-session/#findComment-1165095 Share on other sites More sharing options...
lazylodr Posted January 25, 2011 Share Posted January 25, 2011 If you are trying to prevent duplicate form code then I agree with AbraCadaver, sessions will not help you here. His suggestion is spot on. I only wanted to add that Zend_Form in the Zend Framework, specifically the populate method, can also help you achieve what you are looking for. Link to comment https://forums.phpfreaks.com/topic/225646-using-session/#findComment-1165167 Share on other sites More sharing options...
siabanie Posted January 25, 2011 Author Share Posted January 25, 2011 I see ok, so there is no way I can use 'SESSION' instead of $_POST and $_GET in my form? As I have two almost similar form (create.php and modify.php) I would like to make it only one form call createAndmodify.php using SESSION then go to another confirmation page for these form. I thought we can do that? Link to comment https://forums.phpfreaks.com/topic/225646-using-session/#findComment-1165186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.