Jump to content

Validating drop downs


Eskimo887

Recommended Posts

A form on my website uses several drop down boxes.

One to select an id number from a list retrived by a query.

The others to get a start and stop time.  Using two for each, one for hours, one for minutes.

I already have some validation code which works fine, however, I can't seem to be able to use > or < mathematical signs with the times (ie, the start time must be < than the stop time).

Also, I have [code]value="<? echo $name; ?>"[/code] in each of the fields to re-insert the data as it gets erased if the form isn't correct.  How do I do the same for the drop downs?

I hope this makes sense.

[code]
<?
// only validate form when form is submitted
if(isset($Submit)){
      $error_msg='';
      if(trim($starttime)==(trim($stopttime))) { //drop down
          $error_msg.="starttime cannot equal stop time.<br>";
      }
      if(trim($starttime)>(trim($stopttime))) {  //drop down
          $error_msg.="starttime cannot be greater than stop time.<br>";
      }
      if(trim($startfuel)<(trim($stopfuel))) { //text field
          $error_msg.="Departure fuel cannot be less than arrival fuel<br>";
      }
      if(trim($id)=="sca") { //query powered drop down (this one works, but doesn't re-select the option
          $error_msg.="Please select the id number.<br>";
      }
[/code]
[url=http://southerncrossairlines.ausvirtual.com/New%20Site/Complete/validation/Untitled-1.php]http://southerncrossairlines.ausvirtual.com/New%20Site/Complete/validation/Untitled-1.php[/url]
Link to comment
Share on other sites

well, for starters:

[code]
      if(trim($starttime)==(trim($stopttime))) { //drop down
          $error_msg.="starttime cannot equal stop time.<br>";
      }
      if(trim($starttime)>(trim($stopttime))) {  //drop down
          $error_msg.="starttime cannot be greater than stop time.<br>";
      }
[/code]

can be condensed to:

[code]
      if (intval($starttime) >= intval($stopttime) {  //drop down
          $error_msg.="More generic error message.<br>";
      }
[/code]

Another thing:

Do _NOT_ use register globals. use the $_POST, $_GET, or $_REQUEST superglobals.

As for defaulty selecting a value from a dropdown list:
Isnt it just a matter of adding the attribute 'selected'? (may be wrong about the name. im more of a text-entry kinda guy ;) if im wrong, reffer to the HTML 4 Spec at www.w3c.org)
Link to comment
Share on other sites

Try this...

[code]<?php

//set $error_msg to NULL as this will be used later to see if there are any errors
$error_msg = NULL;

//first retrieve values from the form
$departtimeh = isSet($_POST['departtimeh']) ? trim($_POST['departtimeh']) : NULL;
$departtimem = isSet($_POST['departtimem']) ? trim$_POST['departtimem']) : NULL;
$arrivetimeh = isSet($_POST['arrivetimeh']) ? trim$_POST['arrivetimeh']) : NULL;
$arrivetimem = isSet($_POST['arrivetimem']) ? trim$_POST['arrivetimem']) : NULL;

//first check to see if the hours are the same OR if arrive is less as those are the only 2 that would produce errors
if($arrivetimeh >= $departtimeh)
{

$error_msg = "starttime cannot be greater than or same as stop time.<br/>";

//if they are the same hour also check the minutes
if($arrivetimeh == $departtimeh && $arrivetimem <= $departtimem)
{
$error_msg = "starttime cannot be greater than or same as stop time.<br/>";
}

//otherwise everything checks out and things are fine
else
{
$error_msg = NULL;
}
}

//check to see if error message exists.  If so then do not proceed
if ($error_msg != "")
{
echo $error_msg;
}

else
{
echo "Everything is fine..proceed";
}

?>[/code]
Link to comment
Share on other sites

  • 4 years later...
I don't know where else to put this:

Issue:

When submitted with the wrong verification code, all data entered into my online form was lost (blanked out) and the form returned correctly with message "wrong verification code", However, going through this great forum I managed to get all - manually entered - data returned back with same content ! Great ! I placed 
value="<?php echo $_GET['the_field_name'];?>"/  after each input field.

BUT... not so with input fields entered from drop-down menu ! Now my question is:
How do I put a similar string for the drop-down menu field "Payment by" in this sample:

<tr>
<td class="table-inquire" width="47%">
<font face="Verdana" size="1" color="#000042">
Payment by:</font></td>
<td class="table-inquire" width="51%" colspan="2">
<font color="#400000" face="Verdana">
<select name="payment" size="1">
<option value="VISA">VISA</option>
<option value="MASTER">MASTER</option>
<option value="CASH">CASH</option>
<option value="T/T Banktransfer">T/T Banktransfer</option>
<option selected>Please select</option>
</select></font><font size="2" color="#400000" face="Verdana"></font></td>
</tr>

Same counts for the message field. It's gone also....
Can anyone advise please, what I have to specify, in order to keep the message field preserved in this sample:

<font face="Verdana" size="2" color="#000042"><br>
Further comments:<br></font
<font color="#400000" face="Verdana">
<textarea rows="5" name="message" cols="60"></textarea>

