-2

I have this form,

<form action="/action_page.php">
  First name:
  <input name="firstname" type="text">
  <br>
  Last Name:
  <input name="lastname" type="text">
  <br>
 <select name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select> <br>
  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="submit">
</form>

all fields could be empty on submission, except if gender=female, where the user must fill in firstname and lastname Since this is an internal form, client validation is enough..

Sorry, this is my first post.. I'm searching for javascript validation code..

can someone help me ?

PAOK1926
  • 1
  • 2
  • 1
    `if ($_GET['gender'] == "female") { ` can be a good start (get because you didn't defined a method, the default one being get) – Cid Feb 12 '19 at 15:06
  • Where is you validation code part? – Siva Feb 12 '19 at 15:07
  • 1
    Do you want server side or client side validation? You've added both PHP and Javascript tag, but you didn't include code from either of them. – Stephan Vierkant Feb 12 '19 at 15:07
  • @StephanVierkant You never want client-side validation *only* – Cid Feb 12 '19 at 15:08
  • i do not have any validation code.. thats what i'm searching for.. :) – PAOK1926 Feb 12 '19 at 15:08
  • client side validation, would be ok.. – PAOK1926 Feb 12 '19 at 15:09
  • 1
    @Cid I agree, but one should start with at least one of them. My question was: what is this question about? – Stephan Vierkant Feb 12 '19 at 15:09
  • client side validation is not secure. Anyone can change the DOM and skip the validation. @StephanVierkant I got your point – Cid Feb 12 '19 at 15:09
  • 1
    @PAOK1926 Instead of placing multiple comments, you can edit them. Or even better: edit your question so other users don't have to read trough all comments before understanding your question. – Stephan Vierkant Feb 12 '19 at 15:11
  • @StephanVierkant the question is about how to validate and submit this form... – PAOK1926 Feb 12 '19 at 15:11
  • @Cid You are right, but having client side validation is not just about security, but also about a proper user experience (don't have to wait for the server). – Stephan Vierkant Feb 12 '19 at 15:11
  • Questions like 'I want A and B, please fix it' will be closed here because it's too broad. Please do some research about both client side and server side validation and edit your question if you've chosen one. And why did you add the `for-loop` tag? – Stephan Vierkant Feb 12 '19 at 15:13
  • _client side validation, would be ok_ Well now we have your requirements, would now be a good time to talk money? – RiggsFolly Feb 12 '19 at 15:20

1 Answers1

1

Your question involves many different things. You tagged it as javascript, but your post and code only reference PHP. So I will talk about a possible solution in PHP:

We know that this form will send the data to /action_page.php. Since you haven't specified a method=".." in your form, the data will be transmitted by the GET method, which will then be accessible at action_page.php through $_GET['gender'], for example.

Now at action_page.php you just verify your input and decide on what course of action will be taken.

if ($_GET['gender'] == 'female' && !isset($_GET['firstname']) {....}

Cahue
  • 11
  • 1