user6014037
user6014037

Reputation:

Submit Button Action Depending If Field Matches Another Form

I have one page with two forms, but only one is shown for now. The idea being is if the consumer selects a certain state from the drop down menu, it will either redirect them to a thank you page or the second form when they click on the submit button.

Here is my form:

<form action="capture_redirect.php" method="post">
<select placeholder="State" name="_State" id="state" required/>
<input type="submit" id="submit" value="Check Eligibility">

Javascript:

<script type="text/javascript">
function submit   {
if(document.getElementById(state) = 'AZ', 'AR', 'CA')
   {
    location.href = "home.php";
   }
else
{
    location.href = "capture_redirect.php";
   }
}
</script>

I'm sure I made an error somewhere here but I can't seem to figure it out. Right now when the consumer clicks on the submit button it either redirects them to the thank you page or back on the form.

I have seen examples where each indivual state is displayed with it's own if/else statements, but I'm hoping to avoid that since it would make my document much longer.

Upvotes: 1

Views: 83

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

1.= is used for assignment not for comparison so use ==instead of it.

2.Also break your condition into three parts like below.

3.You forgot ''also, around state in your condition:-

if(document.getElementById('state').value == 'AZ' || document.getElementById('state').value == 'AR' || document.getElementById('state').value == 'CA'){.......}else{.....}

Or more efficient way to do it:-

if( ['AZ', 'AR', 'CA'].indexOf( document.getElementById('state').value) )

4.Instead of location.href use window.location.href

Upvotes: 0

rrk
rrk

Reputation: 15846

Use value and array for this.

if( ['AZ', 'AR', 'CA'].indexOf( document.getElementById('state').value) )

Or in jQuery you can use jQuery.inArray().

if( $.inArray($('#state').val(), ['AZ', 'AR', 'CA'] ) )

Upvotes: 1

Related Questions