Href Modal not working - php

I am new in PHP. My href modal doesn't work perfectly.
I am working on a database search with edit items function.
On initial load of the datatable, the edit anchor tag should return field values in
the modal form.
However it doesn't seem to work. When I clicked the icon it didn't do anything.
I am thinking maybe there is conflict between my search queries and div ids?
Here is my code:
My code for the search query:
$sql_result = mysql_query ($sql, $conn ) or die ( "Could not execute SQL
query" .$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
$project=$row['Project'];
$id=$row['ID'];
$discipline=$row['Discipline'];
$area_code=$row['Area_Code'];
$drawingnumber=$row['Drawing_Number'];
$documenttitle=$row['Document_Title'];
$approveddate=$row['Approved_Date'];
$revnumber=$row['Revision'];
$status=$row['Status'];
$transnumber=$row['Transmittal_No'];
//table row
<tr>
<td><?php echo $project; ?></td>
<td><?php echo $discipline; ?></td>
<td><?php echo $area_code ;?></td>
<td><?php echo $drawingnumber ;?></td>
<td><?php echo $documenttitle; ?></td>
<td><?php echo $approveddate; ?></td>
<td><?php echo $revnumber; ?></td>
<td><?php echo $status; ?></td>
<td><?php echo $transnumber; ?></td>
<td><?php echo $id; ?></td>
<div class='btn-group' role='group' aria-label='...'>
<td> <button type='button' class='btn btn-warning btn-sm'>Edit<span class='glyphicon glyphicon-edit' aria-hidden='true'></span></button></td>
</div>
</tr>
My href edit Modal:
<div id="edit<?php echo $id; ?>" class="modal fade" role="dialog">
<form method="post" class="form-horizontal" role="form">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Item</h4>
</div>
<div class="modal-body">
<input type="hidden" name="edit_item_id" value="<?php echo $id; ?>">
<div class="form-group">
<label class="control-label col-sm-2" for="discipline">Discipline:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="discipline" name="discipline" value="<?php echo $id; ?>" required autofocus>
</div>
<label class="control-label col-sm-2" for="area_code">Area Code:</label>
<div class="col-sm-4">
<input type="text" readonly class="form-control" id="area_code" name="area_code" value="<?php echo $area_code;?>" required>
</div>
<label class="control-label col-sm-2" for="drawingnumber">Drawing Number:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="drawingnumber" name="drawingnumber" value="<?php echo $drawingnumber; ?>" required >
</div>
<label class="control-label col-sm-2" for="documenttitle">Document Title:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="documenttitle" name="documenttitle" value="<?php echo $documenttitle; ?>" required >
</div>
<label class="control-label col-sm-2" for="approveddate">Approved Date:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="approveddate" name="approveddate" value="<?php echo $approveddate; ?>" required>
</div>
<label class="control-label col-sm-2" for="revnumber">Revision Number:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="revnumber" name="revnumber" value="<?php echo $revnumber; ?>" required >
</div>
<label class="control-label col-sm-2" for="status">Status:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="status" name="status" value="<?php echo $status; ?>" required >
</div>
<label class="control-label col-sm-2" for="transnumber">Transmittal Number:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="transnumber" name="transnumber" value="<?php echo $transnumber; ?>" required >
</div>
</div>
</div>
</div>
</div>
</form>
</div>

Related

How to pass form values to bootstrap model form hidden fields

i need to pass form values to bootstrap model form hidden fields on form button click.
basically the button is form button that holds the value of pos_id from database and a hidden text field that holds pro_id from database and when use click "Apply" button a registration form in model is opened where i need to take input from user and also want to store hidden fields value to database along with user inputs
here is my code of form that i am submitting.
<?php
echo "<div class='mt-3'>
<table class='table table-striped'>
<thead>
<tr>
<th>Apply For</th>
<th>Description</th>
<th>Project Name</th>
<th>Type</th>
<th>Last Date</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>".$row['post_name'],"</td>
<td>".$row['post_desc'],"</td>
<td>".$row['proj_name'],"</td>
<td>".$row['proj_type'],"</td>
<td>".$row['last_date'],"</td>
<td>
<form id='post_apply' method='post' action=''>
<input type='hidden' value='".$row['proj_id'],"'","></input>
<button data-toggle='modal' data-target='#applymodal' type='button' name='apply_btn' value='".$row['post_id'],"'"," class='btn btn-success border float-right '>Apply</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>";
}
}
else {
echo "<p class='alert alert-danger text-center'>No Posts Found</p>";
exit;
}
mysqli_close($conn);
and model code is as:
<!-- The Modal -->
<div class="modal fade" id="applymodal" role="dialog">
<div class="modal-dialog " style="max-width:600px;">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header bg-danger ">
<h1 class="modal-title text-center" color="white">New Registration</h1>
<button type="button" class="btn btn-danger" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div id="modalmain" class="modal-body">
<form class="form" action="signup.php" method="post" enctype="multipart/form-data">
<fieldset>
<!-- display form error messages -->
<?php include(INCLUDE_PATH . "/layouts/messages.php") ?>
<div class="form-group <?php echo isset($errors['firstname']) ? 'has-error' : '' ?>">
<label class="control-label">Firs Name</label>
<input type="text" name="first_name" id="first_name" class="form-control form-control-sm">
<?php if (isset($errors['firstname'])): ?>
<span class="help-block"><?php echo $errors['firstname'] ?></span>
<?php endif; ?>
<?php echo $row['post_id'];?>
</div>
<div class="form-group <?php echo isset($errors['last_name']) ? 'has-error' : '' ?>">
<label class="control-label">Last Name</label>
<input type="text" name="last_name" id="last_name" class="form-control form-control-sm">
<?php if (isset($errors['last_name'])): ?>
<span class="help-block"><?php echo $errors['last_name'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['mobile']) ? 'has-error' : '' ?>">
<label class="control-label">Mobile</label>
<input type="text" name="mobile" id="mobile" class="form-control form-control-sm" data-inputmask=" 'mask' : '0399-9999999'" placeholder="Mobile">
<?php if (isset($errors['mobile'])): ?>
<span class="help-block"><?php echo $errors['mobile'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['username']) ? 'has-error' : '' ?>">
<label class="control-label">CNIC Number</label>
<input type="text" name="username" id="username" class="form-control form-control-sm" data-inputmask="'mask': '99999-9999999-9'" placeholder="14 Digits CNIC Number" required="yes">
<?php if (isset($errors['username'])): ?>
<span class="help-block"><?php echo $errors['username'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['email']) ? 'has-error' : '' ?>">
<label class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control form-control-sm" placeholder="Email Address">
<?php if (isset($errors['email'])): ?>
<span class="help-block"><?php echo $errors['email'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['password']) ? 'has-error' : '' ?>">
<label class="control-label">Password</label>
<input type="password" name="password" id="password" class="form-control form-control-sm">
<?php if (isset($errors['password'])): ?>
<span class="help-block"><?php echo $errors['password'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['passwordConf']) ? 'has-error' : '' ?>">
<label class="control-label">Password confirmation</label>
<input type="password" name="passwordConf" class="form-control form-control-sm">
<?php if (isset($errors['passwordConf'])): ?>
<span class="help-block"><?php echo $errors['passwordConf'] ?></span>
<?php endif; ?>
</div>
<input type="hidden" value="$_POST['proj_id'];"></input>
<input type="hidden" value="$_POST['post_id'];"></input>
<div class="form-group">
<button type="submit" name="signup_btn" class="btn btn-block btn-success ">Create Account</button>
</div>
</fieldset>
</form>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<p class="text-start">Aready have an account? Sign in</p>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#salar zaidi: Thank you soo much for help.
This solution worked for me:
$("#post_apply").click(function() {
var proj = $(this).find("#proj_id").val();
var post = $(this).find("#apply_btn").val();
var postn = $(this).find("#post_name").val();
$("#field_proj_id").val(proj)
$("#field_post_id").val(post)
$("#field_post_n").val(postn)
});

