form validation problem [help need]

Status
Not open for further replies.

shakac

Active Member
47
2011
0
0
i want to validate a form field that is an array of input but is not working even input field is not showing
please help me
here is my form in php validation
PHP:
<?php
include("header.php");
?>
<?php
    session_start();
$class=$_SESSION['class']=$_POST['class'];
$sess=$_SESSION['session'] = $_POST['session'];
$sub=$_SESSION['subject'] = $_POST['subject'];
$term=$_SESSION['term'] = $_POST['term'];
include("config.php");
$query="SELECT * FROM student_information WHERE class='$class'";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

    // initialization
    $photo_upload_fields = "";
    $counter = 1;

    // default number of fields
    $number_of_fields = $num_rows; 

// If you want more fields, then the call to this page should be like, 
// preupload.php?number_of_fields=20

    if( $_GET['number_of_fields'] )
    $number_of_fields = (int)($_GET['number_of_fields']);

    // Firstly Lets build the Category List
echo"<h1>class:$class</h1>";
echo"<br/>";
echo"<h1>session:$sess</h1>";
echo"<br/>";
echo"<h1>subject:$sub</h1>";
echo"<br/>";
echo"<h1>term:$term</h1>";
// Lets build the Photo Uploading fields
    while( $counter <= $number_of_fields )
    {
    $row1 = mysql_fetch_row($result);
$sid = $row1['1'];
$photo_upload_fields .=<<<__HTML_END
<tr>
    <td>
          roll {$counter}:<input name='marks[]' type='text' />// want to validate
          <input type='hidden' name='roll[]' value='$counter'/>
          
    </td>
</tr>
<tr>
    <td>
        <input name='sid[]' type='hidden' value='$sid'/>
    </td>
</tr>
<tr>
    <td>
         
    </td>
</tr>
<tr>
    <td>
         <input name='sub[]' type='hidden' value='$sub'/>
    </td>
</tr>
<tr>
    <td>
         <input name='sess[]' type='hidden' value='$sess'/>
    </td>
</tr>
<tr>
    <td>
         <input name='class[]' type='hidden' value='$class'/>
    </td>
</tr>
<tr>
    <td>
        <input name='term[]' type='hidden' value='$term'/>
    </td>
</tr>
__HTML_END;
    $counter++;
    }

// Final Output
echo <<<__HTML_END
<html>
<head>
    <title>insert classtest marks</title>
    <script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
  if (form.marks.value == "") {
    alert( "Please enter marks." );
    form.marks.focus();
    return false ;
  } 
  // ** END **
  return true ;
}
//-->
</script>
</head>
<body>
<form  action='marksentry1.php' method='post' name='upload_form' onSubmit="return checkform(this);">
<table width='90%' border='0' align='center' style='width: 90%;'>
<tr>
    <td>
        <p>&nbsp;</p>
    </td>
</tr>

<!-Insert the photo fields here -->
$photo_upload_fields

<tr>
    <td>
            <input type='submit' name='submit' value='Add marks' />
    </td>
</tr>
</table>
</form>
</body>
</html>
__HTML_END;
?>
 
3 comments
Replace checkform javascript function:
PHP:
function checkform ( form )
{
  if (form.marks.value == "") {
    alert( "Please enter marks." );
    form.marks.focus();
    return false ;
  } 
  // ** END **
  return true ;
}
With:
PHP:
function checkform ( form )
{
  if (form.elements["marks[]"].value == "") {
    alert( "Please enter marks." );
    form.elements["marks[]"].focus();
    return false ;
  } 
  // ** END **
  return true ;
}
This should work to validate the marks first.
 
Last edited:
Status
Not open for further replies.
Back
Top