Jump to content

form submition


rhock_95

Recommended Posts

$var = $_POST['variable'];
$var2 = $_POST['variable2'];
$errors = array();

if(!$var){ $errors[] = "Var one is missing!"; }
if(!$var2){ $errors[] = "Var two is missing!"; }

if(count($errors) > 0){
   foreach($errors AS $error){
   echo "<font color=\"red\">$error</font><br>\n";
   }
}else {
echo "both variables are set, you're ready for take off!\n";
}

Link to comment
Share on other sites

$var = $_POST['variable'];
$var2 = $_POST['variable2'];
$errors = array();

if(!$var){ $errors[] = "Var one is missing!"; }
if(!$var2){ $errors[] = "Var two is missing!"; }

if(count($errors) > 0){
   foreach($errors AS $error){
   echo "<font color=\"red\">$error</font><br>\n";
   }
}else {
echo "both variables are set, you're ready for take off!\n";
}

 

Can you explain how to use this snippet?

 

do I have to declare the variables? if so how?

Link to comment
Share on other sites

Just define the variables that correspond with your form field names.

 

$name = $_POST['form_field_name_one']; //etc....

 

To see if a value exists just implement the 5th and 6th of my example to fit your own.

 

If you have more than two just continue adding sets of line five and six to meet your requirements

Link to comment
Share on other sites

<?php
$self = $_SERVER['PHP_SELF'];

$query = $_POST['query'];

if (isset($query))
{
	// Do stuff with $query
	// Redirect somewhere when done.
}
else 
{
	echo "You must fill in the form.";
}
?>

<html>
<head>
<title>A form</title>
</head>

<body>

<form action="<?php echo $self; ?>" method="post">
	<input type="text" name="query" />
	<input type="submit">
</form>

</body>
</html>

Link to comment
Share on other sites

Then just remove line five or six and replace it with your own variable/error.

 

Using PHP_SELF can be a bad move since the user could implement his or her own code into the URL.

 

Can y'all explain this more?  How could a user put more code in the URL?  What's the best alternative for PHP_SELF?

Link to comment
Share on other sites

<?php
$self = $_SERVER['PHP_SELF'];

$query = $_POST['query'];

if (isset($query))
{
	// Do stuff with $query
	// Redirect somewhere when done.
}
else 
{
	echo "You must fill in the form.";
}
?>

<html>
<head>
<title>A form</title>
</head>

<body>

<form action="<?php echo $self; ?>" method="post">
	<input type="text" name="query" />
	<input type="submit">
</form>

</body>
</html>

 

the form is just an html doc it calls the script from the action parameter

Link to comment
Share on other sites

<?php
$self = $_SERVER['PHP_SELF'];

$query = $_POST['query'];

if (isset($query))
{
	// Do stuff with $query
	// Redirect somewhere when done.
}
else 
{
	echo "You must fill in the form.";
}
?>

<html>
<head>
<title>A form</title>
</head>

<body>

<form action="<?php echo $self; ?>" method="post">
	<input type="text" name="query" />
	<input type="submit">
</form>

</body>
</html>

 

the form is just an html doc it calls the script from the action parameter

 

Exactly.  You said you wanted it to show the form again if nothing was submitted.  This checks that and then redirects to wherever you want if the data is correct.  You should consider doing js data validation too...

Link to comment
Share on other sites

<?php
$self = $_SERVER['PHP_SELF'];

$query = $_POST['query'];

if (isset($query))
{
	// Do stuff with $query
	// Redirect somewhere when done.
}
else 
{
	echo "You must fill in the form.";
}
?>

<html>
<head>
<title>A form</title>
</head>

<body>

<form action="<?php echo $self; ?>" method="post">
	<input type="text" name="query" />
	<input type="submit">
</form>

</body>
</html>

 

the form is just an html doc it calls the script from the action parameter

 

Exactly.  You said you wanted it to show the form again if nothing was submitted.  This checks that and then redirects to wherever you want if the data is correct.  You should consider doing js data validation too...

 

so where does this code call the script?

Link to comment
Share on other sites

so where does this code call the script?

 

Eh, can you tell me exactly what you need?  I reread your original post and you say that you want it to display an error if nothing is entered but also append that error to the top of the form?  Not really sure what you need.

Link to comment
Share on other sites

so where does this code call the script?

 

Eh, can you tell me exactly what you need?  I reread your original post and you say that you want it to display an error if nothing is entered but also append that error to the top of the form?  Not really sure what you need.

 

I just want the script to return a message stating the form was submitted without any data...please click the back button and try again...etc

 

right now everything I have tried still pulls down all the records...some of the snippets I've tried print the error message but the script still pulls down all the records and some even when a valid query is entered it still prints the error message

Link to comment
Share on other sites

So all you would need to do to my code above is change the action to some other php file and put my php code in it, sans the $self line.

 

the (HTML) needs to be a different doc...otherwise the pagination menu/display is printed

 

That's what I said to do.  Put the php code in a separate file where the form points to in the action field.

Link to comment
Share on other sites

So all you would need to do to my code above is change the action to some other php file and put my php code in it, sans the $self line.

 

the (HTML) needs to be a different doc...otherwise the pagination menu/display is printed

 

That's what I said to do.  Put the php code in a separate file where the form points to in the action field.

 

It does not work... it still prints all the records

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.