How can I update user's data by clicking on the edit button?

Using php, I want to make the Edit button in register.php to display the form which shows fields that have the already existing user's data and then admin can update the data. I'm about to go crazy because of this thing it's been the third day in row not moving to the second step! I've watched many YT tutorials and explored many solutions here and in google as well, but couldn't actually figure out how to fix mine! Please if you know where my mistake is and what should I edit, do tell me and I will be more than happy to fix it.
These are my files:
register.php
<?php
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addadminprofile">
Add Admin Profile
</button>
<!-- Modal -->
<div class="modal fade" id="addadminprofile" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Admin Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="code.php" method="POST">
<div class="modal-body">
<div class="form-group">
<label>Username </label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Full name </label>
<input type="text" name="full_name" class="form-control" required>
</div>
<div class="form-group">
<label>Email </label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Password </label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="form-group">
<label>Confirm password </label>
<input type="password" name="cpassword" class="form-control" required>
</div>
<div class="form-group">
<label>Gender </label>
<input type="radio" name="gender" tabindex="1" value="Male">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female">
<span> Female</span>
</div>
<div class="form-group">
<label>Age </label>
<input type="text" name="age" class="form-control" required>
</div>
<div class="form-group">
<label>Phone number </label>
<input type="text" name="phone_number" class="form-control" required>
</div>
<div class="form-group">
<label>Work durationr </label>
<input type="text" name="work_duration" class="form-control" required>
</div>
<div class="form-group">
<label >Food Category:</label>
<select name="food_category" class="form-control" required>
<option value="hot_cold_dishes">Hot/Cold Dishes</option>
<option value="desserts">Desserts</option>
<option value="drinks">Drinks</option>
</select>
</div>
<div class="form-group">
<label>IG Account </label>
<input type="text" name="ig_account" class="form-control" required>
</div>
<div class="form-group">
<label >State:</label>
<select name="state" class="form-control" required>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3n">Third</option>
</select>
</div>
<div class="form-group">
<label>Postcode </label>
<input type="text" name="postcode" class="form-control" required>
</div>
<div class="form-group">
<label>City </label>
<input type="text" name="city" class="form-control" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="Submit" name="submit" class="btn btn-primary">Register</button>
</div>
</form>
</div>
</div>
</div>
<?php
/*
if(isset($_SESSION['success']) && $_SESSION['success'] != ''){
echo '<h2>'.$_SESSION['success'].'</h2>';
unset ($_SESSION['success']);
}
if(isset($_SESSION['status']) && $_SESSION['status'] != ''){
echo '<script>alert('.$_SESSION['status'].')</script>';
unset ($_SESSION['status']);
}
*/
?>
<div class="card shadow">
<div class="card-body">
<div class="table-responsive">
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject") or die ("<script>alert('Connection Failed.')</script>");
$sql = "SELECT * FROM users";
$result = mysqli_query($connection, $sql);
?>
<table class="table table-borderd" id="dataTable" width="30%" cellspecing="0">
<thead>
<tr>
<th>Username</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
<th>Age</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Work Duration</th>
<th>Food Category</th>
<th>IG Account</th>
<th>State</th>
<th>Postcode</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['username']?></td>
<td><?php echo $row['full_name']?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['password']?></td>
<td><?php echo $row['phone_number']?></td>
<td><?php echo $row['work_duration']?></td>
<td><?php echo $row['food_category']?></td>
<td><?php echo $row['ig_account']?></td>
<td><?php echo $row['state']?></td>
<td><?php echo $row['postcode']?></td>
<td><?php echo $row['city']?></td>
<td><?php echo $row['gender']?></td>
<td><?php echo $row['age']?></td>
<td>
<form action="first_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
</td>
<td>
<button type="submit" class="btn btn-danger">Delete</button>
</td>
</tr>
<?php
}
}
else{
echo "No record found";
}
?>
</tbody>
</table>
</div>
</div>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
first_register.php
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject")or die ("<script>alert('Connection Failed.')</script>");
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> EDIT Admin Profile </h6>
</div>
<div class="card-body">
<?php
if(isset($_POST['edit_button']))
{
$username = $_POST['username'];
$query = "SELECT * FROM register WHERE username='$username' ";
$query_run = mysqli_query($connection, $query);
foreach($query_run as $row)
{
?>
<form action="code.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'] ?>">
<div class="form-group">
<label> Full name </label>
<input type="text" name="edit_full_name" value="<?php echo $row['full_name'] ?>" class="form-control">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="edit_email" value="<?php echo $row['email'] ?>" class="form-control"
placeholder="Enter Email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="edit_password" value="<?php echo $row['password'] ?>"
class="form-control" placeholder="Enter Password">
</div>
<div class="form-group">
<label>Gender</label>
<input type="radio" name="edit_gender" value="Male" class="form-control">
<span> Male</span>
<input type="radio" name="edit_gender" value="Female" class="form-control">
<span> Female</span>
</div>
<div class="form-group">
<label>Age</label>
<input type="text" name="edit_age" value="<?php echo $row['age'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" name="edit_phone_number" value="<?php echo $row['phone_number'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Work Duration</label>
<input type="text" name="edit_work_duration" value="<?php echo $row['work_duration'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Food Category</label>
<select name="food_category" class="form-control" required>
<option value="hot_cold_dishes">Hot/Cold Dishes</option>
<option value="desserts">Desserts</option>
<option value="drinks">Drinks</option>
</select>
</div>
</div>
<div class="form-group">
<label>IG Account</label>
<input type="text" name="edit_ig_account" value="<?php echo $row['ig_account'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>State</label>
<select name="state" class="form-control" required>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3n">Third</option>
</select>
</div>
<div class="form-group">
<label>Postcode</label>
<input type="text" name="edit_postcode" value="<?php echo $row['postcode'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>City</label>
<input type="text" name="edit_city" value="<?php echo $row['city'] ?>"
class="form-control">
</div>
<form action="edit_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
</form>
<?php
}
}
?>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
edit_register.php
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject")or die ("<script>alert('Connection Failed.')</script>");
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<div class="card shadow">
<div class="card-header">
<h6 class="font-weight-bold text-primary">EDIT Profile </h6>
</div>
<div class="card_body">
<?php
if(isset($_POST['updatebtn']))
{
$username2 = $_POST['edit_username'];
$full_name2 = $_POST['edit_full_name'];
$email2 = $_POST['edit_email'];
$password2 = $_POST['edit_password'];
$cpassword2 = $_POST['cpassword'];
$phone_number2 = $_POST['edit_phone_number'];
$work_duration2 = $_POST['edit_work_duration'];
$food_category2 = $_POST['edit_food_category'];
$ig_account2 = $_POST['edit_ig_account'];
$state2 = $_POST["edit_state"];
$postcode2 = $_POST['edit_postcode'];
$city2 = $_POST['edit_city'];
$gender2 = $_POST['edit_gender'];
$age2 = $_POST['edit_age'];
$query = "UPDATE users SET username='$username2', full_name='$full_name2', email='$email2', password='$password2', phone_number='$phone_number2', word_duration='$work_duration2', food_category='$food_category2',
ig_account='$ig_account2', state='$state2', postcode='$postcode2', city='$city2', gender='$gender2', age='$age2' WHERE username='$username2'";
$query_run = mysqli_query($connection, $query);
if($query_run)
{
$_SESSION['status'] = "Your Data is Updated";
$_SESSION['status_code'] = "success";
header('Location: register.php');
}
else
{
$_SESSION['status'] = "Your Data is NOT Updated";
$_SESSION['status_code'] = "error";
header('Location: register.php');
}
?>
<?php
}
?>
</div>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
At register.php, you have this line this -
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
So at first_register.php, you should have this line like this instead -
$username = $_POST['username']; to $username = $_POST['edit_username'];
and this line like this instead -
<form action="code.php" method="POST"> to <form action="edit_register.php" method="POST">
i could see you have a form inside a another form like this
<form action="edit_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
//remove the form tag to this only since you have input for edit_username aready at the top of your form
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
//and finally at edit_register.php change
if(isset($_POST['updatebtn']))
to if(isset($_POST['edit_button']))
I guess this should help achieve the goal. good luck

