Jump to content

Using PHP functions for forms


mazman13

Recommended Posts

Ive never used functions in a PHP script...so I started to use them with this form. I was wondering if my setup is going the right way before I continue.

 

<?php 
#####//Site Functions Start Here//#####

function text_field($name, $value, $size, $max)
{
echo"<input name=\"$name\" type=\"text\" value=\"$value\" size=\"$size\" maxlength=\"$max\" />";	
}

#####//Processes Start Here//#####

if($_REQUEST['submit_check'] == 1)
{
if($_REQUEST['fname'] == "")
	{
	echo "Please enter first name.";
	$error = 1;
	}
else{
	echo "Information has been added.";
	$error = 0;
	}
} 
?>
<!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>Guest Database</title>

<!-- Start Style-->
<style>
body {
background:#FFFFFF;
font-family:Verdana, Arial, Helvetica, sans-serif;
size:10px;	
}
</style>

</head>

<body>
<!-- Start forms-->
<form name="add_edit_info" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if(!isset($_REQUEST['submit_check']) and $error != "1") ////Start blank form
{
echo "First Name: ";
text_field("fname", "", "20", "20");
echo "<br />Last Name: ";
text_field("lname", "", "20", "20");
echo "<input name=\"submit_check\" type=\"hidden\" value=\"1\" />";
echo "<br /><input name=\"submit\" type=\"submit\" value=\"Submit\" />";
echo "test 1";
}
else ////If something is wrong, or the entry needs to be editied, start edit form
{
echo "First Name: ";	
text_field("fname", $_REQUEST['fname'], "20", "20");
echo "<br />Last Name: ";
text_field("lname", $_REQUEST['lname'], "20", "20");
echo "<input name=\"submit_check\" type=\"hidden\" value=\"1\" />";
echo "<br /><input name=\"submit\" type=\"submit\" value=\"Submit\" />";
echo "test 2";
}

?>
</form>
</body>
</html>

 

This would be if they are adding a new entry. And if it's wrong, it shoots back a form with the data and the correct or add it.

 

Is it best to do all this in one script? Or should I break it down and have different pages?

If I want to make a script with a form to EDIT data they already have in the DB, should I add that to this page? Or make it's own page? Or, if I do add it to this page, should I write the script in another page and include it here using an if statement?

 

Sorry. Bunch of questions. Just want to make it right the first time.

Link to comment
Share on other sites

Just a few hints.

 

Instead of escaping 999 times, you could do this:

 

<?php

// This
echo '<br /><input name="submit" type="submit" value="Submit" />';

// Is the same as this
echo "<br /><input name=\"submit\" type=\"submit\" value=\"Submit\" />";

// And you don't even need to echo all of your HTML code.

?>

 

Doesn't your function result in an error? Also, I'm afraid I'm a little confused at what you are trying to do (maybe that's just me, I don't know), but I don't understand this sentence of yours:

 

And if it's wrong' date=' it shoots back a form with the data and the correct or add it.[/quote']

 

I don't see why you would want to bring out a new input field if the entered information is wrong; why not just display a message saying what's wrong and use the same fields again? I'm a little confused so if I'm way off, never mind. :P

Link to comment
Share on other sites

Sorry about that sentence. Lol that's what I for trying to multitask. The form is going to be much longer. But if something is wrong or is left blank, I want the form to come back with all the info they typed in. I'd hate for them to type everything all over again becuase they forgot to check a box or something.

Link to comment
Share on other sites

Here is an example of how you could do that with a single form:

 

<?php

if (isset($_POST['your_button']))

{

if (empty($_POST['field1'])) // If field1 is empty

{

       echo "Field1 is empty!";

}

elseif (empty($_POST['field2'])) // If field2 is empty

{

      echo "Field2 is empty!";

}

else // If none of the above is true, the user has filled information into both fields

{

      echo "Success!";

}

}

?>


<form name="your_form" method="post">

<input type="text" name="field1" value="<?php echo $_POST['field1']; ?>">

<input type="text" name="field2" value="<?php echo $_POST['field2']; ?>">

<input type="submit" name="your_button" value="Submit">

 

If the user typed in some info in one of the fields but left the other empty, an error message will appear and the info the user did in fact fill in will still be there.

 

If I made a small mistake in the code, I'm sorry; I just wrote it real fast. :)

Link to comment
Share on other sites

@Mazman13

 

You are not restricted also to the examples of Andy. This works fine also.

 

<?php

if(trim($_POST['field1'])=="")
{
echo "field1 error noticed";
}
?>

 

I have used "TRIM" because, "As of PHP 5, objects with no properties are no longer considered empty". Also, "As of PHP 4, The string value "0" is considered empty" according to www.php.net

 

Hope that helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.