Page 1 of 1
how to validate in if Statement
Posted: Thu Jan 14, 2010 7:36 am
by manojsemwal1
Hai
Iam using to variable
$obtmarks = $_POST['obtmarks']; //Text Box value in Array
$check = $_POST['check']; //Check box value in Array
i want like
1 if user select any checkbox and not entered any value in textbox it will display error like "pl enter value in text box".
2-if user fill value in textbox and checkbox will not select then give error pl select checkbox.
the error should be same checkbox and textbox field.
Thanks for Advanced.
Regards,
Re: how to validate in if Statement
Posted: Thu Jan 14, 2010 8:01 am
by requinix
So how much work have you done yourself?
If you don't have any code to show us, come back when you do.
Re: how to validate in if Statement
Posted: Thu Jan 14, 2010 10:47 pm
by manojsemwal1
Thanks for your Reply my code are as follows :
<?php
include('../db.php');
$obtmarks = $_POST['obtmarks']; //Getting marks entered by User that can be n number so i take array type
$check = $_POST['check']; //Getting Value Check box Its also array type because user can select any checkbox
$regdid = $_POST['regdid'];
$subjectid = $_POST['subjectid'];
$countmarks=count($obtmarks);
//echo $countmarks;
//echo $subjectid[0];
if(isset($_POST['check']))
{
$numbSelected = sizeof($_POST['obtmarks']);
$numbCheck = sizeof($_POST['check']);
echo $numbSelected;
echo $numbCheck;
$minimumSelected = 1;
foreach($_POST['check'] as $key=>$checkid)
{
for($i=0;$i<$numbSelected;$i++)
{
if($obtmarks[$i]=='' )
{
$errors[]="Pl enter Marks";
echo $check[$i];
}
else
{
$sql="insert into studentmarks value('$checkid',$obtmarks[$i]'',$subjectid'') ";
echo $sql;
}
}}
}
else
{
$errors[]="Pl Select the Check Box";
}
iam attaching the operation window pl review it
Re: how to validate in if Statement
Posted: Sat Jan 16, 2010 5:48 am
by social_experiment
Code: Select all
<?php
//check if any checkboxes have been marked
if (isset($_POST['obtmarks'])) {
//if a check box has been marked, check if text has been submitted
if (isset($_POST['textfield']) && $_POST['textfield'] != '') {
//code that does something
}
//if no text has been entered in the textbox
else {
echo 'Please fill in the text field';
}
}
//no checkboxes have been marked
else {
echo 'Please tick checkbox';
}
?>
Re: how to validate in if Statement
Posted: Sat Jan 16, 2010 2:40 pm
by AbraCadaver
You're going to have to make sure that the index of the checkbox and the text field match because checkboxes are only included in the $_POST Array if they are checked, so you cant just use var[] (not tested):
Code: Select all
<input type="text" name="txt[1]">
<input type="checkbox" name="cb[1]">
<input type="text" name="txt[2]">
<input type="checkbox" name="cb[2]">
<input type="text" name="txt[3]">
<input type="checkbox" name="cb[3]">
<input type="text" name="txt[4]">
<input type="checkbox" name="cb[4]">
Code: Select all
foreach($_POST['txt'] as $key => $val) {
if(empty($_POST['txt'][$key]) && isset($_POST['cb'][$key])) {
echo "You must enter text for the checked entry";
}
elseif(!empty($_POST['txt'][$key]) && !isset($_POST['cb'][$key])) {
echo "You must check the box for the text entered";
}
}
You can also use something more descriptive in the array:
Code: Select all
<input type="text" name="txt[something]">
<input type="checkbox" name="cb[something]">
<input type="text" name="txt[somethingelse]">
<input type="checkbox" name="cb[somethingelse]">
Re: how to validate in if Statement
Posted: Mon Jan 18, 2010 7:18 am
by manojsemwal1
Thanks AbraCadaver for your nice reply
ya i have used both text and checkbox value in array. i had tried your code its working fine in single entry but when multiple entry comes that time its show both error see the image below.