How to use an if statement with base_url() in PHP?

I want to insert in my db the date of creation of an object form only if the base_url is equal to insertobject ! to be more clear i dont want to insert the date if i'm in update mode my code right now is inserting the date even when i'm updating.
<form method="post" id="object_add" class="card" action="<?php echo base_url();?>object/<?php echo (isset($objectdetails))?'updateobject':'insertobject'; ?>">
<input type="hidden" id="obj_created_date" name="obj_created_date" value="<?php echo date('Y-m-d H:i:s'); ?>">
</form>
Here is my main code :
<form method="post" id="vehicle_add" class="card" action="<?php echo base_url();?>vehicle/<?php echo (isset($vehicledetails))?'updatevehicle':'insertvehicle'; ?>">
<div class="card-body">
<div class="row">
<?php if(isset($vehicledetails)) { ?>
<input type="hidden" name="v_id" id="v_id" value="<?php echo (isset($vehicledetails)) ? $vehicledetails[0]['v_id']:'' ?>" >
<?php } ?>
<div class="col-sm-6 col-md-4">
<label class="form-label">Registration Number</label>
<div class="form-group">
<input type="text" name="v_registration_no" id="v_registration_no" class="form-control" placeholder="Registration Number" value="<?php echo (isset($vehicledetails)) ? $vehicledetails[0]['v_registration_no']:'' ?>">
</div>
</div>
<div class="col-sm-6 col-md-4">
<label class="form-label">Vehicle Name</label>
<div class="form-group">
<input type="text" name="v_name" id="v_name" class="form-control" placeholder="Vehicle Name" value="<?php echo (isset($vehicledetails)) ? $vehicledetails[0]['v_name']:'' ?>">
</div>
</div>
<div class="col-sm-6 col-md-4">
<div class="form-group">
<label class="form-label">Model</label>
<input type="text" name="v_model" value="<?php echo (isset($vehicledetails)) ? $vehicledetails[0]['v_model']:'' ?>" class="form-control" placeholder="Model">
</div>
</div>
<input type="hidden" id="obj_created_date" name="obj_created_date" value="<?php echo date('Y-m-d H:i:s'); ?>">
</form>
Any hero to save my day ??

