php input class error handling? - php

I have a login div with two input fields. Every input field will be checked for errors.
When I submit the data there are some problems. Here is the HTML:
<div class="small">
<?php if (!isset($errors['a'])):?>
<input type="text" id="a" name="a" value="<?php echo isset ($_POST['a'])? $_POST['a'] : 'Login'; ?>"/>
<?php endif;?>
<?php if (isset($errors['a'])):?>
<input class="log-error" type="text" id="a" name="a" value="<?php echo isset ($_POST['a'])? $_POST['a'] : 'Login'; ?>"/>
<?php endif;?>
</div>
<?php if (isset($errors['a'])):?>
<?php echo "<div id='errordiv'></div><div class='error'>";
echo $errors['a'][0];
echo "</div>";
endif;?>
<div class="small">
<?php if (!isset($errors['b'])):?>
<input type="text" id="b" name="b" value="<?php echo isset ($_POST['b'])? $_POST['b'] : 'Password'; ?>"/>
<?php endif;?>
<?php if (isset($errors['b'])):?>
<input class="log-error" type="text" id="b" name="b" value="<?php echo isset ($_POST['b'])? $_POST['b'] : 'Password'; ?>"/>
<?php endif;?>
</div>
<?php if ( ( isset($errors['b']) ) && (empty($errors['a'][0]) == true) ):?>
<?php echo "<div id='errordiv'></div><div class='error'>";
echo $errors['b'][0];
echo "</div>";
endif;?>
When submitting the data, it won't display both errors at the same time for the input class. It seems like the value-tag is causing this problem.
When I remove the value-tag from both input fields, it will display the error input class at the same time for both.

To me it seems like there is logic misunderstanding.
If you want to display both errors at the time - replace this line
<?php if ( ( isset($errors['b']) ) && (empty($errors['a'][0]) == true) ):?>
with this
<?php if (isset($errors['b'])):?>

Related

How can i Activate Multiple check boxes from mysql database using php

I have saved multiple courses id's in course field for student in Mysql database..
Ex : course : 1,5,7,9
if i edit the student how can i activate selected courses..?
enter image description here
<?php
$courses_query=mysql_query("select * from `course` where status='1'");
while($c_fetch=mysql_fetch_array($courses_query)){
?>
<input type="checkbox" name="course[]" value="<?php echo $c_fetch['id']; ?>" id="courseid" <?php if($re['course']==$c_fetch['id']){ echo "checked='checked'"; } ?> /> <?php echo $c_fetch['course'];
?>
<?php
$cids=$c_fetch['id'];
$softw_qry=mysql_query("select * from `softwares` where course='$cids' and status='1'");
while($softw_fetch=mysql_fetch_array($softw_qry)){
?>
<input type="checkbox" name="software[]" value="<?php echo $softw_fetch['id']; ?>" <?php if($re['software']==$softw_fetch['id']){ echo "checked='checked'"; } ?> /> <?php echo $softw_fetch['software']; ?>
<?php
$soft_ids=$softw_fetch['id'];
$topicsn_qry=mysql_query("select * from `topics` where course='$cids' and software='$soft_ids' and status='1'");
while($topicn_fetch=mysql_fetch_array($topicsn_qry)){
?>
<input type="checkbox" name="topics[]" value="<?php echo $topicn_fetch['id']; ?>" /> <?php echo $topicn_fetch['topic']; ?>
<?php } }} ?>

Adding checked variable from database with fields from database as well

