Jump to content

Redirect if form empty?


rocky48

Recommended Posts

Hi

 

I am trying to check whether all the form has been filled in in a questionaire on my site.

 

If any entry has not been made I want it to re-present the script for the form again.

I have tried this, but it runs the form whatever the state.

<?php session_start();
$_SESSION['Test']=$_POST['VID'];
print_r ($_POST);
$mail = $_POST['email'];
$Vrate = $_POST['rate'];
$Verse_imp = $_POST['Vimp'];
$site_sug = $_POST['site'];
$Site_imp = $_POST['Simp'];

if (empty($email)) {
    echo '$mail is either 0, empty, or not set at all';
    require('questionnaire.html');

}
if (empty($Vrate))
{
    echo '$Vrate is either 0, empty, or not set at all';
     require('questionnaire.html');
}
if (empty($Verse_imp)){
    echo '$Verse_imp is either 0, empty, or not set at all';
     require('questionnaire.html');
}
if (empty($site_sug))
{
    echo '$site_sug is either 0, empty, or not set at all';
     require('questionnaire.html');
}
if (empty($Site_imp))
{
    echo '$Site_imp is either 0, empty, or not set at all';
     require('questionnaire.html');
}
?>

This is unfinished code thats why I have left the echos in.  Before I add the require it detected the empty line OK.

 

Any suggestions?

Link to comment
Share on other sites

Hi

 

Here is the html form:

<?php session_start();
$_SESSION['Test']=$_POST['VID'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Yosemite  
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20091106

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>1066 Cards 4U - Questionnaire</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper"><!--Start Wrapper-->
<div id="menu"><!--Start Menu-->
		<ul>
			<li class="current_page_item"><a href="index.php">Home</a></li>
			<li><a href="Links.html">Links</a></li>
			<li><a href="Verse_Menu.html">Verses</a></li>
			<li><a href="Techniques.html">Techniques</a></li>
			<li><a href="News.html">News</a></li>
			<li><a href="Gallery.html">Gallery</a></li>
			<li><a href="contact.html">Contact</a></li>
			<li><a href="stats1.html">Stats</a></li>
		</ul>
</div><!-- end menu -->
<div id="header"><!--Start Header-->
		<div id="logo">
			<h1><a href="http://www.1066cards4u.co.uk">1066 Cards 4U</a></h1>
</div><!-- end wrapper -->
</div><!-- end header -->
<div id="page"><!--Start Page-->
<div id="page-bgtop"><!--Start Page-bgtop-->
<div id="page-bgbtm"><!--Start Page-bgbtm-->
<div id="content"><!--Start Content-->
		 <h1>Feedback Questionnaire</h1>
<p>We are sorry that we have had to resort to this method of getting feedback, but users seem reluctant to give any feedback.  We can tell from our stats that there is a very strong contingent in California, USA and we would like to get your opinion of our site and the FREE verse utility.  We would also value your opinion elsewhere in the world.<br />
All questions must be answered before you proceed to the normal input form.
</p> 
</br>

	<p>
<form NAME=feedback action="Qstore.php" method="post">
<strong>Your email address:</strong> <input type="email" size="20" name="email"/><br/><br/>
Please rate the Verse Utility</br>
<input type="checkbox" name="rate" value="1"> 1
<input type="checkbox" name="rate" value="2"> 2
<input type="checkbox" name="rate" value="3"> 3
<input type="checkbox" name="rate" value="4"> 4
<input type="checkbox" name="rate" value="5"> 5	<br/><br/>	
<strong>Are there any ways we can improve the utility?:</strong> <br/><textarea name="Vimp" rows="5" cols="30"></textarea><br/><br/>
<strong>What do you think of the site as a whole?:</strong> <br/><textarea name="site" rows="5" cols="30"></textarea><br/><br/>
<strong>How do you think we can improve the site?:</strong> <br/><textarea name="Simp" rows="5" cols="30"></textarea><br/><br/>
<strong>Any other comments?:</strong> <br/><textarea name="Simp" rows="5" cols="30"></textarea><br/><br/>
<input type="submit" value="Submit"/>
</p>
</div><!-- end content -->

<div id="sidebar"><!--Start Sidebar-->
			
					<div id="search" <!--Start Search-->
					<form method="get" action="http://1066cards4u.co.uk">
						<div><!--Start Submit-->
							<input type="text" name="s" id="search-text" value="" />
							<input type="submit" id="search-submit" value="GO" />
						</div><!-- end submit -->
					</form>
					</div><!-- end Search -->
					<div style="clear: both;"> </div>
				
				
		</div><!-- end #sidebar -->
		<div style="clear: both;"> </div>
	</div><!-- end page-bgtop -->
	</div><!-- end page-bgbtm -->
	</div><!-- end page -->
</div>

<div id="footer"><!--Start Footer-->
		<p>Copyright (c) 2008 Sitename.com. All rights reserved. Design by <a href="http://www.freecsstemplates.org/" rel="nofollow">FreeCSSTemplates.org</a>.</p>
</div><!-- end footer -->
</body>
</html>
		

Also, when this form has been filled in correctly I want to proceed to another php script.

What I am doing is trying to force users to fill the questionnaire, as I have slipped this in between a set of scripts.

Link to comment
Share on other sites

which one are you trying to use? Neither of those two forms do not fitt to be checked from your php script. What result you're getting by calling - print_r ($_POST); in the third line of your script?

Link to comment
Share on other sites

You've called your variable $mail, but you're checking for $email.

 

Besides that, this is all a bit weird. What's the point of the empty() checks? The variables obviously do exist, because you've defined them right above this part. So why not just check for !$your_variable?

 

Or rather: Why not leave out all the superfluous variables and use the $_POST array directly? This makes your code much shorter and will also prevent warnings if the POST parameters do not exist at all.

 

There are also issues with your HTML. You're missing the closing tag of the form, and you cannot have multiple checkboxes with the same name (well, this is actually a PHP issue). If you want an array of values, you need the bracket syntax: rate[].

Link to comment
Share on other sites

Ops....it was my fault the form exists :)  There is no closing form tag to the first form. You cannot use nested forms in one html document.  

