Two Submit from normal form and Modal Form - php

I have a form to enter some details,in that one select control is there,to select some option(Audit ID) .IF the user could not find the correct option,then he can add in using another button in the same form beside of that select control.By clicking that button one modal-form will appear and the input control is there,entering data and saving it,will save that value in one table (table : auditnumber). And the main form will save all entered data in another table(auditplan).So i have two submit buttons saving different value to different tables and the modal form is just optional..in case user didnt find exact option.
Just i tried these code below..but didnt work. the modal form submit button didnt work.
And i dont know whether this one is good idea. Just im beginner in web app.
<?php
include("config.php");
include("header.php");
session_start();
try {
$sql = "SELECT * FROM auditnumber";
$stmt = $DB->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
}
catch (Exception $ex)
{ echo $ex->getMessage(); }
?>
<div class="col-md-10 main">
<form class="form-horizontal" role="form" action="auditplanentry.php" method="POST">
<div class="form-group">
<label class="control-label col-sm-2" for="usr">Audit ID:</label>
<div class="col-sm-5">
<select id="course" name="course" class="form-control" ><option>Select</option>
<?php foreach($result as $row){ ?>
<option><?php echo $row['auditnumber']?></option>
<?php }
?>
</select>
</div>
<button type="button" class="btn btn-primary btn-circle" rel="tooltip" title="Add audit ID,if not present in select option" data-toggle="modal" data-target="#auditidmodal"><i class="glyphicon glyphicon-plus"></i></button>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Year:</label>
<div class="col-sm-5">
<input type="text" class="form-control col-xs-3" id="year" name ="year">
</div>
</div>
<div class="form-group">
<div class=" col-sm-offset-3">
<button type="submit" name="submit" id ="submit" class="btn btn-primary">Save</button>
<button type="submit" name="submit1" id ="clear" class="btn btn-primary">Cancel</button>
</div>
</div>
</form>
</div>
<?php
if(isset($_POST['submit']))
{
if(trim($_POST['auditid'])=='')
{
echo "<script language='javascript'>alert('Please Enter Audit ID.');</script>";
exit;
}
elseif(trim($_POST['year'])=='')
{
echo "<script language='javascript'>alert('Please Enter Year.');</script>";
exit;
}
$audit=trim($_REQUEST['auditid']);
$year=trim($_REQUEST['year']);
$sql = "INSERT INTO auditplan(auditid,year) VALUES " . "(:audit, :year)";
try {
$stmt = $DB->prepare($sql);
// bind the values
$stmt->bindValue(":audit", $audit);
$stmt->bindValue(":year", $year);
// execute Query
$stmt->execute();
}
catch (Exception $ex)
{
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = $ex->getMessage();
}
}
elseif(isset($_POST['submit1']))
{
echo "<script language='javascript'>document.location.href='Auditplan.php';</script>";
}
?>
<div class="modal" id="auditidmodal" tabindex="-1" role="dialog" aria-labelledby="messageModelLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<form id="auditidform" role="form" action="auditplanentry.php" method="POST">
<div class="control-group">
<label class="control-label" for="leave_status">Enter Audit ID:</label>
<div class="controls">
<input id="audit" class="form-control" name="audit" placeholder="Eg: IA01" ></input>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="submit" name="save" id ="save" >Save</button>
<button class="btn" type="submit" name="clear" id ="clear">Cancel</button>
</div>
</div>
</div>
</div>
<?php
if(isset($_POST['save']))
{
if(trim($_POST['audit'])=='')
{
echo "<script language='javascript'>alert('Please Enter Audit ID.');</script>";
exit;
}
$audit=trim($_REQUEST['audit']);
$sql = "INSERT INTO auditnumber(auditid) VALUES " . "(:audit)";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":audit", $audit);
$stmt->execute();
}
catch (Exception $ex)
{
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = $ex->getMessage();
}
}
elseif(isset($_POST['clear']))
{
echo "<script language='javascript'>document.location.href='Auditplanentry.php';</script>";
}
?>