- error.jpg (44.74 KiB) Viewed 1778 times
why this is happening ?
Re: how to validate in if Statement
Posted: Mon Jan 18, 2010 8:56 am
by AbraCadaver
Well yes. That was example code that you can integrate into your app. You have to decide what you want to do when you encounter an error. I left that up to you, but here is another example:
Code: Select all
foreach($_POST['txt'] as $key => $val) {
if(empty($_POST['txt'][$key]) && isset($_POST['cb'][$key])) {
echo "You must enter text for the checked entry";
break;
}
elseif(!empty($_POST['txt'][$key]) && !isset($_POST['cb'][$key])) {
echo "You must check the box for the text entered";
break;
}
}
Re: how to validate in if Statement
Posted: Tue Jan 19, 2010 12:20 am
by manojsemwal1
Hai AbraCadaver
In earlier statement i have selected one checkbox and filled the value but if and else both error comes. while both conditions is ok and send no errors but its seems error.
i mean that if user select any record of n numbers , the record user has been selected, the value of remarks should go into the database but in here first value of remarks is taking. see in the image above i have selected 8011 but its taking 8009 remark value.
i think its happening in array type like if i select cb[1] it should be take value text[1].but its taking text[0] value.
code are:-----
echo"<tr><td class='x-blue_dg_td dg_center dg_nowrap'><input type='checkbox' value='$regdid' name='checkregdid[]'></td>
<td class='x-blue_dg_td dg_center dg_nowrap' >$regdid</td>
<td class='x-blue_dg_td dg_center dg_nowrap' >$name</td>
<td class='x-blue_dg_td ' ><textarea cols='80' rows='5' name='remarks[]'>$b[Marksobtain]</textarea></td>";
submit form code are-:
foreach($_POST['remarks'] as $key => $val)
{
if(empty($_POST['remarks'][$key]) && isset($_POST['checkregdid'][$key]))
{
$errors[]= "You must enter text for the checked entry"."<br>";
break;
}
else if(!empty($_POST['remarks'][$key]) && !isset($_POST['checkregdid'][$key]))
{
$errors[]= "You must check the box for the text entered"."<br>";
break;
}
}
foreach($_POST['checkregdid'] as $key => $val)
{
echo $val;
if(empty($_POST['remarks'][$key]))
{}
else
{
$rem= $_POST['remarks'][$key];
echo $rem;
}
}
Thanks.........
Re: how to validate in if Statement
Posted: Tue Jan 19, 2010 8:04 am
by AbraCadaver
Yes, I told you earlier that you can't use name='checkregdid[]' and name='remarks[]'. You have to use name='checkregdid[1]', name='remarks[1]' and name='checkregdid[2]', name='remarks[2]', etc...
Re: how to validate in if Statement
Posted: Wed Jan 20, 2010 1:09 am
by manojsemwal1
yes i know that but iam using n numbers of student it s not possible that we define each array with their index.
like if u have 100 student and u want to put 50 any student marks out of 100 so how u can define array with index.......
while name value coming form database..........................
Thanks
Re: how to validate in if Statement
Posted: Wed Jan 20, 2010 7:49 am
by AbraCadaver
manojsemwal1 wrote:yes i know that but iam using n numbers of student it s not possible that we define each array with their index.
like if u have 100 student and u want to put 50 any student marks out of 100 so how u can define array with index.......
while name value coming form database..........................
Thanks
In your loop to echo the inputs increment a counter:
Code: Select all
$i++;
echo"<tr><td class='x-blue_dg_td dg_center dg_nowrap'><input type='checkbox' value='$regdid' name='checkregdid[$i]'></td>
<td class='x-blue_dg_td ' ><textarea cols='80' rows='5' name='remarks[$i]'>$b[Marksobtain]</textarea></td>";
Or instead of the counter you could use the $regdid or the student id, whatever is appropriate.
Re: how to validate in if Statement
Posted: Wed Jan 20, 2010 11:31 pm
by manojsemwal1
I had tried the loop but its take last selection value.................
Re: how to validate in if Statement
Posted: Thu Jan 21, 2010 10:35 am
by AbraCadaver
manojsemwal1 wrote:I had tried the loop but its take last selection value.................
Then you didn't do it properly. Show your form code and form processing code. Use code tags.
Re: how to validate in if Statement
Posted: Thu Jan 28, 2010 12:24 am
by manojsemwal1
Sorry for Delay.........
my code are as follows :
while($n<$count)
{
$regdid=mysql_result($rs,$n,0);
$name=mysql_result($rs,$n,1);
$courseid=mysql_result($rs,$n,2);
$sql2="select Remarks from quitstudents where CourseId='$courseid' and RegdId='$regdid' and Approval='L'";
//echo $sql2;
$rs1=mysql_query($sql2);
$countsql2=mysql_num_rows($rs1);
$b=mysql_fetch_array($rs1);
$marksobt = $b[Remarks];
//echo $countsql2;
if($countsql2==1)
{
echo"<tr><td class='x-blue_dg_td dg_center dg_nowrap'><input type='checkbox' name='check[]' checked></td>";
}
else
{
echo"<tr><td class='x-blue_dg_td dg_center dg_nowrap'><input type='checkbox' name='check[]'></td>";
}
echo" <td class='x-blue_dg_td dg_center dg_nowrap' ><input type='hidden' value='$regdid' name='checkregdid'>$regdid</td>
<td class='x-blue_dg_td dg_center dg_nowrap' >$name</td>
<td class='x-blue_dg_td ' ><textarea cols='80' rows='5' name='remarks[]'>$b[Remarks]</textarea></td>";
$n++;
}
Thanks