Forum Moderators: coopster

Message Too Old, No Replies

Form Validation - How can I do this?

         

glamdring

9:23 am on Jun 11, 2004 (gmt 0)

10+ Year Member



Hi -

can someone suggest a method or point me in the right direction for validating a simple form using php.

I have a form which only has two significant fields - a drop down with three choices, and a dropdown with five choices - and I want the user to have chosen the correct value from the first dropdown before allowing the form to be submitted : ideally, on choosing the incorrect value, the page will display "incorrect please try again" or something along those lines.

the form is like this :

<form action="http://www.domain.com/details.php" method="post" name="id">
<div align="center">
<input type="hidden" name="cc" value="RX">
<input type="hidden" name="co" value="RXS">
</div>
<p align="center">To enter, just answer this simple question:
<p align="center"><b>What year did England win the World Cup? </b></p>
<p align="center">Your Answer&nbsp;&nbsp;&nbsp;&nbsp;
<select name="sa">
<option value="1966">1966</option>
<option value="1968">1968</option>
<option value="1969">1869</option>
</select>
</p>
<p align="center"> <span class="style6">I would like</span>
<select name="quantity">
<option value="1.00">1 ticket</option>
<option value="2.00">2 tickets</option>
<option value="3.00">3 tickets</option>
<option value="4.00">4 tickets</option>
<option value="5.00">5 tickets</option>
</select>
<br>
</p>
<p align="center">&nbsp;</p>
<p align="center"> <a href="javascript:document.id.submit();" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('enter','','Flash/enterbutton_On.gif',1)"><img src="Flash/enterbutton_Off.gif" alt="Go!" name="enter" width="85" height="77" border="0"></a> </p>
</form>

Not the toughest question in the world but thats not what I'm after.

venelin13

10:20 am on Jun 11, 2004 (gmt 0)

10+ Year Member



if($_POST['sa'] == '1966')
{
echo "You win $_POST[quantity] tickets";
}
else
{
echo "Incorrect! Please, try again.";
}

glamdring

11:41 am on Jun 11, 2004 (gmt 0)

10+ Year Member



Thanks - but that doesnt seem to do what I'm after.

Ideally I would like the "submit" button to be inactive until such point as they have chosen the correct answer, or if they submit with an incorrect answer they get the "incorrect" message.

I'm not sure if this is even possible : if someone can tell me how to do it thats great, but if not maybe just point me in the right direction?

Thanks again

HelenDev

11:53 am on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe you should try doing this with javascript. I don't think you can 'disable' the submit button, as it were with PHP, as php, being a server-side language, only works when a request is sent to the server. With javascript however you can manipulate stuff within the page itself.

inwebsys

12:49 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



Javascript would give the desired effect, but I don't know how it would behave if the client has Javascript off in their browser - you might end up giving away a lot of free tickets ...

What I would do is only show the first part of the form ...

<select name="sa">
<option value="1966">1966</option>
<option value="1968">1968</option>
<option value="1969">1869</option>
</select>
</p>

... with a submit button.

Check the validity of the 'sa' value, then refresh and show the rest of the form ...

<p align="center"> <span class="style6">I would like</span>
<select name="quantity">
<option value="1.00">1 ticket</option>
<option value="2.00">2 tickets</option>
<option value="3.00">3 tickets</option>
<option value="4.00">4 tickets</option>
<option value="5.00">5 tickets</option>
</select>

This will insure that the question is answered correctly before the choice for tickets is given.

glamdring

1:03 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



Ah, now thats a good idea - I hadnt thought of that.

I dont really want to use JS, would rather stick with php if poss.

How might I go about doing it? Sorry I dont want all the information for free, just maybe pointed in the right direction as to be honest I havent got a clue where to start : very new to php.

glamdring

2:42 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



As an alternative, is it possible to modify a jump menu to only "jump" when a submit button is pressed?

That way I could get around it because a correct answer would take them to the correct page, an incorrect answer would allow me to direct them to another page, perhaps with an automatic redirection back to the first page?

Is that plausible? or even a good idea?

inwebsys

5:49 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



What do you mean by a 'jump menu'?

glamdring

6:49 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



Sorry maybe its Dreamweaver terminology?

Its a drop-down menu that jumps to a specified url as soon as the value is selected in the dropdown or when the button attached to said menu is clicked

It looks like this :

<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<option value="page3.htm">page3</option>
<option value="page2.htm">page2</option>
<option value="page1.htm">page1</option>
</select>
<input type="button" name="Button1" value="Go" onClick="MM_jumpMenuGo('menu1','parent',0)">

and I didnt know if it would be possible to modify the onClick to work as a submit as well.

bcolflesh

6:55 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice PHP validation class:

[x-code.com...]

glamdring

8:37 am on Jun 13, 2004 (gmt 0)

10+ Year Member



Thankyou for that. A most useful little addition for those of us who know less than nothing about validation.

It has solved one validation issue I had on another page, but the problem with the existing page (the one on this thread) remains.

One remaining question : Would this be easier if I had radio buttons rather than a drop-down?

IE :- choosing button A allow the form to be processed, buttons B or C get the from redisplayed with "incorrect" beside the buttons?