EDIT: Jacques1 is alredy answered, sorry for the double reply :)

Link to comment
Share on other sites

I admit to the first mistake and the missing form tag, but I thought empty checks whether the variable has a value.

If you don't fill this value in the form this would surely be a null value?

I agree with removing the variables and using the POSTed ones.

 

Apart from picking holes in my coding, nobody has attempted to answer my question!!

Link to comment
Share on other sites

I admit to the first mistake and the missing form tag, but I thought empty checks whether the variable has a value.

 

To be exact, it yields true if the variable doesn't exist, or if it has a falsy value. So empty($var) is the same as !isset($var) || !$var

 

In your code above, you know for sure that the variable exists, so you can jump straight to !$var. There's simply no need for empty(). I'm not saying that it's wrong, but it's overly complicated and makes no sense from a technical perspective.

 

Also be aware that the string "0" is considered falsy as well. This can lead to unintended behaviour, but it should be OK in your case.

Link to comment
Share on other sites

 

Apart from picking holes in my coding, nobody has attempted to answer my question!!

Jacques1 - I rerally don't understand what you are getting at?  How else am I going to check if the user has completely filled in the form (Apartr from Isset())?

 

I have corrected the form now as you have pointed out the mistakes.

 

How about helping by giving me some clues to solve the orginal question?  - How do you return to the form if any of the fields has not been filled in?

Here is the php code:

<?php session_start();
$_SESSION['Test']=$_POST['VID'];
print_r ($_POST);
$mail = $_POST['email'];
$Vrate = $_POST['rate'];
$Verse_imp = $_POST['Vimp'];
$site_sug = $_POST['site'];
$Site_imp = $_POST['Simp'];


if (empty($mail)) {
    echo '$mail is either 0, empty, or not set at all';
}
elseif (require('VInput.php'));

if  (empty($Vrate))
{
    echo '$Vrate is either 0, empty, or not set at all';
    }
elseif (require('VInput.php'));

if (empty($Verse_imp))
{
    echo '$Verse_imp is either 0, empty, or not set at all';
    }
elseif (require('VInput.php'));

if (empty($site_sug))
{
    echo '$site_sug is either 0, empty, or not set at all';
    }