Passed Undefined Index ID

Line 3
edit_accounts.php
<?php $get_id = $_GET['id']; ?>
<?php// $get_id = isset($_POST['id']) ? $_POST['id'] : ''; ?>
<div class="container">
<div class="margin-top">
<div class="row">
<div class="span12">
<?php
$query=mysqli_query($dbcon,"select * from users_tbl where id ='$get_id'")or die(mysqli_error());
$row=mysqli_fetch_array($query);
$id=$row['id'];
?>
<div class="alert alert-info"><i class="icon-pencil"></i> Edit Member</div>
<p><a class="btn btn-info" href="users.php"><i class="icon-arrow-left icon-large"></i> Back</a></p>
<div class="addstudent">
<div class="details">Please Enter Details Below</div>
<form class="form-horizontal" method="POST" action="update_accounts.php" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="inputEmail">User_ID:</label>
<div class="controls">
<input type="text" id="inputEmail" value="<?php echo $row['code'];?>" name="code" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Username:</label>
<div class="controls">
<input type="text" id="inputEmail" name="username" maxlength="30" value="<?php echo $row['username'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Firstname:</label>
<div class="controls">
<input type="text" id="inputPassword" name="firstname" maxlength="30" value="<?php echo $row['firstname'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Middlename:</label>
<div class="controls">
<input type="text" id="inputPassword" name="middlename" maxlength="30" value="<?php echo $row['middlename'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Lastname:</label>
<div class="controls">
<input type="text" id="inputEmail" name="lastname" value="<?php echo $row['lastname'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Email:</label>
<div class="controls">
<input type="email" id="inputEmail" name="email" value="<?php echo $row['email'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Address:</label>
<div class="controls">
<input type="text" id="inputEmail" name="address" value="<?php echo $row['address'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Contact No:</label>
<div class="controls">
<input type="text" id="inputEmail" name="contact_number" maxlength="11" value="<?php echo $row['contact_number'];?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Position:</label>
<div class="controls">
<input type="text" id="inputEmail" name="position" value="<?php echo $row['position'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">User Type:</label>
<div class="controls">
<input type="text" id="inputEmail" name="type_id" value="<?php echo $row['type_id'];?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">BirthDate:</label>
<div class="controls">
<input type="date" id="inputEmail" name="birthdate" value="<?php echo $row['birthdate'];?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Date Started:</label>
<div class="controls">
<input type="date" id="inputEmail" name="date_started" value="<?php echo $row['date_started'];?>">
</div>
</div>
<div class="control-group">
<div class="controls">
<button name="submit" type="submit" class="btn btn-success"><i class="icon-save icon-large"></i> Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
if (isset($_POST['submit'])){
$usercode=$_POST['code'];
$username=$_POST['username'];
$firstname=$_POST['firstname'];
$middlename=$_POST['middlename'];
$lastname=$_POST['lastname'];
$type_id=$_POST['type_id'];
$birthdate=$_POST['birthdate'];
$address = $_POST['address'];
$email=$_POST['email'];
$password="qwerty";
$contact_number=$_POST['contact_number'];
$position=$_POST['position'];
$date_started=$_POST=['date_started'];
mysqli_query($dbcon,"UPDATE users_tbl set
code='$usercode',firstname='$firstname',lastname='$lastname',middlename = '$middlename',
lastname = '$lastname',type_id = '$type_id',birthdate = '$birthdate',address = '$address',email = '$email',password = '$password',
contact_number = '$contact_number',position = '$position',date_started = '$date_started' where id='$id'")or die(mysqli_error());
?>
<script>alert('Update Status Successful!');
window.location='accounts.php';</script>;
<?php
}
?>
accounts.php
<thead>
<tr>
<th>User ID:</th>
<th>Username</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Email</th>
<th>Address</th>
<th>Contact No.</th>
<th>Position</th>
<th>User Type:</th>
<th>Birthdate:</th>
<th>Date Started</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysqli_query($dbcon,"select * from users_tbl")or die(mysqli_error());
while($row=mysqli_fetch_array($user_query)){
$id=$row['id']; ?>
<tr class="del<?php echo $id ?>">
<td><?php echo $row['code']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['middlename']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact_number']; ?></td>
<td><?php echo $row['position']; ?></td>
<td><?php echo $row['type_id']; ?></td>
<td><?php echo $row['birthdate']; ?></td>
<td><?php echo $row['date_started']; ?></td>
<td class="action">
<a rel="tooltip" title="Delete" id="<?php echo $id; ?>" href="#delete_book<?php echo $id; ?>" data-toggle="modal" class="btn btn-danger"><i class="icon-trash icon-large"></i></a>
<?php include('delete_book_modal.php'); ?>
<a rel="tooltip" title="Edit" id="<?php echo $id; ?>" href="edit_accounts.php<?php echo '?id='.$id; ?>" class="btn btn-success"><i class="icon-pencil icon-large"></i></a>
</tr>
<?php } ?>
</tbody>
</table>
When I press edit, all information is there and After I click Update the error will show(as you can see in the picture) It's already getting the ID because it will not be show the information if not. And it does not save my update, I change the information but after click the button the information was still the same.
Information
Try this,
Replace :
$query=mysqli_query($dbcon,"select * from users_tbl where id
='$get_id'")or die(mysqli_error());
to
$query=mysqli_query($dbcon,"select * from users_tbl where id
=$get_id")or die(mysqli_error());
Remove the single quotes. has it is not replacing as its value.
Please let me know if this works for you.
you should check, does it return result or no.
This error is possible if you do not have proper ID in your DB.
$query=mysqli_query($dbcon,"select * from users_tbl where id ='$get_id'")or die(mysqli_error());
if (!($row=mysqli_fetch_array($query))) {
echo "Can not find user";
exit();
}
$id=$row['id'];
PS: spend time to learn what's prepared statements. And do not use way you passed arguments to SQL query. In this case it's very easy to do SQL Injection.

update checkboxes using ajax php mysqli

this is my modal
<div class="modal fade bs-example-modal-lg" id="exampleModal<?php echo $row['t_id'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel<?php echo $row['t_id'] ?>">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">Termin Ansicht <font color="red">ID</font>: <b><?php echo $row['t_id'] ?></b> <font color="red">Call Agent</font>: <b><?php echo $row['username'] ?></b> <font color="red">Call Datum</font>: <b><?php echo date("d.m.y" , strtotime($row['datum'])) ?></b></h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="col-sm-3">
<input type="text" id="username" value="<?php echo $row['username']; ?>" class="form-control">
</div>
<div class="col-sm-3">
<input type="text" id="t_id" value="<?php echo $row['t_id']; ?>" class="form-control">
</div>
</div>
<br>
<div class="row">
<label class="col-sm-1 control-label">Anrede</label>
<div class="col-sm-2">
<input type="text" id="anrede<?php echo $row['t_id']; ?>" value="<?php echo $row['anrede']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">Vorname</label>
<div class="col-sm-3">
<input type="text" id="vorname<?php echo $row['t_id']; ?>" value="<?php echo $row['vorname']; ?>" class="form-control" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false">
</div>
<label class="col-sm-1 control-label">Nachname</label>
<div class="col-sm-3">
<input type="text" id="nachname<?php echo $row['t_id']; ?>" value="<?php echo $row['nachname']; ?>" class="form-control" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false">
</div>
</div>
<br>
<div class="row">
<label class="col-sm-1 control-label">Strasse</label>
<div class="col-sm-3">
<input type="text" id="strasse<?php echo $row['t_id']; ?>" value="<?php echo $row['strasse']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">PLZ</label>
<div class="col-sm-3">
<input type="text" id="plz<?php echo $row['t_id']; ?>" value="<?php echo $row['plz']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">Ort</label>
<div class="col-sm-2">
<input type="text" id="ort<?php echo $row['t_id']; ?>" value="<?php echo $row['ort']; ?>" class="form-control">
</div>
</div>
<br>
<div class="row">
<label class="col-sm-1 control-label">Krankenkasse</label>
<div class="col-sm-3">
<input type="text" id="krankenkasse<?php echo $row['t_id']; ?>" value="<?php echo $row['krankenkasse']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">Seit</label>
<div class="col-sm-3">
<input type="text" id="seit<?php echo $row['t_id']; ?>" value="<?php echo $row['seit']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">Personen</label>
<div class="col-sm-1">
<input type="text" id="personen<?php echo $row['t_id']; ?>" value="<?php echo $row['personen']; ?>" class="form-control">
</div>
</div>
<br>
<div class="row">
<label class="col-sm-1 control-label">Telefon</label>
<div class="col-sm-2">
<input type="text" id="telefon<?php echo $row['t_id']; ?>" value="<?php echo $row['telefon']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">Termin</label>
<div class="col-sm-2">
<input type="date" id="termin<?php echo $row['t_id']; ?>" value="<?php echo $row['termin']; ?>" class="form-control">
</div>
<label class="col-sm-1 control-label">Uhrzeit</label>
<div class="col-sm-2">
<input type="text" id="time<?php echo $row['t_id']; ?>" value="<?php echo date("H:i", strtotime($row['time'])) ?>" class="form-control">
</div>
<div class="col-sm-2">
<select class="form-control" id="vermittler<?php echo $row['t_id']; ?>" value='<?php echo $row['vermittler'];?>'>
<option ><?php echo $row['vermittler'];?></option>
<?php
$result1 = ("SELECT * FROM users where type='berater'") or mysql_error();
$ilaz=mysql_query($result1);
while($rowa = mysql_fetch_array($ilaz)){
?>
<option id="berater" value = "<?php echo $rowa['username']; ?>" >
<?php echo $rowa['username']; ?>
</option>
<?php
}
?>
</select>
</div>
</div>
<br>
<div class="row">
<label for="ccomment" class="control-label col-lg-2">Bemerkung</label>
<div class="col-lg-4">
<textarea class="form-control " id="coment<?php echo $row['t_id']; ?>" required><?php echo $row['coment']; ?></textarea>
</div>
<label for="ccomment" class="control-label col-lg-1">Feedback</label>
<div class="col-lg-4">
<textarea class="form-control " id="feedback<?php echo $row['t_id']; ?>" ></textarea>
</div>
</div>
<br>
<div class="row">
<?php
$bfeedback = ("select id, user, feedback_b, datum from b_feedback where id='". (int)$_GET['t_id']."' order by datum desc" ) or mysql_error();
$bresult = mysql_query($bfeedback);
if($bfeedback === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($beraterfeedback = mysql_fetch_array($bresult)){
?>
<label for="b_feedback" class="control-label col-lg-2"><?php #echo $beraterfeedback['user'] ?><?php #echo $beraterfeedback['datum'] ?></label>
<td><font color="red"><?php echo $beraterfeedback['user'] ?></font></td>
<td><?php echo $beraterfeedback['datum'] ?></td>
<td><?php echo $beraterfeedback['feedback_b'] ?></td><br> <hr>
<?php
}
?>
</div>
<br>
<div class="row">
<div class="col-sm-20">
<?php
$iAstatus = explode(",",$row['Astatus']);
foreach($arrAstatus as $aAstatus)
{
if(in_array($aAstatus,$iAstatus))
echo "<label class='col-sm-1 control-label'><input type='checkbox' class='s' value='".$aAstatus."' checked> .$aAstatus. </label>";
else
echo "<label class='col-sm-1 control-label'><input type='checkbox' class='s' value='".$aAstatus."'> .$aAstatus.</label>" ;
}
?>
</div>
<div class="col-sm-1">
<input type="text" id="positiv<?php echo $row['t_id']; ?>" value="<?php echo $row['abschluss']; ?>" class="form-control">
</div>
</div>
<br>
<div class="row">
<label class="col-sm-5 control-label"><p style="color:red"><font size="2">Personen Angaben (Namen, Geburtsdatum)?</font></p></label>
<div class="col-sm-5">
<textarea class="form-control " id="personen_angaben<?php echo $row['t_id']; ?>" ><?php echo $row['personen_angaben']; ?></textarea>
</div>
</div>
<br>
<div class="row">
<label class="col-sm-5 control-label"><p style="color:red"><font size="2">Sind Sie (bzw. jemand in Ihrem Haushalt) momentan in ärztliche Behandlung/Therapie(Physio) oder steht Ihnen eine Behandlung bevor?</font></p></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="fr_1<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_1']; ?>" >
</div>
</div>
<br>
<div class="row">
<label class="col-sm-5 control-label"><p style="color:red"><font size="2">Zusatzfrage wenn JA- Dies könnte Auswirkungen auf Ihren Wechsel haben. Deshalb, welcher Art ist denn diese Behandlung?</font></p></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="fr_2<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_2']; ?>">
</div>
</div>
<br>
<div class="row">
<label class="col-sm-5 control-label"><p style="color:red"><font size="2">Nehmen Sie (bzw. jemand in Ihrem Haushalt) regelmässig Medikamente?</font></p></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="z_fr_2<?php echo $row['t_id']; ?>" value="<?php echo $row['z_fr_2']; ?>">
</div>
</div>
<div class="row">
<label class="col-sm-5 control-label"><font size="2"><p style="color:red">Zusatzfrage wenn JA- Was sind das für Medikamente oder für was müssen Sie diese nehmen?</p></font></label>
<div class="col-sm-5">
<input class="form-control" id="fr_3<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_3']; ?>" >
</div>
</div>
<div class="row">
<label class="col-sm-5 control-label"><font size="2"><p style="color:red">Hatten Sie (bzw. jemand an Ihrem Haushalt) in den letzten 10 Jahren eine Operation oder steht eine bevor?</p></font></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="z_fr_3<?php echo $row['t_id']; ?>" value="<?php echo $row['z_fr_3']; ?>">
</div>
</div>
<div class="row">
<label class="col-sm-5 control-label"><font size="2"><p style="color:red">Was für eine Operation wann /ist das?</p></font></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="fr_4<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_4']; ?>" >
</div>
</div>
<div class="row">
<label class="col-sm-5 control-label"><font size="2"><p style="color:red">Wenn die Operation in der Vergangenheit war: beziehen Sie nun eine IV-Rente oder ist dies folgenlos geheilt?</p></font></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="z_fr_4<?php echo $row['t_id']; ?>" value="<?php echo $row['z_fr_4']; ?>" >
</div>
</div>
<div class="row">
<label class="col-sm-5 control-label"><font size="2"><p style="color:red">Falls 1 Person krank ist - Können Sie sich vorstellen, einen Teil Ihrer Familie zu versichern?</p></font></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="fr_5<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_5']; ?>" >
</div>
</div>
<div class="row">
<label class="col-sm-5 control-label"><font size="2">Status</font></label>
<div class="col-sm-5">
<input class="form-control" type="text" id="fr_6<?php echo $row['t_id']; ?>" value="<?php echo $row['status']; ?>">
</div>
</div>
<div class="row">
<label class="col-sm-4 control-label"> </label>
<div class="col-sm-3">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" onclick="updatedata('<?php echo $row['t_id'] ?>')" data-dismiss="modal" class="btn btn-primary">Update</button>
</div>
</div>
and this jquery ajax
function updatedata(str){
var t_id = str;
var anrede = $('#anrede'+str).val();
var vorname = $('#vorname'+str).val();
var nachname = $('#nachname'+str).val();
var strasse = $('#strasse'+str).val();
var plz = $('#plz'+str).val();
var ort = $('#ort'+str).val();
var krankenkasse = $('#krankenkasse'+str).val();
var seit = $('#seit'+str).val();
var personen = $('#personen'+str).val();
var telefon = $('#telefon'+str).val();
var termin = $('#termin'+str).val();
var time = $('#time'+str).val();
var vermittler = $('#vermittler'+str).val();
var coment = $('#coment'+str).val();
var feedback = $('#feedback'+str).val();
var s = new Array();
$("input:checked").each(function() {
s.push($(this).val());
});
var positiv = $('#positiv'+str).val();
var personen_angaben = $('#personen_angaben'+str).val();
var fr_1 = $('#fr_1'+str).val();
var fr_2 = $('#fr_2'+str).val();
var z_fr_2 = $('#z_fr_2'+str).val();
var fr_3 = $('#fr_3'+str).val();
var z_fr_3 = $('#z_fr_3'+str).val();
var fr_4 = $('#fr_4'+str).val();
var z_fr_4 = $('#z_fr_4'+str).val();
var fr_5 = $('#fr_5'+str).val();
var fr_6 = $('#fr_6'+str).val();
var datas="anrede="+anrede+"&vorname="+vorname+"&nachname="+nachname+"&strasse="+strasse+"&plz="+plz+"&ort="+ort+"&krankenkasse="+krankenkasse+"&seit="+seit+"&personen="+personen+"&telefon="+telefon+"&termin="+termin+"&time="+time+"&vermittler="+vermittler+"&coment="+coment+"&feedback="+feedback+"&s[]="+s+"&positiv="+positiv+"&personen_angaben="+personen_angaben+"&fr_1="+fr_1+"&fr_2="+fr_2+"&z_fr_2="+z_fr_2+"&fr_3="+fr_3+"&z_fr_3="+z_fr_3+"&fr_4="+fr_4+"&z_fr_4="+z_fr_4+"&fr_5="+fr_5+"&fr_6="+fr_6;
alert(datas);
$.ajax({
type: "POST",
url: "updatedata.php?t_id="+t_id,
data: datas,
cache: false
}).done(function( data ) {
$('#info').html(data);
viewdata();
});
}
and this the php array
$arrAstatus = array("Wollte k T","Kunde Ni err.","St .Gef","N.Zuhaus","Offen","Negativ","MJV","Behan.","Abgesagt","Positiv");
Now when i submit the form it update all unchecked checkboxes.
how can i do that it update only checked checkboxes and after refresh to show me it checked
Problems:
you are creating your checkbox Ids as an array syntax s[]. I believe you are confusing it with the name attribute. Change this: id='s[]' to this: name='s[]'
<font> is no longer supported. Also, the opening and closing tags of that element is not correct.
where is your ajax? where's your form? where's your submit button? Without this info, you leave us to assume this is a form, and that your jquery function gets triggered when the form is submitted. If thats the case, then you need your actionattribute with the URL that AJAX will use to send the request.
Summary:
Please be more descriptive, show your HTML, your PHP, jQuery properly so that we can best help you. In order to solve your problem you need to send an AJAX request. Find out how to do that, implement that in your code, and then we can continue from there.
It seems you need help urgently with this. The main problem I see is that you are missing the name attribute in your form elements. You need to use name attributes so that when you gather the data in jQuery you can use the .serialize() function. I will sort of redo your code in a way that I think you have it working. I will delete all that bootstrap stuff and the non-related code so that you can see what I am doing:
I am assuming your data is coming from your database. I will use an array called $data to mimic that result set:
//your database data
$data = array(
array(
"t_id" => 1,
"username" => "john",
"Astatus" => "Wollte k T,Kunde Ni err.,N.Zuhaus"
)
);
//your comparison data
$arrAstatus = array("Wollte k T", "Kunde Ni err.", "St .Gef", "N.Zuhaus", "Offen", "Negativ", "MJV", "Behan.", "Abgesagt", "Positiv");
Next, I assume you iterate through each result as $row and create your forms. I removed your classes/ids because we do not need them for this purpose. Instead, I placed the needed name attributes in your inputs:
<?php foreach ($data as $row) { ?>
<form>
<input type="text" name="username" value="<?php echo $row['username']; ?>" class="form-control">
<input type="text" name="t_id" value="<?php echo $row['t_id']; ?>" class="form-control">
<?php
$iAstatus = explode(",", $row['Astatus']);
foreach ($arrAstatus as $aAstatus) {
if (in_array($aAstatus, $iAstatus))
echo "<label><input type='checkbox' name='s[]' value='$aAstatus' checked>$aAstatus</label><br>";
else
echo "<label><input type='checkbox' name='s[]' value='$aAstatus'>$aAstatus</label><br>";
}
?>
<input type="submit">
</form>
<?php } ?>
Lastly, your jQuery should be triggered when the form is submitted using the .submit() function. Once submitted, use .serialize() to obtain all inputs with the name attributes and send it through AJAX:
$('form').submit(function () {
var datas = $(this).serialize();
$.ajax({
type: "POST",
url: "submit.php",
data: datas,
cache: false
}).done(function (data) {
console.log(data);
});
return false;
});
Hope this helps.

Categories