Jump to content

Select Button Validation


ainoy31

Recommended Posts

I have a drop down menu where my client makes a selection and then click in the select button.  The info is pulled from a database.  What is the best way to validate that the user chooses a record and then clicks on the select button.  I do not want user to just click on the select button without making a selection in the drop down menu.  Hopefully this is not confusing.  I am using PHP.

 

Much appreciation.

 

AM

Link to comment
https://forums.phpfreaks.com/topic/53740-select-button-validation/
Share on other sites

JavaScript was designed for this kind of thing.  Write a JS script to check that the user has selected something, failing to submit unless a selection has been made.  Of course, users can turn off JavaScript or use a browser without it, so you'll also need to check for submitted data in PHP.  Just check that $_POST['your_select_name'] is set to some (legal) value.  If no value is set, or value is out of range or illegal, redisplay the page with an error message prompting the user to choose something, and include the form again.

This is what I have:

 

<script text = "text\javascript" language = "javascript">

<!--

 

function SelectCustomer()

{

if(document.PendingCustomerInfo.Select.value)

{

alert("Please select a customer.");

return false;

}

return true;

}

//-->

</script>

 

Do I set the value to something?

You should be using a not (!) condition (or reverse your conditions).

if (! document.PendingCustomerInfo.select.value) {
alert("Please select a customer.");
return false;
} else { return true; }

// Or

if (document.PendingCustomerInfo.select.value) { return true; }
else { alert("Please select a customer."); return false;}

 

What's the onSubmit code in your form?

 

It should be something like:

 

<form .... onSubmit="return SelectCustomer()">

 

Also, you're supposed to use the "/" slash in "text/javascript."

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.