elseif (require('VInput.php'));

if (empty($Site_imp))
{
    echo '$Site_imp is either 0, empty, or not set at all';
    }
elseif (require('VInput.php'));
?>

I have since added the elseif to make it run the next code if the form has been completely filled in.

 

Here is the form:

<div id="content"><!--Start Content-->
		 <h1>Feedback Questionnaire</h1>
<p>We are sorry that we have had to resort to this method of getting feedback, but users seem reluctant to give any feedback.  We can tell from our stats that there is a very strong contingent in California, USA and we would like to get your opinion of our site and the FREE verse utility.  We would also value your opinion elsewhere in the world.<br />
All questions must be answered before you proceed to the normal input form.
</p> 
</br>

	<p>
<form NAME=feedback action="Qstore.php" method="post">
<strong>Your email address:</strong> <input type="email" size="20" name="email"/><br/><br/>
Please rate the Verse Utility</br>
<input type="radio" name="rate" value="1"> 1
<input type="radio" name="rate" value="2"> 2
<input type="radio" name="rate" value="3"> 3
<input type="radio" name="rate" value="4"> 4
<input type="radio" name="rate" value="5"> 5	<br/><br/>	
<strong>Are there any ways we can improve the utility?:</strong> <br/><textarea name="Vimp" rows="5" cols="30"></textarea><br/><br/>
<strong>What do you think of the site as a whole?:</strong> <br/><textarea name="site" rows="5" cols="30"></textarea><br/><br/>
<strong>How do you think we can improve the site?:</strong> <br/><textarea name="Simp" rows="5" cols="30"></textarea><br/><br/>
<strong>Any other comments?:</strong> <br/><textarea name="Simp" rows="5" cols="30"></textarea><br/><br/>
<input type="submit" value="Submit"/>
</form>
</p>
</div><!-- end content -->

I have changed the checkboxes to radio buttons as |I only want one checked.

 

Please help.

Link to comment
Share on other sites

 

Jacques1 - I rerally don't understand what you are getting at?  How else am I going to check if the user has completely filled in the form (Apartr from Isset())?

 

I understand that you want to check if the fields are empty, and this is perfectly fine. The problem is that you don't understand how empty() works, which is why I'm trying to explain it.

 

But of course we can leave it at that if you don't care.

 

 

 

How about helping by giving me some clues to solve the orginal question?  - How do you return to the form if any of the fields has not been filled in?

 

You don't need to. Make a single script which displays the form and processes all form submissions. If the input is wrong, the user stays on the same page and sees the form again. If everything is fine, you redirect the user to some other page.

 

This solves two problems at the same time: You get your form, and the user won't accidentally submit the form data again by refreshing the page (see the POST/redirect/GET pattern):

<?php

// the form processing logic
$field_empty = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (empty($_POST['test_field']))
    {
        $field_empty = true;
    }
    else
    {
        // The input is fine. Save it and then redirect the user
        header('Location: https://yoursite.com/success.php');
        exit;
    }
}
?>
<?php if ($field_empty): ?>
    <p>You did not fill out the form.</p>
<?php endif; ?>
<!-- Your form -->
<form method="post">
    <label for="test-field">Please fill out the field.</label>
    <input id="test-field" type="text" name="test_field">
    <input type="submit">
</form>
Link to comment
Share on other sites

Use the header() php function with some get parameter to it if you wat to redirect the validated script to the basic form in case it failed.

 

Without going into details about validation of client's email.

 

Qstore.php

if (empty($mail)) {
   header('Location: form.php?action=error'); exit;
}

form.php