Any advise greatly appreciated. Thanks.

Link to comment
Share on other sites

Off the top of my head . . .

[code]
<option value="VISA" <?php echo $_GET['payment'] == 'VISA' ? 'selected="selected"' : ''; ?>>VISA</option>
<option value="MASTER" <?php echo $_GET['payment'] == 'MASTER' ? 'selected="selected"' : ''; ?>>MASTER</option>
<option value="CASH" <?php echo $_GET['payment'] == 'CASH' ? 'selected="selected"' : ''; ?>>CASH</option>
<option value="T/T Banktransfer" <?php echo $_GET['payment'] == 'Banktransfer' ? 'selected="selected"' : ''; ?>>T/T Banktransfer</option>
[/code]
Link to comment
Share on other sites

[quote author=Pikachu2000 link=topic=101449.msg1467079#msg1467079 date=1285086870]
Off the top of my head . . .

[code]
<option value="VISA" <?php echo $_GET['payment'] == 'VISA' ? 'selected="selected"' : ''; ?>>VISA</option>
<option value="MASTER" <?php echo $_GET['payment'] == 'MASTER' ? 'selected="selected"' : ''; ?>>MASTER</option>
<option value="CASH" <?php echo $_GET['payment'] == 'CASH' ? 'selected="selected"' : ''; ?>>CASH</option>
<option value="T/T Banktransfer" <?php echo $_GET['payment'] == 'Banktransfer' ? 'selected="selected"' : ''; ?>>T/T Banktransfer</option>
[/code]
[/quote]


Thanks Pikachu2000,

but unfortunately it doesn't work. Instead the form comes now with the first option "VISA" filled in. If you want to try, there's a test at - www.phuket-beachvillas.com/contact/index.php

(For now there must be any entry in the message box. Otherwise error. I'll correct that later on)
Link to comment
Share on other sites

Didn't realize this was a tag-along to a 4-1/2 year old thread. In the future, you're better off to start a new thread to ask a question.

Anyhow, I missed the last <option> tag in that list. It's coded to have the "selected" in it regardless of anything else, and since it appears last, it's overriding everything else. It needs to be changed to

[code]
<option value="" <?php if( !isset$_GET['payment'] || empty($_GET['payment']) ) { echo 'selected="selected"'; } ?>>Please select</option>
[/code]
Link to comment
Share on other sites

OOOPS... sorry to bother you again, but it resolves in a parse error. My code is now:


<option value="VISA" <?php echo $_GET['payment'] == 'VISA' ? 'selected="selected"' : ''; ?>>VISA</option>
<option value="MASTER" <?php echo $_GET['payment'] == 'MASTER' ? 'selected="selected"' : ''; ?>>MASTER</option>
<option value="CASH" <?php echo $_GET['payment'] == 'CASH' ? 'selected="selected"' : ''; ?>>CASH</option>
<option value="T/T Banktransfer" <?php echo $_GET['payment'] == 'Banktransfer' ? 'selected="selected"' : ''; ?>>T/T Banktransfer</option>
<option value="" <?php if( !isset$_GET['payment'] || empty($_GET['payment']) ) { echo 'selected="selected"'; } ?>>Please select</option>

I'm simply not "pro" enough to solve this issue, but I've done a lot of studies (and copies) to get my form (URL above) perfect and I would hate to give up... (guess you know what I mean ?).

BTW.: Yes, better to start a new thread and I did (http://www.phpfreaks.com/forums/index.php/topic,310607.new.html#new).
But I saw how active you are and read your qualified past posts and therefore directed this issue to you once more...

Hope not to bother you too much ?
Link to comment
Share on other sites

  • 3 weeks later...
OK, no problem anymore. The Form works fine now and I would like to add on one more issue, but the code below resolves is a parse error at the last "if"-line. (I,m still learning...)
Scenario:

I have 3 fields: "arrival", "departure" and "no. of persons" (pax) to check.
When arrival (date_start) is entered and departure (date_end) is entered, there should be an entry in the field "pax" as well. If not, there should be a message: "please tell us the number of persons in your group". The code is currently:

# arrival, departure and comments empty (works fine):

} if(empty($data_start) && empty($data_end) && empty($comment)){
exit("You have not specified any details to submit to us."); }

# arrival, departure given, but pax empty (resolves in parse error):

if(!isset($data_start) && if(!isset($data_end)) && if(empty($pax)){
exit("Please tell us the number of persons in your group."); }

Error:
Parse error: syntax error, unexpected T_IF in /home/xxxx/xxxx (last line).

Can someone please check and tell me what's wrong with the last string ? Thanks to anyone for any hint.
Link to comment
Share on other sites

@BlueSkyIS:) Thanks ! That resolves the parse error, but unfortunately not the issue. The script does not realize that the field "pax" is left blank.
Ideally that string should read: If "data_start" not blank AND "data-end" not blank, BUT "pax" is blank,
then send error message.
For test purpose: www.phuket-beachvillas.com/contact-it/contact-it.php
Maybe you have any clue ?
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.