I have a form where I enter a username and it gets the users information. after calling for the users information I have another form wtih a checkbox for "banned" where I can ban or unban the user. But how can I store the entered username from the first form, to later use it in the second form where I check the banned checkbox?
code:
<form action="" method="post" id="msform">
<input type="text" name="datausername" placeholder="Username" maxlength="64" readonly onfocus="this.removeAttribute('readonly');"/>
<input type="submit" name="userdata" value="Get Data" class="databutton">
</form>
<?php
if(isset($_POST['userdata'])){
$datausername = $_POST['datausername'];
$sql = "SELECT * FROM users WHERE username = '$datausername'";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '
<div class="container" style="width:100%;">
<div class="flag note note--secondary">
<div class="flag__body note__text" style="width:100%;">
<h1>Personal Information</h1>
ID : <div class="fr">'.$row['id'].'</div><br />
E-mail: <div class="fr">'.$row['email'].'</div><br />
Username: <div class="fr">'.$row['username'].'</div><br />
Firstname: <div class="fr">'.$row['firstname'].'</div><br />
Lastname: <div class="fr">'.$row['lastname'].'</div><br />
<br />
Activated: <div class="fr">'.$row['active'].'</div><br />
Banned: <div class="fr">'.$row['banned'].'</div><br />
<br />
<h1>Data Information</h1>
Title: <div class="fr">'.$row['title'].'</div><br />
Rank: <div class="fr">'.$row['rank'].'</div><br />
Level: <div class="fr">'.$row['level'].'</div><br />
Exp: <div class="fr">'.$row['exp'].'</div><br />
<br />
<h1>Forum / Profile stats</h1>
Forum Posts: <div class="fr">0</div><br />
Forum Threaths: <div class="fr">0</div><br />
Comments: <div class="fr">0</div><br />
Awards: <div class="fr">0</div><br />
Friends: <div class="fr">0</div><br />
Followers: <div class="fr">0</div><br />
<br />
<h1>Security Information</h1>
IP: <div class="fr">'.$row['ip'].'</div><br />
Last IP: <div class="fr">'.$row['active_ip'].'</div><br />
Secret Question: <div class="fr">'.$row['question'].'</div><br />
Timestamp: <div class="fr">'.$row['timestamp'].'</div><br />
Activate Link: <div class="fr">link</div><br />
<br />
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>';
if($title == 'Head Admin' || $title == 'Admin'){
if($row['banned'] == '1'){
$checkbox = '<label><input type="checkbox" name="confirm" checked/></label>';
} else {
$checkbox = '<label><input type="checkbox" name="confirm" /></label>';
}
echo '
<div class="container" style="margin-top:20px; width:100%;">
<div class="flag note note--secondary">
<div class="flag__body note__text" style="width:100%;">
<h1>Settings</h1>
<form method="post" action="" id="msform" style="text-align:left;">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
</form>
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>
';
}
}
} else {
echo '
<div class="container" style="margin:0 auto; width:100%;">
<div class="flag note note--secondary">
<div class="flag__image note__icon">
<i class="fa fa-user"></i>
</div>
<div class="flag__body note__text">
User not found!
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>';
}
}
$db->close();
?>
This is the second form where I want to have the username stored to re use i to update the "banned" checkmark:
if($title == 'Head Admin' || $title == 'Admin'){
if($row['banned'] == '1'){
$checkbox = '<label><input type="checkbox" name="confirm" checked/></label>';
} else {
$checkbox = '<label><input type="checkbox" name="confirm" /></label>';
}
echo '
<div class="container" style="margin-top:20px; width:100%;">
<div class="flag note note--secondary">
<div class="flag__body note__text" style="width:100%;">
<h1>Settings</h1>
<form method="post" action="" id="msform" style="text-align:left;">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
</form>
</div>
<a href="#" class="note__close">
<i class="fa fa-times"></i>
</a>
</div>
</div>
PS: title in
if($title == 'Head Admin' || $title == 'Admin'){
Is your logged in title, so this will only be an option if you are either Head Admin or Admin
You can create a hidden text field to store it, then pass it via the POST data to the next stage:
(instead of using datausername here, you should be using the row ID returned from your SQL query)
<form method="post" action="" id="msform" style="text-align:left;">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
<input type="hidden" name="username" value="<?=$datausername?>">
</form>
Then in the next stage $_POST['datausername'] will exist.
OR
Use sessions (the better solution to be honest, less prone to hacking)
For example, you can start a new session, shove some data into it and unset the session after your finished with it.
In your index.php / config.php / whatever
session_start();
In your php file:
$datausername = $_POST['datausername'];
$_SESSION['customdata']['username'] = $datausername;
if($title == 'Head Admin' || $title == 'Admin') {
if ( isset($_SESSION['customdata']) ) {
$username = $_SESSION['customdata']['userid'];
//ban the user here, update SQL etc.
//Unset the session variable
unset($_SESSION['customdata'];
}
}
In your form add a hidden field to contain the id of the user. You can then use that as a key for the update query in the second form
<form method="post" action="" id="msform" style="text-align:left;">
<input type="hidden" name="key" value="<?php echo $row['id']; ?>">
Ban: <div class="fr">'.$checkbox.'</div>
<div style="padding:10px;"></div>
<input type="submit" name="dataset" value="Update" class="databutton">
</form>
Alternativley use the session
In form one add thsi at the top of the script
<?php
session_start();
then load a value ito the session
$_SESSION['Users_id'] = $row['id'];
Now in the second form you will use that value in the UPDATE
<?php
session_start();
if ( isset($_SESSION['users_id']) ) {
$key = $_SESSION['users_id'];
} else {
// we really should not be in this form
header('Location: somewhere_else.php');
}
Related
Hi guys please help me out, I have this php code where I get data from the database and displays on the web and if you check the input hidden tag below I gave it a value but instead not all the values are displaying properly in it. Instead it's creating an attribute.
PHP CODE
<?php
$output = '';
$sqlNO = "SELECT * FROM pnewoffer";
$NOresult = mysqli_query($conn, $sqlNO);
if(mysqli_num_rows($NOresult) > 0) {
while($row = mysqli_fetch_array($NOresult)) {
$output .= '<div class="offer-card">
<form action="./bked/savedit.php" method="post">
<a href="#" class="offer-card-inner">
<div class="offer-img">
<img src="./image/'.$row['offerPImage'].'" alt="New Offer Image" width="200">
</div>
<div class="offer-info">
<h3 class="offer-title">'.$row['offerPName'].'</h3>
<div class="clearfix rating marT8 ">
<div class="rating-stars ">
<div class="grey-stars"></div>
<div class="filled-stars" style="width:60.0%"></div>
</div>
</div>
<h4 class="offer-product-price">N'.$row['offerPPrice'].' </h4>
</div>
</a>
<input type="hidden" name="newPPrice" value='.$row['offerPPrice'].'>
<input type="hidden" name="newPName" value='.$row['offerPName'].'>
<input type="hidden" name="newPImg" value='.$row['offerPImage'].'>
<div class="offer-bt-btn">
<div class="offer-btn">
<button type="submit" name="saveForLater" class="favorite-offer-btn">
<i class="far fa-heart"></i>
</button>
<a href="#" class="offer-product-info-btn">
<i class="fa fa-info"></i>
</a>
</div>
</div>
</form>
</div>';
}
}
echo $output;
?>
RESULT
<input type="hidden" name="newPName" value="Blue" louis="" vuitton="" women="" bag="">
<input type="hidden" name="newPName" value="Samsung" galaxy="" s9="" 6gb="" ram,="" 32gb="" rom,="" 16mpbs="">
its actually supposed to be
<input type="hidden" name="newPName" value="Samsung Galaxy S9 6gb ram, 32gb rom, 16mpbs">
<input type="hidden" name="newPName" value="Blue Louis Vuitton Women Bag">
Please, how do I go about it?
If any of the value contain ' characters, that will end the value attribute and start a new attribute. Use htmlspecialchars() to encode it and prevent this.
<input type="hidden" name="newPPrice" value='.htmlspecialchars($row['offerPPrice'], ENT_QUOTES).'>
<input type="hidden" name="newPName" value='.htmlspecialchars($row['offerPName'], ENT_QUOTES).'>
<input type="hidden" name="newPImg" value='.htmlspecialchars($row['offerPImage'], ENT_QUOTES).'>
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']
I'm Working on a Webshop for a School Project. The website is done and works as intended. Except for the Search function, which works but does something weird.
This is the Code from my Search Page
<?php
$stmt = $auth_user->runQuery("SELECT * FROM customer WHERE id=:id");
$stmt->execute(array(":id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if(isset($_POST['btn-offer']))
{
try
{
$auth_user->redirect('offer.php');
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
<form method="post" class="form-signin">
<div class="form-group">
<input type="text" class="form-control" name="search" placeholder="Enter an Item"/> <br />
<button type="Search" name="btn-search">
<i class="glyphicon glyphicon-open-file"></i> Search
</button>
<hr />
</div>
<?php
if(isset($error)){
foreach($error as $error){
?>
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?>
</div>
<?php
}
}
else if(isset($_GET['search'])){
//$search_term = $_SESSION['search_term'];
$stmt = $user_req->runQuery("SELECT * FROM requests WHERE item LIKE :search");
$stmt->bindValue(":search","%".$_SESSION['search_term']."%");
$stmt->execute();
while($userReq=$stmt->fetch(PDO::FETCH_ASSOC)){
?><h6> Request ID </h6><?php echo($userReq['id']); ?> <br /><br /> <?php
?><h6> Requested Item </h6><?php echo($userReq['Item']); ?> <br /><br /> <?php
?><h6> Requested Price </h6><?php echo($userReq['price']); ?> <br /><br /> <?php
?><h6> Requested Quantity </h6><?php echo($userReq['quantity']); ?> <br /><br /> <?php
?><h6> Subject </h6><?php echo($userReq['subject']); ?> <br /><br /> <?php
?><h6> Description </h6><?php echo($userReq['descr']);
?>
<br />
<form action="offer.php" method="post" class="form-signin">
<input type="hidden" class="form-control" name="offer_id" value="<?php echo htmlspecialchars($userReq['id']); ?>"/> <br />
<input type="hidden" class="form-control" name="offer_item" value="<?php echo htmlspecialchars($userReq['Item']); ?>"/> <br />
<button type="Search" name="btn-offer">
<i class="glyphicon glyphicon-open-file"></i> Create Offer
</button>
</form>
<form action="all_offers.php" method="post" class="form-signin">
<input type="hidden" class="form-control" name="offer_id" value="<?php echo htmlspecialchars($userReq['id']); ?>"/> <br />
<button type="Search" name="btn-show">
<i class="glyphicon glyphicon-open-file"></i> Show Offers
</button>
</form>
<hr />
<?php
}
?>
<?php
}
?>
<br />
</form>
It displays everything as desired (all entries containing the search input).
But if i click on Create Offer, the hidden Input Types gets passed to my Search function just once. It wont update it with the new hidden variables if i click on another create offer button.
Offer.php file
<?php
if(isset($_POST['offer_id']))
{
$_SESSION['off_rid'] = $_POST['offer_id'];
$_SESSION['offer_item'] = $_POST['offer_item'];
}
?>
<h2 class="form-signin-heading">Create Offer for Request ID <?php echo htmlspecialchars($_SESSION['off_rid']); ?></h2><hr />
<form method="post" class="form-signin">
<input type="hidden" class="form-control" name="off_rid" value="<?php echo htmlspecialchars($_SESSION['off_rid']); ?>"/> <br />
<div class="form-group">
<label>Requested Item</label>
<input type="text" class="form-control" name="off_item" placeholder="<?php echo htmlspecialchars($_SESSION['offer_item']); ?>" value="<?php if(isset($error)){echo htmlspecialchars($_SESSION['offer_item']);}?>" readonly/>
</div>
<div class="form-group">
<input type="number" class="form-control" name="off_price" placeholder="Enter a Price" value="<?php if(isset($error)){echo $off_price;}?>" />
</div>
<div class="form-group">
<input type="number" class="form-control" name="off_quant" placeholder="Enter the Quantity" value="<?php if(isset($error)){echo $off_quant;}?>" />
</div>
<hr />
<div class="form-group">
<button type="submit" name="btn-createoffer">
<i class="glyphicon glyphicon-open-file"></i> Post Offer
</button>
</div>
<?php
if(isset($error)){
foreach($error as $error){
?>
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?>
</div>
<?php
}
}
else if(isset($_GET['posted'])){
?>
<div class="alert alert-info">
<i class="glyphicon glyphicon-log-in"></i> Successfully submitted the Offer!
</div>
<?php
}
?>
<br />
</form>
If i search for every entry containing "ni" i get all the entries, the hidden Inputtypes are also correct.
If i click on create offer it should write the id and the item on the redirected Page but it only works once. if i go back and search another item it still displays the info from the first item i created an offer.
Can anybody point out what i'm doing wrong. I'm stuck on this Problem since a few days and just cant figure it out.
Thanks in advance
I have two form in one page. When I click on the submit button of form one, the validation error shows on both forms. How could show separate validation errors on each form?
This is my view:
<?php echo form_open('user_signup/login',['class'=>'login-form','id'=>'submit_form']);
echo validation_errors();?>
<h3 class="form-title font-green">Sign In</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter any username and password. </span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email1','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email1')]); ?>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<?php echo form_input(['name'=>'pass','type'=>'password','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Password']); ?>
</div>
<div class="form-actions">
<?php echo form_submit('submit', 'Submit',"class='btn green uppercase'" ); ?>
Forgot Password?
</div>
<div class="login-options">
<h4>Or login with</h4>
<ul class="social-icons">
<li>
<a class="social-icon-color facebook" data-original-title="facebook" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color twitter" data-original-title="Twitter" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color googleplus" data-original-title="Goole Plus" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color linkedin" data-original-title="Linkedin" href="javascript:;"></a>
</li>
</ul>
</div>
<div class="create-account">
<p>
Create an account
</p>
</div>
</form>
<!-- END LOGIN FORM -->
<!-- BEGIN FORGOT PASSWORD FORM -->
<form class="forget-form" action="http://keenthemes.com/preview/metronic/theme/admin_2/index.html" method="post">
<h3 class="font-green">Forget Password ?</h3>
<p> Enter your e-mail address below to reset your password. </p>
<div class="form-group">
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Email" name="email" /> </div>
<div class="form-actions">
<button type="button" id="back-btn" class="btn green btn-outline">Back</button>
<button type="submit" class="btn btn-success uppercase pull-right">Submit</button>
</div>
</form>
<!-- END FORGOT PASSWORD FORM -->
<!-- BEGIN REGISTRATION FORM -->
<?php echo form_open('user_signup/login',['class'=>'register-form','id'=>'register_form']);
?>
<h3 class="font-green">Sign Up</h3>
<p class="hint"> Enter your personal details below: </p>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Roll NO</label>
<?php echo form_input(['name'=>'rollno','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Rollno','value'=>set_value('rollno')]); ?>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email2','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email2')]); ?>
</div>
<div class="form-actions">
<button type="button" id="register-back-btn" class="btn green btn-outline">Back</button>
<?php echo form_submit('register', 'register',"class='btn btn-success uppercase pull-right'" ); ?>
This is my controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_signup extends CI_Controller {
public function index()
{
$this->load->view('User/signup');
}
public function login()
{
if ($this->input->post('submit_form')) {
$rules['email1'] = 'required';
$rules['pass'] = 'required';
$this->form_validation->set_rules($rules);
}
else if ($this->input->post('register_form')) {
$rules['rollno'] = 'required';
$rules['email2'] = 'required';
$this->validation->set_rules($rules);
}
if (!$this->form_validation->run()) {
$this->load->view('User/signup');
}
else {
if ($this->input->post('submit_form'))
echo 'Form 1 posted !';
else if ($this->input->post('register_form'))
echo 'Form 2 posted !';
}
}
}
There are two things you need to concern about that. First passing action in HTML form. Second is, getting those parameters and code accordingly in your controller's action.
<?php echo form_open('user_signup/login/form-1',['class'=>'login-form','id'=>'submit_form']);
echo validation_errors();?>
<h3 class="form-title font-green">Sign In</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter any username and password. </span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email1','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email1')]); ?>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<?php echo form_input(['name'=>'pass','type'=>'password','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Password']); ?>
</div>
<div class="form-actions">
<?php echo form_submit('submit', 'Submit',"class='btn green uppercase'" ); ?>
Forgot Password?
</div>
<div class="login-options">
<h4>Or login with</h4>
<ul class="social-icons">
<li>
<a class="social-icon-color facebook" data-original-title="facebook" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color twitter" data-original-title="Twitter" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color googleplus" data-original-title="Goole Plus" href="javascript:;"></a>
</li>
<li>
<a class="social-icon-color linkedin" data-original-title="Linkedin" href="javascript:;"></a>
</li>
</ul>
</div>
<div class="create-account">
<p>
Create an account
</p>
</div>
</form>
<!-- END LOGIN FORM -->
<!-- BEGIN FORGOT PASSWORD FORM -->
<form class="forget-form" action="http://keenthemes.com/preview/metronic/theme/admin_2/index.html" method="post">
<h3 class="font-green">Forget Password ?</h3>
<p> Enter your e-mail address below to reset your password. </p>
<div class="form-group">
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Email" name="email" /> </div>
<div class="form-actions">
<button type="button" id="back-btn" class="btn green btn-outline">Back</button>
<button type="submit" class="btn btn-success uppercase pull-right">Submit</button>
</div>
</form>
<!-- END FORGOT PASSWORD FORM -->
<!-- BEGIN REGISTRATION FORM -->
<?php echo form_open('user_signup/login/form-2',['class'=>'register-form','id'=>'register_form']);
?>
<h3 class="font-green">Sign Up</h3>
<p class="hint"> Enter your personal details below: </p>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Roll NO</label>
<?php echo form_input(['name'=>'rollno','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Rollno','value'=>set_value('rollno')]); ?>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<?php echo form_input(['name'=>'email2','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email2')]); ?>
</div>
<div class="form-actions">
<button type="button" id="register-back-btn" class="btn green btn-outline">Back</button>
<?php echo form_submit('register', 'register',"class='btn btn-success uppercase pull-right'" ); ?>
Note: Check form actions
Here is your updated controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_signup extends CI_Controller {
public function index()
{
$this->load->view('User/signup');
}
public function login()
{
// Checking passed 3rd parameter in URI string.
$submitted_form = $this->uri->segment(3)?$this->uri->segment(3):false;
if ($submitted_form == 'form-1') {
$rules['email1'] = 'required';
$rules['pass'] = 'required';
$this->form_validation->set_rules($rules);
}
if ($submitted_form == 'form-2') {
$rules['rollno'] = 'required';
$rules['email2'] = 'required';
$this->validation->set_rules($rules);
}
if ($this->form_validation->run()) {
if ($submitted_form == 'form-1')
echo 'Form 1 posted !';
else if ($submitted_form == 'form-1')
echo 'Form 2 posted !';
}
$this->load->view('User/signup');
}
}
Let me know if you have any confusion.
Submit by Jquery. For Example in your view
<form id="form1" action="" method="post">
<label>Username</label>
<input type="text" name="username">
<label>Password</label>
<input type="password" name="password">
<input type="hidden" name="form_type" value="login">
<button type="submit" onClick="login()">Login</button>
</form>
<form id="form2" action="" method="post">
<label>Username</label>
<input type="text" name="username">
<label>Password</label>
<input type="password" name="password">
<label>Email</label>
<input type="email" name="email">
<input type="hidden" name="form_type" value="register">
<button type="submit" onClick="register()">Login</button>
</form>
Jquery Script
<script>
function login()
{
$('#form1').submit();
}
function register()
{
$('#form2').submit();
}
</script>
Now in your controller
if($_POST)
{
if($_POST['form_type']=='login')
{
// Validate Login
}
elseif($_POST['form_type']=='register')
{
// validate register
}
}
I have a two step form. create.php and create2.php. Both pages are forms. After first form is filled user presses "Continue" and proceeds to the second form where i pass and store value from first form in hidden inputs. After filling and submitting second form, i want pop up window which means successful submission of the form and inserting all the data into database. It seems that everything is working fine and all data is in database, however i dont get pop up window, what i get is undefined index warning about my hidden inputs.
Ok here is code for create.php:
<form class="formcss" method="post" action="create2.php" id="reportform" enctype="multipart/form-data">
<fieldset style="background-color:white;">
<legend style="font-size: 20px;">New Project</legend>
<br>
<div class="row">
<div class="small-8 large-8 columns">
<label>Project Code: <small style="color:red;">*</small>
<input type="text" name="code" maxlength="155" id="code" class="input input1 name" onkeyup="limitTextCount('code', 'divcount0', 155, 0);" onkeydown="limitTextCount('code', 'divcount0', 155, 0);" <?php if (isset($code)) echo 'value="'.$code.'"' ?>/>
<label class="tool tool1" for="name" style="margin-top:-8px;">Code of the project</br>e.g. ASD001</label>
</label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<label>Project Name: <small style="color:red;">*</small>
<input type="text" name="title" maxlength="155" id="title" class="input input1 name" onkeyup="limitTextCount('title', 'divcount0', 155, 0);" onkeydown="limitTextCount('title', 'divcount0', 155, 0);" <?php if (isset($title)) echo 'value="'.$title.'"' ?>/>
<label class="tool tool1" for="name" style="margin-top:-8px;">Title of the project</br>e.g. Leon</label>
</label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<label>Process
<div class="multiselect">
<div class="selectBox">
<select onclick="showCheckboxes()" class="input input1 name">
<option>-- Select an option --</option>
</select>
<div class="overSelect"></div>
</div>
<div class="scrollable" id="checkboxes">
<?php
while ($row = mysql_fetch_array($result))
{
$row[0] = cleanOutputData($row[0]);
?>
<div class="row">
<div class="small-12 large-12 columns">
<label style="height: 37px; width:80%; float:left;">
<input type="checkbox" class="checkbox" style="margin-left:5%; width:15%;" name="process[]" id=<?php echo $row[0] ?> value=<?php echo $row[0]?> /><?php echo $row[0] ?>
</label>
<label style="width:40%; margin-left:60%;"><input type="text" class="field" disabled style="width:40%;" name="numberpl[]" id=<?php echo $row[0] ?> />
</label>
</div>
</div>
<?php
}
mysql_free_result($result);
?>
</div>
</div>
</label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<label>Comments
<textarea style="resize:none;" class="input input1 name" name="remark" rows="8" cols="50" maxlength="255" id="remark" onkeyup="limitTextCount('remark', 'divcount5', 255, 0);" onkeydown="limitTextCount('remark', 'divcount5', 255, 0);"><?php if (isset($remark)) echo $remark ?></textarea>
<label class="tool tool1" for="name" style="left:-140px;">Further comments</label>
</label>
</div>
<div class="small-6 large-6 columns">
<label> <?php if (isset($remark)){ echo "<label id='divcount5'></label>"; echo "<script>limitTextCount('remark', 'divcount5', 255, 0);</script>";} else echo "<label id='divcount5'>255 characters remaining</label>";?></label>
</div>
</div>
<div class="row">
<div class="small-8 large-8 columns">
<input type = "submit" name ="submit" style="margin-left:600px; width:150px;" class="button" onclick="userSubmitted = true;" value = "Continue"/>
</div>
</div>
<br/><br/>
</fieldset>
</form>
And for the second form create2.php:
<?php
session_start();
//if user haven't sign in will redirect user to login page
if(empty($_SESSION['login_user'])){
session_destroy();
header("Location: login.php");
}
$proc = isset($_POST['process'])?$_POST['process']:'';
//$proc=$_POST['process'];
$len = count($proc); // getting length of ur array that u need to condition ur loop
$num = isset($_POST['numberpl'])?$_POST['numberpl']:'';
//$num=$_POST['numberpl'];
//$len2 = count($num); // getting length of ur array that u need to condition ur loop
include 'verification/verify_form_details2.php';
require_once('inc/config.php');
//include 'verification/verify_form_details.php';
ob_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<form class="formcss" method="POST" name="checkoutForm" action="create2.php#err" id="reportform" enctype="multipart/form-data">
<?php
$res = verifyFormFields();
?>
<!-- hidden inputs from first form(create.php) -->
<input type="hidden" name="holdcode" value="<?php echo isset($_POST['code'])?$_POST['code']:'';?>">
<input type="hidden" name="holdtitle" value="<?php echo isset($_POST['title'])?$_POST['title']:'';?>">
<?php
//an array of inputs
for($y=0;$y<$len;$y++)
{
?>
<input type='hidden' name='holdprocess[]' value="<?php echo $proc[$y]?>">
<input type='hidden' name='holdnumber[]' value="<?php echo $num[$y]?>">
<?php
}
?>
<!-- hidden inputs from first form(create.php) -->
<br>
<fieldset style="background-color:white;">
<legend style="font-size: 20px;">Add Stuff</legend>
<br><br><br>
<div class="small-3 large-3 columns">
<label>Choose username</label>
<input placeholder="Search Me" id="box" type="text" />
<div id="myAccordion">
<?php for($i=321; $i<347; $i++)
{
echo "<h3>".chr($i)."</h3>";
echo '<ul class="source">';
$sql = "SELECT username FROM user WHERE username LIKE '".chr($i+32)."%' ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$name= $row["username"];
echo"<li>". $name ."</li>";
}
} else
{
echo "0 results";
}
echo '</ul>';
}
?>
</div>
</div>
<div id="project" class="small-9 large-9 columns">
<label style="font-size: 40px; margin-left:10%;">Project <?php echo isset($_POST['code'])?$_POST['code']:''; ?></label>
<div class="rowone">
<div id="leader">
<label>Leader:</label>
<div class="ui-widget-content">
<div id="projLeader">
<ol>
<li class="placeholder" name="leader" <?php if (isset($leader)) echo 'value="'.$leader.'"' ?>>Add leader here</li>
<input type="hidden" name="leader" id="hiddenListInput1" />
</ol>
</div>
</div>
</div>
<div id="checker">
<label>Checker:</label>
<div class="ui-widget-content">
<div id="projChecker">
<ol>
<li class="placeholder" name="checker" <?php if (isset($checker)) echo 'value="'.$checker.'"' ?>>Add checker here</li>
<input type="hidden" name="checker" id="hiddenListInput2" />
</ol>
</div>
</div>
</div>
<div id="info">
<label>Information:</label>
<div class="ui-widget-content">
<ol>
<li>Total:</li>
<li>Total:</li>
<li>Total:</li>
<li>Total:</li>
<li>Total:</li>
</ol>
</div>
</div>
</div>
<div class="row">
<input type = "submit" id="savebutton" style="margin-left:300px; width:150px;" name ="submit" class="button" value = "Create Project" onclick="userSubmitted = true;" />
</div>
</div>
<div id="formModal" class="reveal-modal small" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog" data-options="close_on_background_click:false">
<h2 id="modalTitle">Success!</h2>
<div style="font-weight: 400;font-size: 1.5em; font-family: 'Raleway', Arial, sans-serif;">Your Accident Report was Successfully Submitted!</div>
<div class="right">
Ok
</div>
</div>
<?php
if($counta==1)
{
if($res=="")
{
$testing = JSON_encode($_SESSION['role']);
echo '<script>userSubmitted = true;</script>';
insertRecord();
echo "<script type ='text/javascript'>callShowAlert();</script>";
}
else{
echo "
<br><br><a style='color:red';>
$res
</a>
";
}
}
?>
<script>var testing = JSON.parse('<?= $testing; ?>');</script>
</fieldset>
</form>
Here is what i got after submitting second form:
Notice:
Undefined index: process in C:\xampp\htdocs\Projects\1ver\create2.php
on line 9
Notice:
Undefined index: numberpl in C:\xampp\htdocs\Projects\1ver\create2.php
on line 12
Notice:
Undefined index: code in
C:\xampp\htdocs\Projects\1ver\create2.php on line 256
Notice:
Undefined index: title in
C:\xampp\htdocs\Projects\1ver\create2.php on line 257
Notice:
Undefined index: title in C:\xampp\htdocs\Projects\1ver\create2.php on
line 302
I'm using another php page to insert data into database. I just don't get what can be a problem. Thanks for any help
Since POST array is not available for first time. Therefore, you are getting this error. If you really need to use those post variables, you should apply a condition in this case to avoid these errors.
I'm here adding for one, you can do the same for remaining.
Try this:
<?php echo isset($_POST['code'])?$_POST['code']:''; ?>