Conditional Form Action

Is this true? I always thought that PHP would use alphanumeric comparisons in that situation and the documentation seems to agree. What method would you use to compare strings then?

This test code

<?php

$var = "Some string";
$comp = "Another string";

var_dump($var);
var_dump($comp);

if ($var < $comp) { 
  echo "lower";
  }
else 
  {
  echo "Higher";
  }

?>

produces “Higher”, as I would expect, because the string starting with “S” is higher alphabetically than one starting with “A”. If I change the comparison line to read

if ($comp < $var) { 

then I get “lower”, again as I would expect for the opposite reason. I can’t equate that in my head to PHP translating strings to be an integer value of 0. Sure, for doing calculations I would expect that. But not for comparisons.

You could use some client-side JavaScript (if your client browsers have it enabled) and only make the “Submit” button active once the lot number field has been filled in - in fact you could go a step further and use it to perform a validation check if there is one. If they have JS disabled, though, it won’t work and you have to consider that unless you’re in control of their environment.

In that case, though, you could do it the “old” way and have your processing code do all the validation that it must do anyway, and just re-draw the form, with all the valid user inputs, and show them which box they missed out - you can easily do that because you’ll be re-drawing the form in PHP and can pass error flags and so on.

It’s not as “nice” or modern to throw it back, but you have to consider it.

1 Like

Far better to have something working and it can always be updated later.

I believe so. At least in loose comparisons

It seems that PHP makes effort to get a number out of the string and use that, but the last “else” is to return a zero.

For example
"2peas" < "3pod" would be pruned to 2 < 3

http://php.net/manual/en/language.types.string.php#language.types.string.conversion

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).

1 Like

Assuming type juggling uses the same mechanisms like type casting, it’ll convert a complete string to a single 0. If the string leads with numbers, it’ll strip everything out except for the leading numbers. If it ends with numbers, but begins with strings, it’ll convert the complete string to a single 0. I have tested type casting before and it seems to be doing this. So I can only assume that when PHP type juggles, it’ll do the same.

And yet, that doesn’t seem to be what’s happening in that short bit of code I posted, it appears to be doing an alphabetic comparison. If I add digits on the end of the strings, it doesn’t alter the outcome.

If my comparison strings are “2peas” and “2pets”, then it correctly determines that the first is lower than the second, whichever way around I put the test. It just seems to me as if it’s doing a character-by-character comparison.

The documentation page for these comparison operators specifically doesn’t say that it does type juggling for <, >, <= or >=.

The key thing to me in that link to php.net is the phrase "When a string is evaluated in a numeric context, the resulting value and type are determined as follows. " - I don’t think they’re being used in a numeric context here. All the examples show strings having numbers added or subtracted, not simply compared with other strings that may or may not have numbers in them.

:face_with_hand_over_mouth: I’ll have to go back and test it out.

You idea of disabling the Submit button until the LotNo is entered is a good idea.

I could have a “Validate” button in place of the “Submit” button until the LotNo is entered. Then I could make the “Submit” button visible and make the “Validate” button invisible. The “Validate” button would work off a Function that would be part of the form,

I have not quite got my mind around Server Side versus Client Side. Anyway, I am assuming the form itself is Server side so the Java Script language will be available.

Mike

You could, but I’d just do it on “blur” from any of the required fields, or even just call the validation function when submit is pressed, then have the end of the validation function do the form submit. If it just needs to have something in it, though, can’t you do that with the “required” flag in HTML5?

The form is displayed on the client side, but processed (when you do the “submit”) on the server side. JavaScript is a client-side language, so it’s up to the client whether their browser enables it or not. If you rely on JS to do validation (and submission) and they turn it off, nothing will happen, hence you have to validate on the server as well. That’s why you would use JS to disable the submit button rather than drawing it disabled and relying on JS to enable it - if JS is disabled the user won’t be able to send the form.

The format of our lot numbers is 456a i.e. its a number followed by a character. Most times there is no character but in a few exceptional cases there are characters at the end.

If it would make people happier I could left trim whatever the user entered and check for " " where " " is 3 spaces.

Mike

Why are you checking for spaces anyway?

What would you suggest?

All good ideas. I like the idea of the disabled Submit button which indicates that something is wrong. The Validate button will give them a chance to see what is missing, then they will be able to complete the lot number. When they leave the Lot Number field, the Validate button will become invisible and the Submit button will be enabled.

I will work on this and provide a link so people can see and comment my solution.

I should mention that the form has a Total button that does a raft of calculations to total the order. This is all done with JavaScript and I don’t see how the values could be put back on the screen without JavaScript.

I don’t know how common JavaScript is on people’s computers. This is a retirement community where there are a lot of hand held smart phones.

Mike

It’d be done by having the next stage of the code, the submit destination, do the calculation and display a confirmation page prior to order.

If you have a very controlled set of users, you might be able to rely on them having JS enabled. In a general usage situation it’s wise to be able to deal with what happens if they do not. As you’re already relying on it to do your totalling, then maybe you should get it working with that assumption, then looking at adding the code to support a non-JS environment if you think you need it.

I have come up with a solution to get the validation within the form so I don’t have to depend on going to form action and redirecting with the controversial script code.

The answer is to put the Validation Code into the onmouseover event of the submit button. This works amazingly well because the submit button is not clickable when there is an alert message up. The function itself requires JavaScript. There are 1100 homes in our retirement community and we can send everyone a notice about downloading javascript onto their computers. Probably one third have smart phones, so they may not have java script at all.

Anyway, the Validation code looks like this.

function Validate1 () {
var nopizzas = document.getElementById("nopizzas1").value;
var nobrats = document.getElementById("nobrats1").value;
var nofrch = document.getElementById("nofrchicken1").value;
var nofrfr = document.getElementById("nofrfries1").value;
var nototal = nopizzas + nobrats + nofrch + nofrfr;
var vlotno = document.getElementById("lotno").value;
if (vlotno < "   a"){
    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.');
 }
 else if  (nototal < 1)
{
alert ('You have not entered any quantities for pizza, brats, fried chicken or french fries.  You must enter some food quantities before you can submit your order.');
}
}

This code checks for 2 things: a) have you entered your lot number and b) have you entered any food quantities. If both validations pass, then the Submit button is available for clicking.

This appears as a very good solution. You can see how it works at http://mapleleafcomputerclub.com/Restaurant/Restaurant01H.php

Mike

Good that it’s doing what you need it to.

I’m still not sure why you compare the lot number to " a" just to see if it’s empty. Why not just do

if (vlotno.trim() == "") { 
1 Like

Since this is a matter of Javascript at this point, I am moving this thread to the Javascript category. It makes no sense to me to keep this thread in the PHP category if PHP isn’t being used.

1 Like

Thankyou. I will make that change.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.