Jump to content


Photo

Validating drop downs


  • Please log in to reply
13 replies to this topic

#1 Eskimo887

Eskimo887
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 22 July 2006 - 04:45 AM

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
value="<? echo $name; ?>"
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.

<? 
 // 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>"; 
      }
http://southerncross.../Untitled-1.php

#2 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 04:58 AM

well, for starters:

      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>"; 
      } 

can be condensed to:

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

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)
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#3 cunoodle2

cunoodle2
  • Members
  • PipPipPip
  • Advanced Member
  • 602 posts

Posted 22 July 2006 - 05:16 AM

Try this...

<?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";
	}

?>

If the code was working and now it's not, something changed. Did you change any code?

yeah, just a little bit, I am trying to start from beginning and trace my steps to see what i did wrong unless you see it.


#4 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 September 2010 - 03:17 PM

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.


Regular failure is also a form of reliability

#5 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 21 September 2010 - 04:34 PM

Off the top of my head . . .

<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>

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#6 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 September 2010 - 04:54 PM

Off the top of my head . . .

<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>



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)
Regular failure is also a form of reliability

#7 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 September 2010 - 05:12 PM

Correction:
Thanks Pikachu2000,

the first option "VISA" filled in was my mistake, but unfortunately it doesn't work anyway. The entries are not remembered.

(Once again if you try the form: For now there must be any entry in the message box. Otherwise error. I'll correct that later on)
Regular failure is also a form of reliability

#8 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 21 September 2010 - 05:16 PM

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

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

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#9 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 21 September 2010 - 06:02 PM

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...07.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 ?
Regular failure is also a form of reliability

#10 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 21 September 2010 - 08:23 PM

Sorry, I had a typo. Forgot parentheses in the last edit, should be if( !isset($_GET['payment']) || . . .

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#11 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 October 2010 - 06:13 AM

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.
Regular failure is also a form of reliability

#12 BlueSkyIS

BlueSkyIS
  • Members
  • PipPipPip
  • reallynotfakenews.com
  • 4,268 posts

Posted 09 October 2010 - 07:44 AM

you can't nest if's like that. the line should be more like:

if(!isset($data_start) && !isset($data_end) && empty($pax)) {

deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores and
Find Motorcycle/ATV Repair, Tires

#13 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 October 2010 - 09:02 AM

@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 ?
Regular failure is also a form of reliability

#14 keyboarder

keyboarder
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 October 2010 - 09:45 AM

BINGO ! I got it myself ! Thanks anyway ! :D
The string is now:
# arrival, departure given, but pax empty

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



Regular failure is also a form of reliability




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users