Related

How to fix 'If(isset($_POST['Id'])) error' while editing the form in a PHP based CRUD application?

The "if(isset($_POST["titleId"]) && !empty($_POST["titleId"])" in my code is returning false value.
I'm working on a CRUD application, the insert modal is working fine, now I'm stuck at the update part of it. So when you click on the update icon it does fetch the right titleId in the URL but the first 'if' condition returns false and hence the update isn't working.
Here's what I've tried so far.
admin.php
<?php
$typeId = filter_input(INPUT_GET, "type");
$titleId = filter_input(INPUT_GET, "titleId");
$active = "admin" . $typeId;
require_once './pages/header.php';
require_once './functions/queries.php';
$getAll = Queries::getAllTitle($typeId);
?>
<div class="container">
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left"></h2>
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#facultyAddModal">Add Title</button>
</div>
<!--<div class="container">
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#facultyAddModal">Add Title</button>
<br><br>-->
<div class="panel-group" id="titleAccordion">
<?php
for ($i = 0; $i < count($getAll); $i++) {
echo <<<HTML
<div class="panel panel-default">
<div class="panel-heading"><h4 class="panel-title">
<a data-toggle="collapse" data-parent="#titleAccordion" href="#collapseF{$i}">{$getAll[$i]['title']}</a></h4>
</div>
<div id="collapseF{$i}" class="panel-collapse collapse" >
<div class="panel-body">
<div class="table-responsive">
<table class="table table-condensed"><tbody>
<tr><td>Title:</td><td>{$getAll[$i]['title']}</td></tr>
<tr><td>Units:</td><td>{$getAll[$i]['units']}</td></tr>
<tr><td>Category:</td><td>{$getAll[$i]['category']}</td></tr>
<tr><td>
<tr><td><input type="hidden" id="titleId" name="titleId" value="{$getAll[$i]['titleId']}"> </tr><td>
<a href='edit.php?titleId={$getAll[$i]['titleId']}' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>
<a href='delete.php?titleId={$getAll[$i]['titleId']}' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>
</tr></td>
</tbody></table>
</div>
</div>
</div>
</div>
HTML;
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Title Add Modal-->
<div class="modal fade" id="facultyAddModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Title</h4>
</div>
<div class="modal-body">
<div id="adminResult" class="hide" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div id="resultAdminContent"></div>
</div>
<form class="cmxform" id="adminForm" method="post">
<label for="Activity">ActivityAttended (required)</label>
<input class="form-control" id="adminTitle" name="title" type="text" required>
<br>
<label for="units">Units (required)</label>
<input class="form-control" id="adminUnits" type="number" name="units" required>
<br>
<label for="Category">Category (Optional)</label>
<input class="form-control" id="adminCategory" type="text" name="category">
<br>
<?php echo
'<input type="hidden" id="addadminTypeId" value="'.$typeId.'">';
?>
<?php echo
'<input type="hidden" id="titleId" name="titleId" value="'.$titleId.'">';
?>
<button class="btn btn-info btn-primary" type="submit">Submit</button>
<br>
<br>
</form>
</div>
</div>
</div>
</div>
update.php
<?php
require_once 'functions/db_connection.php';
$conn = DB::databaseConnection();
$title = $units = $category = "";
if(isset($_POST["titleId"]) && !empty($_POST["titleId"])){
$titleId = $_POST['titleId'];
$sql = "UPDATE title SET title = :title, units = :units, category = :category WHERE titleId = :titleId";
if($stmt = $conn->prepare($sql))
{
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':titleId', $titleId);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':units', $units);
$stmt->bindParam(':category', $category);
if ($stmt->execute()) {
header("location: index.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
unset($stmt);
}
unset($conn);
} else{
if(isset($_GET["titleId"]) && !empty(trim($_GET["titleId"]))){
$titleId = trim($_GET["titleId"]);
$sql = "SELECT * FROM title WHERE titleId = :titleId";
if($stmt = $conn->prepare($sql))
{
$stmt->bindParam(':titleId', $titleId);
if ($stmt->execute()){
if($stmt->rowCount() == 1){
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// Retrieve individual field value
$title = $result["title"];
$units = $result["units"];
$category = $result["category"];
} else{
echo"error1";
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
unset($stmt);
unset($conn);
} else{
// URL doesn't contain id parameter. Redirect to error page
echo"error2";
exit();
}
}
?>
<!--<!DOCTYPE html>-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Update Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<label for="Activity">Title</label>
<input class="form-control" id="adminTitle" name="title" type="text" value="<?php echo $title; ?>" required>
<br>
<label for="units">Units (required)</label>
<input class="form-control" id="adminUnits" type="number" name="units" value="<?php echo $units; ?>" required>
<br>
<label for="Category">Category (Optional)</label>
<input class="form-control" id="adminCategory" type="text" value="<?php echo $category; ?>" name="category">
<br>
<input type="hidden" name="titleId" value="<?php echo $titleId; ?>">
<button class="btn btn-info btn-primary" type="submit">Submit</button>
<br>
<br>
</form>
</div>
</<div>
</div>
</div>
</div>
</body>
</html>
The only goal here is to get the update form working, the user should be able to update the records of the respective title being selected.
I don't know crud but I think there is a way to debug a little:
e.g. try this:
if(isset($_POST["titleId"]) && !empty($_POST["titleId"])){
// test if you are here:
echo 'hi, yeah I am here!';
}
or this
echo '<pre>';
var_dump($_POST);
echo '</pre>';
// before:
if(isset($_POST["titleId"]) && !empty($_POST["titleId"])){
// ...
}
also, take a look at
error_get_last()['message']

Mysql and PHP Update Problems

I am working on a simple Userprofile, where the user can update some data over Mysql. But it doesn´t update anything. Why?
<?php
if (isset($_GET['pageID'])) {
$id = $_GET['pageID'];
include("connector.php");
$stmt = $conn->prepare(" SELECT
cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
ad.street, ad.city, ad.postalcode,
wo.work_ID,
ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
FROM client cl
JOIN adress ad ON cl.clientID = ad.clientID
LEFT JOIN work wo ON cl.clientID = wo.clientID
LEFT JOIN hardware ha ON ad.adressID = ha.adressID
WHERE cl.clientID = '$id'");
$stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div class="container">
<div class="col-sm-12 col-md-12 col-xl-12">
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<img src="Userfotos/jedi.jpg" class="img-responsive"><br>
<button type="submit" name="submit" class="btn btn-info">edit Foto</button>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Lastname:</h4><?php echo $row['lastname']; ?></p>
<p><h4>Firstname:</h4> <?php echo $row['firstname']; ?></p>
<p><h4>Street</h4> <?php echo $row['street']; ?></p>
<p><h4>City</h4> <?php echo $row['city']; ?></p>
<p><h4>Postalcode</h4> <?php echo $row['postalcode']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editPrivateData">Edit data
</button>
<div id="editPrivateData" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Personal data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php?pageID=1" method="post">
<div class="form-group">
<label for="_firstname">Firstname</label>
<input type="text" class="form-control" id="_firstname" name="firstname"
value="<?php echo $row['firstname']; ?>">
</div>
<div class="form-group">
<label for="_lastname">Lastname</label>
<input type="text" class="form-control" id="_lastname" name="lastname"
value="<?php echo $row['lastname']; ?>">
</div>
<div class="form-group">
<label for="_street">Street</label>
<input type="text" class="form-control" id="_street" name="street"
value="<?php echo $row['street']; ?>">
</div>
<div class="form-group">
<label for="_city">City</label>
<input type="text" class="form-control" id="_city" name="city"
value="<?php echo $row['city']; ?>">
</div>
<div class="form-group">
<label for="_postalcode">Postalcode</label>
<input type="text" class="form-control" id="_postalcode" name="postalcode"
value="<?php echo $row['postalcode']; ?>">
</div>
<button type="submit" name="submitPrivate" class="btn btn-info">Save</button>
<?php
if (isset($_POST['submitHardware'])) {
try {
$id = $_GET['pageID'];
$stmt = $conn->prepare("UPDATE clients.client SET
firstname = :firstname,
lastname = :lastname,
mobilephone = :mobilephone,
phone = :phone
WHERE clientID = $id");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->execute([':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':mobilephone' => $_POST['mobilephone'],
':phone' => $_POST['phone']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
<p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
<p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
<p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
</button>
<!-- Modal -->
<div id="editHardware" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit hardware data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php?pageID=3" method="post">
<div class="form-group">
<label for="_hardware1">Hardware1</label>
<input type="text" class="form-control" id="_hardware" name="hardware1"
placeholder="<?php echo $row['hardware1']; ?>">
</div>
<div class="form-group">
<label for="serialnumber">Serialnumber1</label>
<input type="text" class="form-control" id="_serialnumber" name="serialnumber"
placeholder="<?php echo $row['serialnumber']; ?>">
</div>
<div class="form-group">
<label for="_hardware2">Hardware2</label>
<input type="text" class="form-control" id="_hardware2" name="hardware2"
placeholder="<?php echo $row['hardware2']; ?>">
</div>
<div class="form-group">
<label for="_serialnumber2">Serialnumber2</label>
<input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
placeholder="<?php echo $row['serialnumber2']; ?>">
</div>
<button type="submit" name="submitHardware" class="btn btn-info">Save</button>
<?php
if (isset($_POST['submitHardware'])) {
require("connector.php");
try {
$stmt = $conn->prepare("UPDATE clients.hardware SET
hardware1 = :hardware1,
serialnumber = :serialnumber,
hardware2 = :hardware2,
serialnumber2 = :serialnumber2,
WHERE hardware.clientID = '$id'");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':client_ID', $client_ID);
$stmt->execute([':hardware1' => $_POST['hardware1'],
':serialnumber' => $_POST['serialnumber'],
':hardware2' => $_POST['hardware2'],
':serialnumber2' => $_POST['seerialnumber2']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>ClientID:</h4><?php echo $row['client_ID']; ?></p>
<p><h4>WorkID:</h4><?php echo $row['work_ID']; ?></p>
<button type="submit" name="submit" class="btn btn-info">Edit data</button>
</div>
</div>
<?php endwhile; ?>
As you can see, there is a while-loop to fetch the data and display them into some fields. In Between this loop, i created two Bootstrap-Modals, where the user can edit something. if the user clicks on the "save" button, nothing happens. i really don`t understand where is the problem.
and another small point: the userprofile is generated dynamically with the clientID(pageID). how do i temporary save this pageID, so i can return to the current page after the form action method ="POST"
The clientID is the primary key in the database for the clients.client table. The client_ID is an ID the user has to set by himself, he can set whatever he want to. The PageID is linked to the clientID is for generating dynamical pages. That means: on another page there is a table which displays some basically informations about all users. If the logged in User clicks on a user from the table, there will be the userprofile dynamically generated.
your code is really weird. Please tell us what the page ID and client ID are. They are obviously not the same. But you use them as such. I reduced your code to just one modal and used hidden fields for client and page ID values.
<?php
// TODO: filter all input
if (isset($_POST['submitHardware']) && $_POST['pageID'] == 3) {
try {
include("connector.php");
$client_id = $_GET['clientID'];
$stmt = $conn->prepare("UPDATE clients.client SET
firstname = :firstname,
lastname = :lastname,
mobilephone = :mobilephone,
phone = :phone
WHERE clientID = $client_id");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->execute([':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':mobilephone' => $_POST['mobilephone'],
':phone' => $_POST['phone']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
if (isset($_GET['clientID'])) {
$client_id = $_GET['clientID']; // TODO: filter
include("connector.php");
$stmt = $conn->prepare(" SELECT
cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
ad.street, ad.city, ad.postalcode,
wo.work_ID,
ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
FROM client cl
JOIN adress ad ON cl.clientID = ad.clientID
LEFT JOIN work wo ON cl.clientID = wo.clientID
LEFT JOIN hardware ha ON ad.adressID = ha.adressID
WHERE cl.clientID = '$client_id'");
$stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div class="container">
<div class="col-sm-12 col-md-12 col-xl-12">
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<img src="Userfotos/jedi.jpg" class="img-responsive"><br>
<button type="submit" name="submit" class="btn btn-info">edit Foto</button>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
<p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
<p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
<p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
</button>
<!-- Modal -->
<div id="editHardware" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit hardware data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php" method="post">
<input type="hidden" name="pageID" value="3">
<input type="hidden" name="clientID" value="<?php echo $client_id; ?>">
<div class="form-group">
<label for="_hardware1">Hardware1</label>
<input type="text" class="form-control" id="_hardware" name="hardware1"
placeholder="<?php echo $row['hardware1']; ?>">
</div>
<div class="form-group">
<label for="serialnumber">Serialnumber1</label>
<input type="text" class="form-control" id="_serialnumber" name="serialnumber"
placeholder="<?php echo $row['serialnumber']; ?>">
</div>
<div class="form-group">
<label for="_hardware2">Hardware2</label>
<input type="text" class="form-control" id="_hardware2" name="hardware2"
placeholder="<?php echo $row['hardware2']; ?>">
</div>
<div class="form-group">
<label for="_serialnumber2">Serialnumber2</label>
<input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
placeholder="<?php echo $row['serialnumber2']; ?>">
</div>
<button type="submit" name="submitHardware" class="btn btn-info">Save</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>

PHP post request from HTML page showing blank input fields

Currently looking to implement functionality to edit details in MySQL database via a HTML page. The page itself shows all data in the database which matches the unique id of the user who is is logged in via a PHP session and echos that data to input boxes in a while loop.
When the user makes changes to the input text and hits the save changes link it then calls the edit endpoint which in turn calls the edit SQL function in a functions file.
I'm using an anchor tag wrapped in a button to send the id of the row that is being edited and all this sits inside a POST action form.
However the input texts are only showing as blank as if the endpoint is not receiving the text in the input field, and despite trying quite a few different methods I can't seem to get a result.
Code for Web page (not whole page but only concerned code)
<?php
$connect =mysqli_connect('localhost','root','','micaddy');
$id_query = mysqli_query($connect, "SELECT unique_id FROM users WHERE email = '{$_SESSION['login_user']}'");
$id_array = mysqli_fetch_assoc($id_query);
$uid = $id_array['unique_id'];
$result = mysqli_query($connect, "SELECT * FROM clubs WHERE user_id =
'$uid'");
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading clearfix"><h3 class="panel-title"><strong>Your Golfbag</strong><button type="button" class="btn btn-info btn-lg pull-right" data-toggle="modal" data-target="#addModal">Add Club</button></h3></div>
<?php while($row=mysqli_fetch_assoc($result)):?>
&nbsp <span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
<div class="panel-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-5">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title"><strong><?php echo $row['club_type'];?></strong></h3></div>
<div class="panel-body">
<form id="" method="POST" action="editClub.php">
<div class="form-group">
<label for="clubType">Club ID</label>
<input type="text" readonly="" class="form-control" id="inputClubType" value="<?php echo $row['id'];?>" name="clubIdInput">
</div>
<div class="form-group">
<label for="clubBrand">Type</label>
<input type="text" class="form-control" id="inputclubBrand" value="<?php echo $row['club_type'];?>" name="clubTypeInput">
</div>
<div class="form-group">
<label for="clubBrand">Brand</label>
<input type="text" class="form-control" id="inputclubBrand" value="<?php echo $row['brand'];?>" name="clubBrandInput">
</div>
<div class="form-group">
<label for="clubNum">Number or Type</label>
<input type="text" class="form-control" id="inputclubNum" value="<?php echo $row['club_number'];?>" name="clubNumInput">
</div>
<div id="deleteClub">
<button id="submitChange" type="button" class="btn btn-danger btn-lg"><?php echo "<a href='deleteClub.php?id=".$row['id']."'>Delete</a>" ?></button>
<button type="button" class="btn btn-info btn-lg"><?php echo "<a href='editClub.php?id=".$row['id']."'>Save Changes</a>" ?></button>
</div>
<span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
</form>
</div>
</div>
</div>
<div class="col-md-5">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title"><strong>Club Image</strong></h3></div>
<div class="panel-body">
<div class="form-group">
<img src="club_images/<?php echo $row['clubImg']; ?>" class="img-rounded" width="250px" height="250px" alt="Image"/>
</div>
</div>
</div>
</div>
</div>
</div>
<span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
</div>
<?php endwhile;?>
</div>
</div>
</div>
</div>
The edit endpoint:
<?php
session_start();
$error='';
require_once '../include/DB_Functions.php';
$db = new DB_Functions();
if(empty($_POST['clubBrandInput']) || empty($_POST['clubNumInput'])){
$_SESSION['message'] = "Warning: Some fields are blank! Please try again";
header("Location: golfbag.php");
} else{
if(isset($_POST['clubBrandInput']) && isset($_POST['clubTypeInput']) && isset($_POST['clubNumInput'])){
$brand = $_POST['clubBrandInput'];
$type = $_POST['clubTypeInput'];
$num = $_POST['clubNumInput'];
$id = $_GET['id'];
$club = $db->editclub($brand, $type, $num, $id);
if($club) {
header("Location: golfbag.php");
$_SESSION['message'] = "Success! Details edited.";
}else{
header("Location: golfbag.php");
echo $error;
}
}
}
?>
The function method:
public function editClub($brand, $type, $num, $id){
$stmt = $this->conn->prepare("UPDATE clubs SET brand = '$brand', club_type = '$type', club_number = '$num' WHERE id = '$id'");
$result = $stmt->execute();
$stmt->close();
if($result){
$stmt = $this->conn->prepare("SELECT * FROM clubs WHERE user_id = ?");
$stmt->bind_param("s", $uid);
$stmt->execute();
$club = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $club;
}else{
return false;
}
}
You do not have a <form> defined in this HTML.
You are also clicking an anchor link <button id="submitChange" type="button" class="btn btn-danger btn-lg"><?php echo "<a href='deleteClub.php?id=".$row['id']."'>Delete</a>" ?></button>
even though it is in a button.
Therefore you will only pass the id=".$row['id']." parameter to the endpoint and that will be passed in the $_GET array and not the $_POST array

How to disable <input "text"> if there is a value in column (inside moda)

Hi GoodMorning(here in philippines) :)
I want to disable the <input ="text"> if there is a value in the dedicated column
<?php
$stmt = $db_con->prepare("SELECT * FROM userinfo WHERE role='student' AND gradelvl='7' AND sectionname='studid' ORDER BY lrnno ASC");
$stmt->execute();
$row=$stmt->fetch(PDO::FETCH_ASSOC)
?>
<form action="71stgmodalaction.php" method="POST" class="form-horizontal">
<div id="1stgrade7" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="form-group">
<input type='text' name='id' value='studid'; ?>' />
<h2 class="modal-title"><div align="center">Please Fill Correctly</div></h2>
<div class="modal-header">
<h4 class="modal-title"><div align="center">1st Grading Result</div></h4>
</div>
<div class="modal-body"><div align="left">
<b>Filipino:</b><input type="text" class="resizedTextbox" name="c71stgfilipino" >
</div>
<div class="modal-footer">
<button type="submit" name="submit71st" class="btn btn-success">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</form>
Here is my action to update column
<?php
if($_POST)
{
$id = $_POST['id'];
$c7finalgfilipino = $_POST['c7finalgfilipino'];
$stmt = $db_con->prepare("UPDATE userinfo SET c7finalgfilipino=:qc7finalgfilipino WHERE id=:id");
$stmt->bindParam(":qc7finalgfilipino", $c7finalgfilipino);
$stmt->bindParam(":id", $id);
if($stmt->execute())
{
echo "Successfully updated";
}
else{
echo "Query Problem";
}
}
?>
How can I disable the input type="text" name="c71sthfilipino" if the is a value in that text ?
hope that will understand what Im saying, Im sorry for my english grammar :)
Thanks in advance
I believe what you are trying to do is JavaScript (JQuery for making it easier).
$(function(){
var elem = $('input[name="c71sthfilipino"]');
if ( !$(elem).val() ){
elem.prop('disabled', true);
}
})
You disable it like this
<input type="text"
class="resizedTextbox"
name="c71stgfilipino"
<?php echo isset($row['column']) ? 'disabled' : '' ?> >

Receiving server error from jQuery function when attempting to send information through to php file

Been attempting to send information using jQuery to a php file but keep getting a server error fault. I know there's not a fault with the php file because i was able to send information from the form to the php file directly and it was able to handle the information and execute the query fine.
I need to use jQuery because i want the response to be displayed on the current page instead of directing the user to the php file.
I have managed to do this before with other forms but I'm not sure why its not working this time.
The form code:
<div id="viewModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="viewModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">View Proposal: </h4>
</div>
<div class="modal-body">
<form id="viewForm">
<div class="form-group">
<label for="code" class="control-label">Proposal ID:</label>
<input type="text" class="form-control" id="proposal_id" name="proposal_id" readonly="">
</div>
<div class="form-group">
<label for="title" class="control-label">Proposal Title:</label>
<input type="text" class="form-control" id="proposal_title_id" name="proposal_title">
</div>
<div class="form-group">
<label for="title" class="control-label">Proposal Description:</label>
<input type="text" class="form-control" id="desc_id" name="description">
</div>
<div class="form-group">
<label for="title" class="control-label">Course:</label>
<input type="text" class="form-control" id="course_id" name="course_title">
</div>
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token']; ?>"/>
<div class="modal-footer">
<div class="btn-toolbar">
<button type="Submit" class="btn btn-success" class="pull-left" onclick="AddFav();"><span class= "glyphicon glyphicon-heart"/> Add To Favourites</button>
<button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
<input type="Submit" class= "btn btn-primary" value="Apply" />
</div>
</div>
</form>
</div>
</div>
</div>
</div>
the Add To Favourite button has the onclick function for the jQuery function.
this is the jQuery:
function AddFav() {
var data = $('#viewForm').serialize();
$.post('Student_Favourites.php', data, function(response){
$("#viewForm").html(response);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
the php:
<?php
session_start();
include "db_conx.php";
try
{
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// var_dump($_SESSION);
$username = $_SESSION['username'];
//$_SESSION['username'] = $username;
$sql = $db_conx->prepare("SELECT username, user_record_id FROM login_details
WHERE username = :username");
$sql->bindParam(':username', $username, PDO::PARAM_STR);
$sql->execute();
$user_record_id = $sql->fetchColumn(1);
// echo "Showing all proposals for : $username \n";
// print_r($user_record_id);
$proposal = $_POST['proposal_id'];
$insertRec = $db_conx->prepare("INSERT INTO student_saved (proposal_id, user_record_id) VALUES (:proposal, :user_record_id)");
$insertRec->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
$insertRec->bindParam(':proposal', $proposal, PDO::PARAM_STR);
$insertRec->execute();
$message = "<p class='text-success'> Proposal Added To Your Favourites <span class='glyphicon glyphicon-ok'/></p>";
}
catch(Exception $e)
{
/*** check if the email already exists ***/
if( $e->getCode() == 23000)
{
$message = 'This proposal has already been saved to your favourites';
}
else
{
$message = $e->getMessage();
/*** fail message incase something went wrong ***/
//$message = 'Unable to process your request. Please try again later';
//$message = $username ." ". $password ." ". $last_user_id;
}
}
}
die($message);
?>
any ideas how i can overcome this? the request failed: error message isn't very helpful

Categories