I'm a PHP learner. I have a website built on PHP. On the front end of one of our website pages there are four tabs. See the pic below for reference -
I want to add one more tab here. There should be a simple option of adding content in the admin panel for this tab (Which I have accomplished)
My problem is, when I am adding content to the text area in admin panel and hitting submit, the content disappears. I guess the content is not being stored in the database. I checked the SQL file made the possible changes. Although I'm not sure if I did it right. But I'm quite sure that there's a very small lag here which I am not able to figure out.
P.S. Check the screenshot of admin panel for more clarification about my issue.
The text area with heading Table of Contents - Simple is the one which I added. But as I mentioned, even if add content here and hit submit it becomes blank.
Below is the code of the page in screenshot -
"<?php
require_once '../config/config.php';
require_once '../class/dbclass.php';
require_once 'class/content.php';
require_once 'class/category.php';
require_once 'class/common.php';
require './isLogin.php';
$content = new Content();
$category = new Category();enter code here
$catList = $category->fetchCategoryTree();
//echo '<pre>';
//print_r($catList);
//echo '</pre>';
//exit;
//$catParentList = $category->fetchParentCategory(30);
//echo '<pre>';
//print_r($catParentList);
//echo '</pre>';
//echo implode("->",$catParentList);
//echo '<pre>';
//print_r(array_reverse($catParentList));
//echo '</pre>';
//exit;
$cat_id = 0;
$title = '';
$price = '';
$description = '';
$descriptions = '';
$author = $published = $report_code = '';
$content_id = isset($_REQUEST['id']) && $_REQUEST['id'] != "" ? $_REQUEST['id'] : 0;
//$category_list = $category->getAllCategory();
if ($content_id) {
$contentData = $content->getContentById($content_id);
$cat_id = isset($contentData[0]['cat_id']) ? $contentData[0]['cat_id'] : '';
$title = isset($contentData[0]['title']) ? $contentData[0]['title'] : '';
$price = isset($contentData[0]['price']) ? $contentData[0]['price'] : '';
$description = isset($contentData[0]['description']) ? $contentData[0]['description'] : '';
$descriptions = isset($contentData[0]['descriptions']) ? $contentData[0]['descriptions'] : '';
$report_code = isset($contentData[0]['report_code']) ? $contentData[0]['report_code'] : '';
$published = isset($contentData[0]['published']) ? $contentData[0]['published'] : '';
$author = isset($contentData[0]['author']) ? $contentData[0]['author'] : '';
}
if($content_id == 0)
$pageName = "Add Report";
else
$pageName = "Edit Report";
?>
<?php require_once './includes/header.php'; ?>
<?php require_once './includes/sidebar.php'; ?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
<b><?php echo $pageName; ?></b>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li class="active"><?php echo $pageName; ?></li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<?php if (isset($_SESSION['error_message']) && $_SESSION['error_message'] != ''): ?>
<div class="alert alert-danger alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="glyphicon glyphicon-remove"></i></button>
<?php echo $_SESSION['error_message']; ?>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['success_message']) && $_SESSION['success_message'] != ''): ?>
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="glyphicon glyphicon-remove"></i></button>
<?php echo $_SESSION['success_message']; ?>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['info_message']) && $_SESSION['info_message'] != ''): ?>
<div class="alert alert-info alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="glyphicon glyphicon-remove"></i></button>
<?php echo $_SESSION['info_message']; ?>
</div>
<?php endif; ?>
</div><!-- /.box-header -->
<!-- form start -->
<form id="content-form" action="models/contentProcess.php" enctype="multipart/form-data" method="post" novalidate>
<div class="box-body">
<input type="hidden" name="type" value="<?php echo ($content_id == 0) ? "addContent" : "editContent"; ?>">
<input type="hidden" name="content_id" id="content_id" value="<?php echo $content_id ?>">
<div class="form-group">
<label>Select Category</label>
<select id="cat_id" name="cat_id" class="form-control">
<?php foreach ($catList as $cl) { ?>
<option value="<?php echo $cl["id"] ?>" <?php echo ($cl["id"] == $cat_id) ? 'selected' : '' ; ?> ><?php echo $cl["name"]; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label>Title</label>
<input type="text" name="title" id="title" placeholder="Enter title" class="form-control" value="<?php echo $title ?>">
</div>
<div class="form-group">
<label>Report Code</label>
<input type="text" name="report_code" id="report_code" placeholder="Enter Report Code" class="form-control" value="<?php echo $report_code ?>">
</div>
<div class="form-group">
<label>Image</label>
<input type="file" class="btn btn-default btn-file" name="image" id="image">
</div>
<div class="form-group">
<label>Published</label>
<input type="text" name="published" id="published" placeholder="Enter published" class="form-control" value="<?php echo $published ?>">
</div>
<div class="form-group">
<label>Author</label>
<input type="text" name="author" id="author" placeholder="Enter author" class="form-control" value="<?php echo $author ?>">
</div>
<div class="form-group">
<label>Price</label>
<input type="text" name="price" id="price" placeholder="Enter price" class="form-control" value="<?php echo $price ?>">
</div>
<div class="form-group">
<label>Description</label>
<textarea id="description" name="description" class="form-control" rows="10" cols="80">
<?php echo $description; ?>
</textarea>
</div>
<div class="form-group">
<label>Table of Contents - Simple</label>
<textarea id="descriptions" name="descriptions" class="form-control" rows="15" cols="100">
<?php echo $descriptions; ?>
</textarea>
</div>
<button class="btn btn-primary" type="submit" id="submitform">Submit</button>
Cancel
</div>
</form>
</div><!-- /.box -->
</div><!--/.col (left) -->
<!-- right column -->
</div> <!-- /.row -->
</section><!-- /.content -->
</div><!-- /.content-wrapper -->
<?php require_once './includes/footer.php'; ?>
<script type="text/javascript" src="<?php echo ADMIN_ROOT; ?>plugins/ckeditor/ckeditor.js"></script>
<script language="javascript">
CKEDITOR.replace('descriptions',{
//uiColor:"#532F12",
toolbar: 'MyToolbar',
});
</script>
<script language="javascript">
CKEDITOR.replace('description',{
//uiColor:"#532F12",
toolbar: 'MyToolbar',
});
</script>"
Code snippet of reportDetail.php
<div class="tab-content" style="min-height:775px;">
<div id="menu1" class="tab-pane <?php if (!isset($_SESSION['user_id'])) { ?>active<?php } ?>" style="overflow: auto;<?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0) { ?>max-height: 1520px;<?php } else { ?> height: 795px;<?php } ?>">
<?= $contentData['description'] ?>
</div>
<div id="menu2" class="tab-pane fade <?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0) { ?>active in<?php } ?>" style="overflow: auto;<?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0) { ?>max-height: 1520px;<?php } else { ?> height: 795px;<?php } ?>">
<?= $contentData['descriptions'] ?>
</div>
<div id="menu3" class="tab-pane fade <?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0) { ?>active in1<?php } ?>" >
html:-
<form action="" method="post">
<textarea name="textarea" rows="10" cols="50"></textarea>
<input type="submit" name="submit">
</form>
php:-
if(isset($_POST["submit"]))
{
$yourtextarea= $_POST["textarea"];
}
Related
Ive built a basket/checkout system for an e-commerce site, and have used session variables to store product name, price and quantity and can have them be displayed on my checkout page.
I'm struggling to be able to add different products to the basket, when the user goes to another product and clicks the add to basket button it removes the first product from the basket. does anyone know how I could change my code so that different products can be added to the basket without removing any products that are currently in the variable. and also how could I add the Quantity to the URL so that I can add it to the basket. the quantity is an input box on the products page that the user can tryp the number of products into.
this is my products page -
<?php
require('includes/application_top.php');
$page_title='Details';
require('includes/site_header.php');
$_GET['prod_ID'];
$product_id = isset($_REQUEST['prod_ID']) ? (int) $_REQUEST['prod_ID'] : 0;
$pound ="£";
?>
<style>
<?php
require('css/prod_details.css');
?>
</style>
<br>
<br>
<br>
<br>
<br>
<?php $product = get_product_details1($freshKickz_conn); ?>
<?php foreach($product as $productdetails) {
?>
<main class="container">
<!-- Left Column / Headphones Image -->
<div class="left-column">
<img data-image="red" class="active" src="<?= htmlspecialchars($productdetails['images']) ?>" style="height: 400px; width: 400px;" alt="">
</div>
<!-- Right Column -->
<div class="right-column">
<!-- Product Description -->
<div class="product-description">
<h1><?= htmlspecialchars($productdetails['prod_Name']) ?></h1>
<p><?= htmlspecialchars($productdetails['prod_Details']) ?></p>
</div>
<!-- Product Pricing -->
<div class="product-price">
<span><?=$pound?><?= htmlspecialchars($productdetails['prod_Price'])?></span>
Add to Basket
</div>
<br>
<div class="quantity">
<span>Quantity</span>
<br>
<input type="number" min="1" max="9" step="1" value="1">
</div>
</div>
</main>
<?php
} ?>
<script src="js/prodJS.js"></script>
<script>
$(document).ready(function () {
$('.color-choose input').on('click', function () {
var headphonesColor = $(this).attr('data-image');
$('.active').removeClass('active');
$('.left-column img[data-image = ' + headphonesColor + ']').addClass('active');
$(this).addClass('active');
});
});
</script>
<?php
require('includes/application_bottom.php');
require('includes/site_footer.php');
?>
and this is my basket/checkout page -
<?php
require('includes/application_top.php');
$page_title='Your Basket';
require('includes/site_header.php');
if ( ! isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
}
$pound ="£";
$action = $_REQUEST['action'] ?? '';
$err = '';
if($_REQUEST['action']=='add_product'){
$_SESSION['basket'][$_REQUEST['name']]=$_REQUEST['quantity']=$_REQUEST['quantity'];
}
?>
<style>
<?php
require('css/basket.css');
?>
</style>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<div class="row">
<div class="col-75">
<div class="container">
<form action="successful_order.php">
<div class="row">
<div class="col-50">
<h3>Billing Address</h3>
<label for="fname"><i class="fa fa-user"></i> Full Name</label>
<input type="text" id="fname" name="firstname" placeholder="John M. Doe">
<label for="email"><i class="fa fa-envelope"></i> Email</label>
<input type="text" id="email" name="email" placeholder="john#example.com">
<label for="adr"><i class="fa fa-address-card-o"></i> Address</label>
<input type="text" id="adr" name="address" placeholder="542 W. 15th Street">
<label for="city"><i class="fa fa-institution"></i> City</label>
<input type="text" id="city" name="city" placeholder="New York">
<div class="row">
<div class="col-50">
<label for="state">County</label>
<input type="text" id="county" name="county" placeholder="Cheshire">
</div>
<div class="col-50">
<label for="zip">PostCode</label>
<input type="text" id="postcode" name="postcode" placeholder="SK11 6TF">
</div>
</div>
</div>
<div class="col-50">
<h3>Payment</h3>
<label for="fname">Accepted Cards</label>
<div class="icon-container">
<i class="fa fa-cc-visa" style="color:navy;"></i>
<i class="fa fa-cc-amex" style="color:blue;"></i>
<i class="fa fa-cc-mastercard" style="color:red;"></i>
<i class="fa fa-cc-discover" style="color:orange;"></i>
</div>
<label for="cname">Name on Card</label>
<input type="text" id="cname" name="cardname" placeholder="John More Doe">
<label for="ccnum">Credit card number</label>
<input type="text" id="ccnum" name="cardnumber" placeholder="1111-2222-3333-4444">
<label for="expmonth">Exp Month</label>
<input type="text" id="expmonth" name="expmonth" placeholder="September">
<div class="row">
<div class="col-50">
<label for="expyear">Exp Year</label>
<input type="text" id="expyear" name="expyear" placeholder="2018">
</div>
<div class="col-50">
<label for="cvv">CVV</label>
<input type="text" id="cvv" name="cvv" placeholder="352">
</div>
</div>
</div>
</div>
<label>
<input type="checkbox" checked="checked" name="sameadr"> Shipping address same as billing
</label>
<input type="submit" value="Checkout" class="btn">
</form>
</div>
</div>
<div class="col-25">
<div class="container">
<h4>Cart
<span class="price" style="color:black">
<i class="fa fa-shopping-cart"></i>
</span>
</h4>
<br>
<br>
<p><span class="name"><?= $_REQUEST['name'] ?></span> <span class="price"><span class="name"><?= $_REQUEST['quantity'] ?><br></span><?= $pound ?><?= $_REQUEST['price'] ?></span></p>
<hr>
<p>Total <span class="price" style="color:black"><b>$30</b></span></p>
</div>
</div>
</div>
<?php
require('includes/application_bottom.php');
require('includes/site_footer.php');
?>
First of all you should avoid using GET request to perform an action.
Next try to use product id because name can be same for different products.
So my offer for product page:
<?php
require('includes/application_top.php');
$page_title='Details';
require('includes/site_header.php');
$product_id = isset($_REQUEST['prod_ID']) ? (int) $_REQUEST['prod_ID'] : 0;
$pound ="£";
?>
<style>
<?php
require('css/prod_details.css');
?>
</style>
<br>
<br>
<br>
<br>
<br>
<?php $product = get_product_details1($freshKickz_conn); ?>
<?php foreach($product as $productdetails): ?>
<main class="container">
<!-- Left Column / Headphones Image -->
<div class="left-column">
<img data-image="red" class="active" src="<?= htmlspecialchars($productdetails['images']) ?>" style="height: 400px; width: 400px;" alt="">
</div>
<!-- Right Column -->
<div class="right-column">
<!-- Product Description -->
<div class="product-description">
<h1><?= htmlspecialchars($productdetails['prod_Name']) ?></h1>
<p><?= htmlspecialchars($productdetails['prod_Details']) ?></p>
</div>
<!-- Product Pricing -->
<form action="basket_page.php" method="POST">
<div class="product-price">
<span><?=$pound?><?= htmlspecialchars($productdetails['prod_Price'])?></span>
<input type="hidden" name="action" value="add_product" />
<!-- i'm not sure what index did you use for id so -->
<input type="hidden" name="id" value="<?php echo htmlspecialchars($productdetails['prod_Name']); ?>" />
<input type="hidden" name="price" value="<?php echo htmlspecialchars($productdetails['prod_Price']); ?>" />
<input type="hidden" name="name" value="<?php echo htmlspecialchars($productdetails['prod_Name']); ?>" />
<button type="submit" class="cart-btn">Add to Basket</button>
</div>
<br>
<div class="quantity">
<span>Quantity</span>
<br>
<input type="number" min="1" max="9" step="1" value="1">
</div>
</form>
</div>
</main>
<?php endforeach; ?>
<script src="js/prodJS.js"></script>
<script>
$(document).ready(function () {
$('.color-choose input').on('click', function () {
var headphonesColor = $(this).attr('data-image');
$('.active').removeClass('active');
$('.left-column img[data-image = ' + headphonesColor + ']').addClass('active');
$(this).addClass('active');
});
});
</script>
<?php
require('includes/application_bottom.php');
require('includes/site_footer.php');
?>
Next you can use associative array for your session. Don't forget to check all user input. It is important.
<?php
require('includes/application_top.php');
$page_title = 'Your Basket';
require('includes/site_header.php');
if (!isset($_SESSION['basket'])) {
$_SESSION['basket'] = [];
}
$pound = "£";
$action = $_REQUEST['action'] ?? '';
$err = '';
if ($action === 'add_product') {
$id = isset($_POST['id']) ? (int) $_POST['id'] : null;
$price = isset($_POST['price']) ? (float) $_POST['price'] : null;
$number = isset($_POST['number']) ? (int) $_POST['number'] : null;
$name = isset($_POST['name']) ? trim($_POST['name']) : null;
if ($id && $price && $number) {
$_SESSION['basket'][$id] = [
'id' => $id,
'price' => $price,
'number' => $number,
'name' => $name,
];
}
}
?>
Next you have an error in rendering. You should use your session array. Not a raw input.
<div class="col-25">
<div class="container">
<h4>
Cart
<span class="price" style="color:black">
<i class="fa fa-shopping-cart"></i>
</span>
</h4>
<br>
<br>
<?php
$total = 0;
foreach ($_SESSION['basket'] as $item):
$total += $item['price'] * $item['number'];
?>
<p>
<span class="name"><?php echo htmlspecialchars($item['name']) ?></span>
<span class="price">
<span class="name"><?= $item['number'] ?><br></span>
<?= $pound ?><?= $item['price'] ?>
</span>
</p>
<hr>
<?php endforeach; ?>
<p>
Total <span class="price" style="color:black"><b>$<?= total ?></b></span>
</p>
</div>
</div>
And finally please take a look at specialized php scripts for ecommerce like WooCommerce or Magento.
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']
Whenever I try to add additional code for additional column, I still got the error of undefined index, although I got it once..The second try was a fail, and it took me a really long time to figure this one out..I did add the column in the database first..For instance, I need to add the keyword column.
<?php
require 'database.php';
if (isset($_GET)) {
// keep track validation errors
$titleError = null;
$authorError = null;
$date_createdError = null;
$abstractError = null;
// keep track post values
$title = $_POST['title'];
$author = $_POST['author'];
$date_created = $_POST['date_created'];
$abstract = $_POST['abstract'];
// validate input
$valid = true;
if (empty($title)) {
$titleError = 'Please enter title';
$valid = false;
}
if (empty($author)) {
$authorError = 'Please enter author name(s)';
$valid = false;
}
if (empty($date_created)) {
$date_createdError = 'Please enter the Date';
$valid = false;
}
if (empty($abstract)) {
$abstractError = 'Please enter the research study abstract';
$valid = false;
}
// insert data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO archives (title,author,date_created,abstract) values(?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($title,$author,$date_created,$abstract));
Database::disconnect();
header("Location: main.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="span10 offset1">
<div class="row">
<h3>Add New Reserch Study</h3>
</div>
<form class="form-horizontal" action="create.php" method="post">
<div class="control-group <?php echo !empty($titleError)?'error':'';?>">
<label class="control-label">Title</label>
<div class="controls">
<span class="resizable-input">
<input name="title" type="text" id="inp" placeholder="Copy & paste complete research title here" value="<?php echo !empty($title)?$title:'';?>">
</span>
<?php if (!empty($titleError)): ?>
<span class="help-inline"><?php echo $titleError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($authorError)?'error':'';?>">
<label class="control-label">Author(s)</label>
<div class="controls">
<span class="resizable-input">
<input name="author" type="text" id="inp" placeholder="Copy & paste complete name(s) here..." value="<?php echo !empty($author)?$author:'';?>">
</span>
<?php if (!empty($authorError)): ?>
<span class="help-inline"><?php echo $authorError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($date_createdError)?'error':'';?>">
<label class="control-label">Date Received</label>
<div class="controls">
<input name="date_created" type="date" id="inp" placeholder="YYYY-MM-DD format only" value="<?php echo !empty($date_created)?$date_created:'';?>">
<?php if (!empty($date_createdError)): ?>
<span class="help-inline"><?php echo $date_createdError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($abstractError)?'error':'';?>">
<label class="control-label">Abstract</label>
<div class="controls">
<span class="resizable-input">
<input name="abstract" maxlength="2000" type="text" placeholder="Copy & paste the abstract here.." id="inp" value="<?php echo !empty($abstract)?$abstract:'';?>">
</span>
<?php if (!empty($abstractError)): ?>
<span class="help-inline"><?php echo $abstractError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Create</button>
<a class="btn" href="main.php">Back</a>
Sign Out
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>
I am redirecting a user to a page named "forgot_pass" in such a way
"forgot_pass.php?code='$code'&username='$hidden_username'>".
But when user clicks on link for redirection the url seems like this
http://localhost/Validation/forgot_pass.php?code=
and there is no any page displayed.
The variables passed in url have values as i have already checked it. But they are not displaying when sent in url.
Help me in solving this issue.
reset.php
<?php
ini_set("display_errors", TRUE);
require_once './include/db_connection.php';
$pass = $_POST['pass'];
$pass1 = $_POST['pass1'];
$code = $_GET['code'];
$hidden_username = $_POST['username'];
if($pass == $pass1)
{
echo 'Password Changed !';
}
else
{
echo "Passowrd must match
<a href='forgot_pass.php?code='$code'&username='$hidden_username'>Try Again</a>
";
}
forgot_pass.php
<?php
ini_set("display_errors", TRUE);
require_once './include/db_connection.php';
if(isset($_GET['code']))
{
$get_code = (isset($_GET['code'])? $_GET['code'] : null);
$get_username =(isset($_GET['username']) ? $_GET['username'] : null);
$match_code = mysqli_query($link, "select * from signup where username='$get_username'");
if(mysqli_num_rows($match_code) > 0)
{
while($row = mysqli_fetch_assoc($match_code))
{
$db_username = $row['username'];
$db_code = $row['paareset'];
}
}
if($get_username == $db_username && $get_code == $db_code)
{ ?>
<html>
<head>
<meta charset="UTF-8">
<title>Change Password</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="text-center">
<h3><i class="fa fa-pencil fa-4x"></i></h3>
<h2 class="text-center">New Password?</h2>
<div class="panel-body">
<form class="form" method="post"
action= "reset_pass.php?code=<?php echo $get_code ?>"
<fieldset>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil color-blue"></i></span>
<input name="pass" placeholder="New Password" class="form-control" type="password" required="">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil color-blue"></i></span>
<input name="pass1" placeholder="Re-type Password" class="form-control" type="password" required="">
<input type="hidden" name="username" value="<?php echo $db_username ?>">
</div>
</div>
<div class="form-group">
<input class="btn btn-lg btn-primary btn-block" name="send" value="Change Password" type="submit">
</div>
<div class="form-group">
<span style="color: red"><?php if(isset($message['mail'])) {echo $message['mail']; } ?></span>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
}
} // End if (isset['code'])
if(!isset($_GET['code']))
{
?>
<html>
<head>
</head>
<body>
// Here i am displaying another form that gets email address and sends email
</body>
</html>
<?php
}
Remove single-quotes around the arguments, you are using them to delimit the URL so don't use them inside the URL (or at least escape them) :
echo "Password must match <a href='forgot_pass.php?code=$code&username=$hidden_username'>Try Again</a>";
Try with this way
echo "Passowrd must match
Try Again"
you have mismatch in columns
try to change your code to
echo "Passowrd must match
<a href='forgot_pass.php?code=".$code."&username=".$hidden_username".'>Try Again</a>
"
Please check if your filenames are correct.
You named: reset.php and forgot_pass.php
In forgot_pass.php your action goes towards reset_pass.php
remove single quote
"forgot_pass.php?code=$code&username=$hidden_username>"
I run my php code to upload multiple file but until right now i get error like this :
PHP erorr Undefined index: link_to_doc_file_
And here is my controller :
function insert_data(){
$this->form_validation->set_rules('tool_code', 'Tool Code', 'required');
if ($this->form_validation->run()){
for($i=1;$i<=10;$i++) {
$link_to_doc_file_[$i] = (isset($_GET['link_to_doc_file_'][$i]) ? $_GET['link_to_doc_file_'][$i] : null);
$link_to_doc_file_[$i] = "";
$file_uploaded = false;
//UPLOAD DOC
if(is_uploaded_file($_FILES['link_to_doc_file_']['tmp_name'][$i])){
$folder = /*base_url() .*/ 'material/';
$ext = substr($_FILES['link_to_doc_file_']['name'][$i],strpos($_FILES['link_to_doc_file']['name'][$i],'.'));
$file_name = $this->input->post('tool_code') . (int)microtime(true) . $ext;
$target_file = $folder .$file_name;
if(!file_exists($folder))
mkdir($folder);
//if(file_exists($target_file)) unlink($target_file);
if(move_uploaded_file($_FILES['link_to_doc_file_']["tmp_name"][$i], $target_file)){
$file_uploaded = true;
$$link_to_doc_file_[$i] = $target_file;
}
}
}
$tool_code = $this->input->post($tool_code);
$data_doc = null;
//Data Description
for($i=1;$i<=10;$i++){
$data_doc['title'][$i-1] = $this->input->post('doc_title_' . $i);
$data_doc['link'][$i-1] = $this->input->post('link_to_doc_file_'.$i);
$data_doc['show'][$i-1] = $this->input->post('doc_display_' . $i);
}
$data['doc'] = $data_doc;
if($this->input->post('is_edit')){
//UPDATE
$this->model_tool->update_tool_document($this, $data);
}else{
//INSERT
$this->model_tool->insert_tool_document($this, $data);
}
redirect(MENU_TOOL_DOC);
}else{
}
}
}
Here my View :
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<?php echo (($id)?"Edit":"Add") . " Document"; ?>
</div>
<div class="panel-body">
<?php echo form_open_multipart(base_url() . MENU_TOOL_DOC . '/insert_data');?>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="hidden" name="tool_code" value="<?php echo $tool_code;?>">
<label>Tool Code : <?php echo $tool_name;?></label>
<?php
for($i=1;$i<=10;$i++){
?>
<div class="form-group">
<label>Document <?php echo $i;?></label>
<div class="form-group">
<label>Show</label>
<select class="form-control" name="doc_show_<?php echo $i;?>">
<?php
for($j=0;$j<count($show);$j++){
?>
<option value="<?php echo ($j); ?>" <?php echo (isset($row))?(($doc['show'][$i-1] == $j)?"selected":""):"";?>>
<?php echo $show[$j];?>
</option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label>Title</label>
<input class="form-control" name="doc_title_<?php echo $i;?>" value="<?php echo (isset($row))?$doc['title'][$i-1]:"";?>">
</div>
<div class="form-group form-inline">
<label>File</label>
<input type="file" name="link_to_doc_file_<?php echo $i;?>" value="<?php echo (isset($row))?$doc['link'][$i-1]:"";?>">
</div>
</div>
<?php
}
?>
</div>
<div class="col-lg-12">
<input type="hidden" name="is_edit" value="<?php echo $is_edit; ?>" />
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
</div>
<?php echo form_close();?>
<!-- /.row (nested) -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
if only one upload i can but if many uploads file not yet until right now.
After your ist update, you are using an incremental variable in form input name.
<input type="file" name="link_to_doc_file_<?php echo $i;?>">
Now, you are getting undefined index array because you are defining index as:
link_to_doc_file_
This should be defined as:
$_FILES['link_to_doc_file_'.$i]
More explanation:
link_to_doc_file_ != link_to_doc_file_1
Side Note:
Also check this variable $_GET['link_to_doc_file_'][$i], you need to change it also.
You're looking for $FILES['link_to_doc_file'] but you're setting it with an integer and the end:
name="link_to_doc_file_<?php echo $i;?>"
in your upload script you have to look for that same integer
if(is_uploaded_file($_FILES['link_to_doc_file_$some_integer']['tmp_name'][$i])){