<?php if(isset($_GET['action']) && $_GET['action'] == 'error') echo '<p style=color:red>your email is either 0, empty, or not set at all</p>';?>
<form NAME=feedback action="Qstore.php" method="post">
<strong>Your email address:</strong> <input type="email" size="20" name="email"/><br/><br/>
Please rate the Verse Utility</br>
<input type="radio" name="rate" value="1"> 1
<input type="radio" name="rate" value="2"> 2
<input type="radio" name="rate" value="3"> 3
<input type="radio" name="rate" value="4"> 4
<input type="radio" name="rate" value="5"> 5	<br/><br/>	
<strong>Are there any ways we can improve the utility?:</strong> <br/><textarea name="Vimp" rows="5" cols="30"></textarea><br/><br/>
<strong>What do you think of the site as a whole?:</strong> <br/><textarea name="site" rows="5" cols="30"></textarea><br/><br/>
<strong>How do you think we can improve the site?:</strong> <br/><textarea name="Simp" rows="5" cols="30"></textarea><br/><br/>
<strong>Any other comments?:</strong> <br/><textarea name="Simp" rows="5" cols="30"></textarea><br/><br/>
<input type="submit" value="Submit"/>
</form>

You should go to the source (php.net) to learn how to validate user's email address.

Link to comment
Share on other sites

Use the header() php function with some get parameter to it if you wat to redirect the validated script to the basic form in case it failed.

 

That's not really a good idea, because then you have to save all user input in the session so that you can restore it on the form page. And you again run into the refresh issue.

 

I would do it the other way round as explained above: You do the processing on the form page. And then you redirect on success.

Link to comment
Share on other sites

 

That's not really a good idea, because then you have to save all user input in the session so that you can restore it on the form page.

 

Well, personally I am using javascript to do this and return only true or false in the server side if the test passed...I don't like to waste the server resources with stupid things :) 

Link to comment
Share on other sites

Hi Jazzman1

 

Found this Js Function code on the internet, but was only evalueating one field.

 

Have I got the sytax right for multiple fields?

function validateForm()
{
var a=document.forms["feedback"]["email"].value;
if (a==null || a=="")
  {
  alert("Email address must be entered");
  return false;
  }
var b=document.forms["feedback"]["rate"].value;
if (b==null || b=="")
  {
  alert("A Site Rating must be entered");
  return false;
  }
var c=document.forms["feedback"]["Vimp"].value;
if (c==null || c=="")
  {
  alert("Your Verses Utility improvement comments must be entered");
  return false;
  }
var d=document.forms["feedback"]["site"].value;
if (d==null || d=="")
  {
  alert("Your site comments must be entered");
  return false;
  }
var e=document.forms["feedback"]["Simp"].value;
if (e==null || e=="")
  {
  alert("Your site improvement comments must be entered");
  return false;
  }
var f=document.forms["feedback"]["other"].value;
if (f==null || f=="")
  {
  alert("Your other comments must be entered");
  return false;
  }   
}

To use it according to the snippet you add ' onsubmit="return validateForm()" ' to the form.

 

It did not work, just went to the action file, with no data entered into the form.

Link to comment
Share on other sites

Hi Jazzman

 

Forgot <script> tag.

 

It seemed to work, but when it got to the site rating radio buttons it would not accept that one had been selected.

Do I need different code for this type of input?

Link to comment
Share on other sites

@rocky48, there is nothing wrong to use PRG pattern as shown in my example. The whole point of using it, is to prevent the same data to be sent to the server multiple times by reloading the form page. For more information - http://web.archive.org/web/20061211082049/http://www.theserverside.com/patterns/thread.tss?thread_id=20936

 

I prefer to use javascript because this increases the speed of the application if the validation somehow failed, however you don't have to trust it because javascript could be easily disabled from some malicious user. You must be sure that the user input fields validation exists on the server side as well.

 

For a newbie in javascript i recommend you of using jquery validation plugin - http://jqueryvalidation.org/documentation/

Link to comment
Share on other sites

Thanks again jazzman1!

 

I am now trying to learn jQuery.  It's very powerful scripting language.

The solution:

<script>
$( "#questionnaire" ).validate({
  rules: {
    rate: {
      required: true
    },
     Vimp: {
      required: true
    },
     email: {
      required: true,
      email: true
    },
     site: {
      required: true
    },
     Simp: {
      required: true
    },
     other: {
      required: true
    }
  }
});
</script>
Link to comment
Share on other sites

HTML natively provides validation attributes:

<input type="email" name="email" required>

And, unlike jQuery validation, this doesn't require JavaScript. The only reason for using jQuery would be if your users primarily run around with old versions of Internet Explorer.

 

I also don't see how client-side validation solves your original problem. But if you're happy, I guess we can leave it at that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.