Conditional Form Action

I have a form that is completed by clicking “Submit” which then exits and executes the Form Action code.

But I would like to conduct some validation checks and return to the current screen if some parts are not properly completed.

I have built my validations into my form action code and have alert boxes going up to explain whats missing but I don’t know how to get back to the original screen.

Any help would be appreciated.

Mike

Please provide code to demonstrate what you are struggling with. No one is a mind reader here.

1 Like
 $lotno   = $_POST["lotno"];
 if ($lotno < "    a")
 {echo "<script>
 alert ('You have not entered a Lot Number at the top of the screen.  You must enter the lot umber for the person doing the pickup before you can submit your order.');</script>";
 redirect('Restaurant01t.php');
 return;
 }

Above is what I have tried to do already.

The Form Action causes the user to exit from the form (Restaurant01t.php) into another form that will do the downstream processing.

But if the person has not entered their lotno, we need to get back to the original form.

The code above does not work. It seems to simply continue on with the downstream processing.

Mike

Yes, that is precisely why it isn’t “working”. Because you seem to not understand the comparison operators.

< - The less than sign means just that.
It is used to compare 2 integers to see if the left integer is less than the right integer.

> - The greater than sign means just that.
It is used to compare 2 integers to see if the left integer is more than the right integer.

<= - The less than or equal to sign means just that.
It does exactly what the less than sign does, but it also checks to see if the integer is equal.

>= - The greater than or equal to sign means just that.
It does exactly what the great than sign does, but it also checks to see if the integer is equal.

= - The equal sign is for assigning.
It isn't supposed to be used for comparison, but people often confuse it with the double equal sign.

== - The double equal sign is for comparing the 2 values.
It is supposed to compare and see if the 2 values are equal or have an equal value.

This part is wrong because the variable on the left may not contain the values " a". The < again is for comparing 2 integers. " a" isn’t an integer. Thus, it will continue to execute without triggering the if statement.

This is also wrong. People need to understand the HTTP protocol before they start using redirect functions. Assuming you are using a framework that has that redirect function, you are using it wrong. And if you are assuming the redirect function for PHP is redirect(), you are also using it wrong. The actual redirect function is header(). And if you are using header() at the end of your script like how you are doing it in the code above, you are still using it wrong. Let me just give you a little insight on why it is wrong.

There are sets of rules that everyone has to follow. This isn’t some kind of fan made rule, this is actually a legit thing. So there’s something called the HTTP protocol. Let me try to make an analogy for you. So the HTTP protocol is pretty much like the law of the land. Everyone has to follow that law no matter what. If you disobey and break the law, the police will come after you. Now, the police in terms of PHP is pretty much the PHP engine. If you violate the HTTP protocol, you will get an error saying headers already sent. What this means is that you are outputting to the screen before you are sending any headers to the HTTP protocol. The law of the land is, that everything must be sent to the HTTP protocol before you can output anything to the screen. I’ll let you think a little bit on that.

This line is unnecessary. If you redirect a user, the return; line becomes redundant and is useless. The only time when you use return; is when you want to return values from a function and pass it to another function or if you want to use the returned value for something.

Please supply minimum, maximum and allowed characters for $lotno

A lot number can contain either numbers or characters and can be up to 4 characters long.

The “<” operator works because the code routes to the alert message properly when the user enters a blank lot number.

I am not clear as to what you are saying about the header. Do I actually write code in the header to look for a blank lot number?

It may be “working” because PHP does “type juggling”. It is something that could be used I suppose, but it is not anything I would recommend relying on. It makes code more difficult to read and can cause difficult to fix bugs. IMHO it is much better to explicitly test for what you’re testing for.

The “header” in terms of the header() function is for sending HTTP (HyperText Transfer Protocol) headers. When you open your dev tools you can look at both Request and Response headers. My rough definition is they are sort of a set of information about how the “body” should be dealt with. For example, content-type text/html says “interpret the bytes that follow as text/html”, while content-type image/png says “interpret the bytes that follow as a png image” etc.

Typically “text/html” headers are the default. So if they are not explicitly set, that is what they will be. A common “gotcha” is trying to send out HTTP headers after they have already been sent. This will not work. The Protocol is headers → body, not headers → some of the body content → headers → more body content.