I have a form which has a list of checkboxes to be filled in on insert to database as well as when editing. When editing I am trying to populate the fields with a list of fields from the database which is in the $groups variable and the checked value being checked against the group_id from groups with the group_id from the $user. It is working except it only fills out one checkbox and some users belong to multiple groups. Any ideas, or more efficient ways to do this.
Heres my code so far
<?php foreach($groups as $group) : ?>
<?php foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
} else {
$checked = '';
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
This problem is that you are searching all the $users['group'] array and not stopping when you find a match, so unless last $uG['group_id'] matches $group['id'] you continue and therefore clear $checked after potentially setting it.
So just add a break when you find a match.
Also, officially the correct way of setting the checked status is checked="checked" although most modern browsers are not that pedantic, its possibly better to stick to the HTML spec
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = 'checked="checked"';
break;
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
you could also use a ternary operator and in_array() to do this all in one simple statement
<?php
foreach($groups as $group) :
$checked = in_array( $group['id'], $user['groups'] ) ? 'checked="checked"' : '';
?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
Define $checked = ''; first then loop and change the value if match found
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>

Put the value as title in text field

i want to get the partcular text value as tittle in that field
How can i do..
<input type="text"
<?php if($name_one != '') { ?> value="<?php echo $name_one;?>" <?php echo set_value('name1'); ?>
<?php } else { ?> value="<?php echo set_value('name1'); ?>" <?php } ?>
name="act1" id="act1" title="<?php echo set_value('name1'); ?>">
You can try like this
<input type="text"
value="<?php echo (!empty($name_one)) ? $name_one : $name1 ; ?>"
Single line if condition

How to modify/delete user php

<?php
include 'core/init.php';
admin_protect();
include 'includes/overall/header.php';
?>
<center><h1>Welcome to Admin Page Modify User</h1></center>
<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
echo 'Your Details have been updated! <br> <br>';
echo "<a href=admin.php>Back to Admin Page</a>";
} else {
if (empty($_POST) === false && empty($errors) === true) {
$update_data = array(
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'gender' => $_POST['gender'],
'email' => $_POST['email'],
'dob_day' => $_POST['dob_day'],
'dob_month' => $_POST['dob_month'],
'dob_year' => $_POST['dob_year'],
'allow_email' => $allow_email = ($_POST['allow_email'] == 'on') ? 1 : 0
);
update_user($session_user_id, $update_data);
header('Location: admin_modify.php?success');
exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>
<form action="" method="post">
<ul>
<li>
First Name* : <br> <input type="text" name="first_name" value="<?php echo $user_data['first_name'];?>">
</li>
<li>
Last Name : <br> <input type="text" name="last_name" value="<?php echo $user_data['last_name'];?>">
</li>
Gender*:<br>
<select name="gender" >
<option><?php echo $user_data['gender']; ?></option>
<option>Male</option>
<option>Female</option>
</select>
<li>
Email* : <br> <input type="text" name="email" value="<?php echo $user_data['email'];?>">
</li>
<li>
Date of Birth*:<br>
<select name="dob_day">
<option><?php echo $user_data['dob_day'];?></option>
<?php
loop_date();
?>
</select>
<select name="dob_month">
<option><?php echo $user_data['dob_month'];?></option>
<?php
loop_month();
?>
</select>
<select name="dob_year">
<option><?php echo $user_data['dob_year'];?></option>
<?php
loop_year();
?>
</select>
</li>
<li>
<input type="checkbox" name="allow_email" <?php if ($user_data['allow_email'] == 1) {echo 'checked="checked"';}?> >Would you like to receive email from us?
</li>
<li>
<input type="submit" value="Update">
</li>
</ul>
</form>
<?php
}
include 'includes/overall/footer.php'
?>
<?php include 'includes/overall/footer.php' ?>
I am encountering problems when I want to modify or delete a user using the admin account. The problem is that the current session of the admin account is being recognized when you what to modify or delete it. How can I solve this problem? Thank you for all your help. :)
The best way to acheive what you want to do is to use a hidden input field.
For example, if the user wants to modify the data of the user 48, make him go the page with a GET argument (modify_user.php?userId=48).
In the page, insert a hidden input field like that:
<input type="hidden" name="userId" value="<?php echo $_GET["userId"] ?>" />
This input is hidden, and the sole purpose of it is to transmit data to the process code.
When the form is submitted, you can access the data as usual ($_POST["userId"]).
Take input hidden field for user id and use it not the session when you want to modify/delete the user from admin account.
On the form:
<input type="hidden" name="user_id" value="<?php if(isset($user_data['id'])){ echo $user_data['id'];} ?>">
On backend php page:
if(isset($_REQUEST['user_id'])){
// modify/delete user
$user_id = $_REQUEST['user_id'];
}

Simple PHP questions about hiding a input

I want to hide a field of a form when number 1 has been chosen and is loaded in the databse.
The code gives no errors, but the field stays visible with number 0 and 1.
Somehow I can't get it right. It tried the following;
<?php
$query3 = mysql_query("SELECT `status`, `authcode` FROM `auth` ORDER BY `status` ASC LIMIT 1");
while($row3 = mysql_fetch_object($query3)){
?>
<tr>
<td><b>Authcode:</b></td>
<td>
<input name="authcode" type="text" value="<?= $row->authcode; ?>" <?php if($row->status == 0) ?> />
<input name="authcode2" type="hidden" value="<?= $row->authcode; ?>" <?php if($row->status == 1) ?> />
</td>
</tr>
You need to wrap the if around the output you want to be conditionalized on it:
<?php if ($row3->status == 0) { ?>
<input name="authcode" type="text" value="<?= $row3->authcode; ?>" />
<?php }
if ($row3->status == 1) { ?>
<input name="authcode2" type="hidden" value="<?= $row3->authcode; ?>" />
<?php } ?>
You have to put the if statement before:
<?php if ($row->status == 1) ?>
<input name="authcode2" type="hidden" value="<?= $row->authcode; ?>"/>
Agree with Bamar and Pablo.
If you only want the hidden field once (row->status == 1), you could be more precise with something like:
<input name="authcode" type="<?php echo (($row->status == 1)?'hidden':'text') ?>" value="<?php echo $row->authcode; ?>" />
Cheers.

Categories