Jump to content

using sessions for form validation


gaza165

Recommended Posts

<?php
session_start();

include ('../dbconnect/dbconnect.php');
$name = mysql_real_escape_string($_POST['name']);
$comments = mysql_real_escape_string($_POST['comments']);

if($_POST['subit']) {

if($_POST['name'] == "") {

$_SESSION['message'] = "You did not enter your name";

} else {

if($_POST['comments'] == "") {

$_SESSION['message'] = "You did not enter your comments";

} else {

$result = "INSERT INTO public_comments (name,message) values ('$name','$comments')";
$sql = mysql_query($result);


}

}

}



?> 

<script type="text/javascript">

window.location = "../portfolio.php"

</script>

 

i need some form validation on my page... when the user submits the form it goes to this page, but before it inserts the data into the db i want to check that they have entered data into the name and comments field...

 

do i use sessions to store the message and when it is redirected back to the page i need to display the message and get them to fill the form in again??

 

need some help... thanks

 

 

Garry

Link to comment
Share on other sites

Use a single page for your form and your form processing code, with validation, display of any validation errors, and putting submitted values back into form fields. Here is a simplified example -

 

<?php
// basic form and form processing code -

// condition the inputs and setup default values -
$submitted = isset($_POST['submit']) ? $_POST['submit'] : FALSE; // was the form submitted?
$name_field = isset($_POST['name']) ? $_POST['name'] : ""; // condition the form's name field

if($submitted) {
$form_error = array(); // array to hold any form validation errors

// validate the form data here (set elements in $form_error to hold error messages)
	if(empty($name_field)) {
	$form_error[] = "Please fill in the name";
}

// if there were no form validation errors, use the data that was submitted
if(empty($form_error)) {
	// do something with the data here
	echo "The name you entered was: $name_field";
}
}

// display the form if it has not been submitted or there are form validation errors
If(!$submitted || !empty($form_error)) {
// check for and display any form validation errors
if(!empty($form_error)) {
	echo "Please correct these errors -<br />";
	foreach($form_error as $error) {
		echo "$error<br />";
	}
}

// display the form, with any previously submitted values
?>
<form method="post" action="">
Name: <input type="text" name="name" value="<?php echo $name_field; ?>">
<input type="submit" name="submit">
</form>
<?php
}
?>

Link to comment
Share on other sites

my question is...

 

when i redirect back to the page to display the message, what code should i be using to display the message or to not....

 

 

atm im doing

 

i would preffer to do the validation on the page it posts to and redirect back with the response...

 

 

		   <div id="contactform">
		   <a name="sendmessage">
		   <form action="blog/sendmessage.php" method="POST">
		   <p class="input">Name:<input type="text" name="name" id="name" /></p> 
		   <p class="input">Comments:<textarea cols="55" rows="5" id="comments" name="comments"></textarea></p>  
		   <input type="submit" id="send_message" name="subit" value="Send Message" class="contactbutton"/>	
		   </form>
		   <? if(isset($_SESSION['message'])) { echo $_SESSION['message']; } ?>
		   </a>
		   </div>

Link to comment
Share on other sites

		<?php
// basic form and form processing code -

// condition the inputs and setup default values -
$submitted = isset($_POST['submit']) ? $_POST['submit'] : FALSE; // was the form submitted?
$name = isset($_POST['name']) ? $_POST['name'] : ""; // condition the form's name field
$comments = isset($_POST['comments']) ? $_POST['comments'] : ""; // condition the form's name field

if($submitted) {

include ('dbconnect/dbconnect.php');

$form_error = array(); // array to hold any form validation errors

// validate the form data here (set elements in $form_error to hold error messages)
	if(empty($name)) {
	$form_error[] = "Please fill in the name";
	}
	if(empty($comments)) {
	$form_error[] = "Please fill in the comments";
	}

// if there were no form validation errors, use the data that was submitted
if(empty($form_error)) {
	// do something with the data here
	include ("blog/sendmessage.php"); 
}
}

// display the form if it has not been submitted or there are form validation errors
If(!$submitted || !empty($form_error)) {
// check for and display any form validation errors
if(!empty($form_error)) {
	foreach($form_error as $error) {
		echo "$error<br />";
	}
}

}
// display the form, with any previously submitted values
?>
		   <div id="contactform">
		   <a name="sendmessage">
		   <form action="#showcomments" method="POST">
		   <p class="input">Name:<input type="text" name="name" id="name" value="<?php echo $name; ?>" /></p> 
		   <p class="input">Comments:<textarea cols="55" rows="5" id="comments" name="comments"></textarea></p>  
		   <input type="submit" id="send_message" name="submit" value="Send Message" class="contactbutton"/>	
		   </form>
		   </a>
		   </div>

 

the only problem with this is that beacuase it does not post to another page.... what if the user keeps hitting F5??? it will keep posting the data....

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.