0

I've been at this the whole day. First I tried to validate my form in php but finally gave up. Now I'm trying to validate the form with JavaScript. It works but since I don't know JavaScript at all and have limited knowledge of PHP I really need help please. It is a test and I want to validate that each question has been answered. The field name in the form = php variable/array, can't get this to work in the javascirpt

var x=document.forms["myForm"]["question_{$q_nr}"].value;

. If I just use a text field name it works fine.

The form

<html>
<head>
<?php
session_start();
?>
<script type="text/javascript" src="counter.js"></script>
<script language="JavaScript">
<!--
function validateForm()
{
var x=document.forms["myForm"]["question_{$q_nr}"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
//-->
</script>
</head>

<body>
<?php
if ($_SESSION['auth']) {
$tid = $_GET['tid'];
echo "<span id='counter'></span>";
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
$result1=mysql_query($sql1);

echo "<form name='myForm' action='http://localhost/index.php?option=com_content&view=article&id=51' onsubmit='return validateForm()' method='post'>";
while($row1 = mysql_fetch_array($result1))
{
    $q_nr=$row1['q_nr'];
    $q_type=$row1['q_type'];
    $question=$row1['question'];
    $option1=$row1['option1'];
    $option2=$row1['option2'];

    echo "<P><strong>$q_nr $question</strong><BR>";
    echo "<img src='images/tests/$pic'>";
    echo "<BR>";
    echo "<BR>";
    echo "</p>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question_{$q_nr}' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question_{$q_nr}' value='B'>$option2<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question_{$q_nr}[]' value='A'>$option1<BR>";
} else {
echo ''; } 
if($option2!="") {
echo "<input type='checkbox' name='question_{$q_nr}[]' value='B'>$option2<BR>";
} else {
echo ''; } 
} //if q_type <> mr
} //while row1
echo "First name: <input type='text' name='fname'>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
} //else if now > testend
} //if ses auth
?>
caitriona
  • 8,569
  • 4
  • 32
  • 36
Wilest
  • 1,820
  • 7
  • 36
  • 62
  • 3
    Validating a form using javascript does not ensure that the sent data are good. Infact, a person could always disable javascript and submit the data. – Aurelio De Rosa Oct 06 '11 at 14:42
  • `First I tried to validate my form in php but finally gave up. Now I'm trying to validate the form with JavaScript.` I fear that server-side validtion is mandatory, or I can just disable my JS and crack your script;) – Damien Pirsy Oct 06 '11 at 14:43
  • Anyway, you accepted an answer on how to validate with php, what's gone wrong in the meantime? http://stackoverflow.com/questions/7672214/validate-form-in-php/7672879#7672879 – Damien Pirsy Oct 06 '11 at 14:46
  • @Damien Pirsy Well it does not want to validate the form in the foreach – Wilest Oct 06 '11 at 14:52

3 Answers3

1

why do you think so complicated ?

just use unique ID's for each label, input field, etc ... and check for the innerHTML or value stored; and also you can change the values whenever you want;

btw, your code is messy;

write your code like this:

zone 1: all the js goes here

zone 2: all the html goes here

zone 1:

<script language="text/JavaScript">
//alljs code here

function validate()
{

if( document.getElementById('unique_2').value == null ) document.getElementById('unique_3').innerHTML = 'error1';

if(document.getElementById('unique_4').value == null ) document.getElementById('unique_6').innerHTML = 'error 2';

}

</script>

zone 2:

<form onsubmit="validate()">

all html label and fields go here, like:

<label id="unique_1">label text</label>
<input type="text" id="unique_2" value="" />
<label id="unique_3"></label> this is for the error message

<label id="unique_4">label text</label>
<input type="text" id="unique_5" value="" />
<label id="unique_6"></label> this is for the error message

</form>
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
0

This is simple JQuery validation.

I think you must have both validation if you want to be secured...

p.s. Just add 'required' in class of input if you want to be required...

crazyjul
  • 2,519
  • 19
  • 26
ikamatovic
  • 43
  • 2
  • 7
-2

Well for one thing -- validating on the server side is the way to go.

If you just validate on the client side, it can be futzed with (a lot)

Naftali
  • 144,921
  • 39
  • 244
  • 303