Ok try a browser search for these conditions:

  1. “PHP Strip whitespace (or other characters) from the beginning and end of a string”
  2. “PHP Get string length”
  3. “PHP check for alphanumeric characters”

Read the PHP Free Manual solutions and especially the examples.

Afterwards rewrite your tests and validate the input $_POST string again and for the benefit of other users, please post your revised solution.

Like @Mittineague said. It’s only “working” because of type juggling. The comparison operators return a boolean value. From my understanding, the comparison operators will do something buggy like this and convert strings into a single 0. Using the below sample as a demo.

$var = 'Some random string';
$compareString = 'Another random string';

if($var < $compareString) {
    // Execute here
} else {
    // Do nothing
}

The results of $var and $compareString will be converted to 0. So this will be the result in layman’s terms.

if(0 < 0) {
    // Execute here
} else {
    // Do nothing
}

Since 0 will never be lower than 0, it will always return the boolean of true. Which means, it’ll always trigger your if statement no matter what.


In simple terms.

John:

My problem is not the check on whether the lot number is blank. That part is working correctly already.

The problem is redirecting the process back to the form that I exited (Restaurant01t) with the file action.

Mike

Try this to understand why the redirect is not working the way you want it to do:

http://php.net/manual/en/function.header.php

…and scroll down to comment #85 to find a solution to your specific problem.

1 Like

I have gone through the documentation and have found the solution to the redirect problem as below:

$lotno   = $_POST["lotno"];
if ($lotno < "    a")
{echo "<script>
alert ('You have not entered a Lot Number at the top of the screen.  You must enter the lot number for the person doing the pickup before you can submit your order.');</script>";
echo '<script type="text/javascript">
window.location = "http://mapleleafcomputerclub.com/Restaurant/Restaurant01H.php"
</script>';
}

This allows you to do error checking in the form action code and return to the original form if the entries don’t pass validation.

Mike

You are missing the point. What you are attempting is very flawed in every aspect. Sooner or later, this code of yours will break without telling you.

1 Like

Guess what will happen if users have Javascript turned off…

As @spaceshiptrooper has said, your approach is very flawed.

John:

Thank you for the link to the documentation.

I find that the redirect code as per section 85 works properly if there is no alert code ahead of it. But when the alert code is included, the alert is ignored. The operator simply comes back to the screen with no alert as to what he missed.

For some reason the script code does not interfere with the alert box and the user is returned to the original screen after clicking OK in the alert box.

I do appreciate the PHP Manual and will make greater use of it now that I have discovered it. For some reason Google does not seem to pick up on this.

Mike

Without seeing the modified script I cannot offer any suggestions.

I hope you understand that the script per section #85 will only work if there has been nothing previously rendered on the screen.

I think the script you mentioned in post #13 validates correct user input and cannot be used with the “section #85”.

Unfortunately it is either one script or the other but not both :frowning:

I am looking for a solution where I can put up an alert box before redirecting.

It would appear there is no solution for this. Even the PHP Manual says that the best they can do is a 5 second wait before the redirecting.

I will try to put the alert box under some other buttons that are internal to the form so I can get the alert in front of them before they press submit,

We cannot accept a form that has no lot number, it will not work for us.

It is unfortunate that there is no workaround for this.

Mike

Forget your attempted solution and using Javascript Alerts. Explain the real problem you are trying to solve and WE will tell you the solution.

Based on the form at the error URL it appears you have an online food order form with one or more required fields. If submitting a valid order is the real task at hand, this is a trivial problem to solve.

The form and Php should be in one page. Php at the top, html at the bottom. To keep the file clean, you can put the form in a separate “template” file and include it in the script
Suedo Code

<?php
$error = null;
if server request method = post{
// validate fields in php
if (empty required_field){
$error[] = 'Error message';
}

if ($error){
//display error array
}
else{
//process form
header redirect to this file or where ever

}

}
?>
HTML Form here or include call

Is it possible to test on the form for correct user input and use buttons instead of JavaScript?

if( $validInput ):
   echo <a href="goToWherEver.php">
     echo '<button> Where Ever </button> ';
  echo '</a>';
else:
   echo <a href="goToElseWhere.php">
     echo '<button> Else Where </button> ';
  echo '</a>';
endif;