Page 1 of 1

FORMS

Posted: Fri May 28, 2010 12:41 am
by wenkep3
I'm developing a website where a user can update their information in the database. When the user first goes to the update page, the function displayUpdateForm is executed. This allows the user to type in the Id for which record they want to update in the database. After they submit the form, the actionUpdateForm function is executed which uses a foreach statement to print all the users information into a form with text fields. Then after they are finished updating the information they press submit one more time which executes the processUpdateForm function and thatll submit the updated information to the database. The problem I'm having is after the user submits the updated information from the actionUpdateForm, the only value being passed in the post array is [submit]=>update. I've looked at the view source in the browser and none of the text fields inside the foreach statement are there even though they are displayed on the screen. Can anyone see where my code is going wrong?

Code: Select all

<?php 
	function displayUpdateForm($message=''){
		userAccess();
		
		print("<div class=\"pageSubHeader\">");
			print("Update Record");
		print("</div>");
		print($message);
		
		print("<form action=\"index.php?update\" method=\"POST\"");
			print("<input type=\"Text\" name=\"Id\"");
			print("<input type=\"Submit\" name=\"Submit\" value=\"Submit\">");
		print("</form>");
	}
	
	function actionUpdateForm(){
		checkNull();
		checkUserExist();
		
		$query="select * from user where Id='".$_POST["Id"]."'";
		$result=mysql_query($query) or die("ERROR IN QUERY");
		$row=mysql_fetch_row($result);
		
		print("<form action=\"index.php?update\" method=\"POST\">");
			foreach($row as $value){
				$column=mysql_fetch_field($_POST["result"]);
				print(ucfirst($column->name).": ");
				print("<input type=\"text\" value=\"$value\">");
				print("<br />");
			}
			print("<input type=\"Submit\" name=\"Submit\" value=\"Update\">");
		print("</form>");
	}
	
	function processUpdateForm(){
		//Debug
		print_r($_POST);
	}

Re: FORMS

Posted: Fri May 28, 2010 1:01 am
by internet-solution
You have not set the name attribute for input elements.

Code: Select all

print("<input type=\"text\" value=\"$value\">");
should be

Code: Select all

print("<input name ='$column->name' type=\"text\" value=\"$value\">");

Re: FORMS

Posted: Fri May 28, 2010 1:04 am
by wenkep3
thanks for the quick response...heres the index page which calls the functions

Code: Select all

<?php
				//Authenticate		
				if(isset($_POST["Login"]) && $_POST["Login"]=="Login"){
					authenticate();
				}
				//Display user forms
				if(isset($_SESSION["Id"], $_SESSION["username"]) && $_POST["Submit"]==""){
					switch($_SERVER["QUERY_STRING"]){
						case("view"):
							displayViewForm();
							break;
						case("update"):
							displayUpdateForm();
							break;
						case("add"):
							displayAddForm();
							break;
						case("delete");
							displayDeleteForm();
							break;
						case("logout");
							logout();
							break;
						default:
							currentUser();
							break;
					}
				//Execute form actions
				}elseif(isset($_SESSION["Id"], $_SESSION["username"]) && $_POST["Submit"]=="Submit"){
					switch($_SERVER["QUERY_STRING"]){
						case("view"):
							actionViewForm();
							break;
						case("update"):
							actionUpdateForm();
							break;
						case("add"):
							actionAddForm();
							break;
						case("delete");
							actionDeleteForm();
							break;
					}
				}elseif(isset($_SESSION["Id"], $_SESSION["username"]) && $_POST["Submit"]=="Update"){
					processUpdateForm();
				//User not logged in
				}else{
					displayForm();
				}

Re: FORMS

Posted: Fri May 28, 2010 1:08 am
by internet-solution
That was quick - I have edited my first post (above) when you were typing your reply.

Re: FORMS

Posted: Fri May 28, 2010 1:12 am
by wenkep3
I have no idea how i missed the name attribute haha
I works perfectly now, thanks for the help and the quick response

Re: FORMS

Posted: Fri May 28, 2010 2:05 am
by internet-solution
wenkep3 wrote:I have no idea how i missed the name attribute haha
don't worry, we've all been there!