how to validate in if Statement
Moderator: General Moderators
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
how to validate in if Statement
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,
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
So how much work have you done yourself?
If you don't have any code to show us, come back when you do.
If you don't have any code to show us, come back when you do.
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
Re: how to validate in if Statement
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
<?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
- Attachments
-
- error.jpg (37.25 KiB) Viewed 1825 times
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: how to validate in if Statement
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';
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: how to validate in if Statement
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):
You can also use something more descriptive in the array:
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";
}
}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]">mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
Re: how to validate in if Statement
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. why this is happening ?
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. why this is happening ?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: how to validate in if Statement
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;
}
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
Re: how to validate in if Statement
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.........
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.........
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: how to validate in if Statement
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...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
Re: how to validate in if Statement
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
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: how to validate in if Statement
In your loop to echo the inputs increment a counter: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
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>";mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
Re: how to validate in if Statement
I had tried the loop but its take last selection value.................
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: how to validate in if Statement
Then you didn't do it properly. Show your form code and form processing code. Use code tags.manojsemwal1 wrote:I had tried the loop but its take last selection value.................
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
manojsemwal1
- Forum Contributor
- Posts: 217
- Joined: Mon Jun 29, 2009 4:13 am
- Location: India
Re: how to validate in if Statement
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
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