Values received from database aren't displaying properly in php - php

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).'>

Related

Php include header not showing on some machines

So i have an header and footer in seperate files, i include them via the php include method. On my machine it works on chrome and edge. When i go to a other pc it works on chrome but not on edge. Now when it doesn't load it actually loads but dissapears directly after. It is only on screen for a short flash.
My file structure is as following:
2.10.0
index.php
assets
headernav.php
In my index.php file i use this code to include.
<?php include("assets/headernav.php"); ?>
The headernav.php file it includes is as follows
<script>
function openmen() {
document.getElementById("menu").style.marginLeft = "0";
document.getElementById('menubtn').setAttribute("onClick", "closemen()");
document.getElementById("content").style.paddingLeft = "250px";
document.getElementById("searchbox").style.left = "240px";
}
function closemen() {
document.getElementById("menu").style.marginLeft = "-230px";
document.getElementById('menubtn').setAttribute("onClick", "openmen()");
document.getElementById("content").style.paddingLeft = "20px";
document.getElementById("searchbox").style.left = "10px";
}
function opensettings() {
document.getElementById("settings").style.display = "block";
}
function closesettings() {
document.getElementById("settings").style.display = "none";
}
</script>
<settings_background id="settings">
<settings>
<h2>Instellingen</h2><i id="closebtn" onclick="closesettings()" class="fas fa-times"></i>
<a>Eenheid</a>
<form method="POST" action="includes/units.inc.php">
<input placeholder="Naam" class="inputmaterial" type="text" name="naam" value="<?php echo $_SESSION['naam']; ?>">
<input placeholder="Callsign" class="inputmaterial" type="text" name="callsign" value="<?php echo $_SESSION['callsign']; ?>">
<select Placeholder="department" class="inputmaterial" name="department">
<option value="<?php echo $_SESSION['department']; ?>"><?php echo $_SESSION['department']; ?></option>
<option value="Police">Police</option>
<option value="Traffic police">Traffic police</option>
<option value="Fire">Fire</option>
<option value="Ems">Ems</option>
<option value="Milatary police">Milatary police</option>
<option value="Siu">Siu</option>
</select>
<input placeholder="rang/functie" class="inputmaterial" type="text" name="rang" value="<?php echo $_SESSION['rang']; ?>">
<input placeholder="Naam" type="hidden" name="id" value="<?php echo $_SESSION["id"]; ?>">
<input type="hidden" name="return" value="index.php">
<br><br><br><input style="float:left;"class="button_grey" type="submit" name="cookie" value="Save">
</form>
<a>Account</a>
<form method="POST" action="login_v3/account.inc.php">
<input placeholder="Huidig wachtwoord" class="inputmaterial" type="password" name="pwdold">
<input placeholder="Nieuw wachtwoord" class="inputmaterial" type="password" name="password">
<input placeholder="uid" type="hidden" name="uid" value="<?php echo $_SESSION['id']; ?>">
<br><br><br><input class="button_grey" type="submit" name="updatepassword" value="Save">
</form>
<a>Personalisatie</a>
<form action="includes/units.inc.php" method="POST">
<input type="checkbox" id="id-name--1" name="meldingen" class="switch-input" <?php if(isset($_SESSION['notificaties'])){echo "checked";} ?>>
<label for="id-name--1" class="switch-label">Meldingen <span class="toggle--on">Aan</span><span class="toggle--off">Uit</span></label>
<input type="checkbox" id="id-name--2" name="darkmode" class="switch-input" <?php if(isset($_SESSION['darkmode'])){echo "checked";} ?>>
<label for="id-name--2" class="switch-label">Darkmode <span class="toggle--on">Aan</span><span class="toggle--off">Uit</span></label><br><br><br>*darkmode is nog in beta
<input placeholder="Naam" type="hidden" name="id" value="<?php echo $_SESSION["id"]; ?>">
<br><br><input type="submit" name="personalisatie" value="Save" class="button_grey">
</form>
</settings>
</settings_background>
<nav_menu id="menu"><div class="section">
<i class="fas fa-phone"></i> Meldingen
<i class="fas fa-id-card"></i> Database
<i class="fas fa-eye"></i> Gezocht
<i class="fas fa-map-marked-alt"></i> Kaarten
</div>
<div class="section">
<p>Verslagen</p>
</i> Verslagen
<a><i class="fas fa-notes-medical"></i> Nieuw verslag</a>
</div>
<div class="section">
<p>Registratie</p>
<i class="fas fa-bell"></i> Maak melding
<i class="fas fa-receipt"></i> Bekeuring
<i class="fas fa-exclamation"></i> Waarschuwing
<i class="fas fa-mask"></i> Misdrijf
<a><i class="fas fa-eye"></i> Arrestatiebevel</a>
</div>
<div class="section">
<p>Database toevoeging</p>
<a><i class="fas fa-car"></i> Voertuig</a>
</div>
<div class="section">
<p>Meldkamer</p>
<i class="fas fa-headset"></i> Meldkamer
</div>
<p>
© www.lscdb.nl
</p>
</nav_menu>
<nav_bar><div class="nav_bar_left"><i id="menubtn" onclick="closemen()" class="fas fa-bars"></i><img src="../img/lscdb_logo_white.png"></div><div class="logged_in"><?php echo $_SESSION['naam']; ?> | <?php echo $_SESSION['callsign']; ?></div><i class="fas fa-question"></i><i class="fas fa-sign-out-alt"></i><i onclick="opensettings()" class="fas fa-cog"></i></nav_bar>
So to conclude, my headernav file i include using php works on some machines, while on others loads and dissapears directly after.
So i found a solution.... finally.
It turns out the header that has a position:fixed allways needs top:0; and left:0;, even if the header is allready in the correct position.

How can I make sure a user either inserted a description or file before form submission?

i have a foreach row that displays the "message icon" (i dont think it has anything to do with the form since the form is outside the loop:
enter image description here
which displays a chat form that you can either type a chat or send an attachment:
enter image description here
How can i make one of them required before submission? I tried through multiple JQuery ways but because they are in a for loop and each of them do not have a special id, its not working. any help please? I just need it to show an alert
Here is my code for the form:
<form method="post"action="<?php echo base_url();?>form_support_chat" enctype="multipart/form-data" class="ticket-reply-form">
<div class="row">
<div class="col-xs-12">
<div class="chat-left">
<input type="text" name="message" placeholder="Type Message ..." class="form-control" autocomplete="off">
<input type="hidden" name="cst_id" value="<?php echo $cst_id; ?>">
<input type="hidden" name="arch_ticket" value="<?php echo $arch_ticket; ?>">
<input type="hidden" name="ticket_number" value="<?php echo $ticket_number?>">
</div>
<div class="chat-right">
<label>
<img src="<?php echo base_url();?>/assest/icon-img/paperclip.png" class="ic_img" >
<input type="file" name="file" class="form-control" style="display:none;" id="hidden_upload_file_chatting">
</label>
<button type="submit" class="btn btn-flat" name="reply" value="reply" >Reply</button>
<button type="button" class="btn btn-flat" data-dismiss="modal" style="margin-left: 10px !important;">Close</button>
<span id="_showName"></span>
</div>
</div>
</div>
</form>
Simply use html5's required value
<input name="cst_id" value="<?php echo $cst_id; ?>" required>
or
<input name="cst_id" value="<?php echo $cst_id; ?>" required="true">
update
Using jQuery, add id value to each input
$(document).ready(function() {
if( $('#input1').valid() || $('#input2').valid(); ):
console.log("Success");
endif;
});

HTML form not passing checkbox value trough $_POST in php file

I have a form in HTML with some checkboxes and I want to send an email with the selected checkboxes in the body, the problem is that the "text" fields are passing to the PHP variables, but the checkboxes values are always set to uncheck when I test it on the PHP file:
HTML:
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control c-check" >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina
</label>
</div>
PHP:
$reservas_bicicletas = '';
if (isset($_POST['checkbox1citadina'])) {
$reservas_bicicletas .= "Citadina: checked\n";
} else {
$reservas_bicicletas .= "Citadina: unchecked\n";
}
echo $reservas_bicicletas;
$reservas_bicicletas always retrieves the else value.
Need help guys, thanks in advance.
remove c-check
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
Basically i have this (i shorted out the form because there are too many elements):
HTML:
<form name="quick_booking" id="quick_booking" method="post" action="assets/reserva-bicicletas.php">
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
<button type="submit" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square">Reservar</button>
</form>

How to auto create divs depending on the data in mysql table using php?

I want to create an ecommerce website, using PHP and mySQL DB. I have this code below and I want (with the use of php if possible), after connecting to sql DB(I did this) to repeat this DIV depending on the entries in the DB table and echo the data of each entry in table. I found this but I don't know if it's the right example: Automatically creating div's using data from a sql table
Here's the code:
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<image>.jpg" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
</li>
</ul>
</div>
</div>
<h5><Itemname></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"><Price></i></p>
<p><i class="item_price"><Description></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="Itemname" />
<input type="hidden" name="amount" value="Price"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
The example you provided above will work just fine, all you need to do is put the div/div's you want to generate multiple times inside a loop. then they will be generated as long as the loop condition is met.
But though the example u provided works but you need to note that it
is using mysql_* functions which are depreciated and no longer
supported by newer php versions .
Therefore I would advice that you learn about prepared statements instead, and use PDO.
consider the following :
<?php
$sql = $pdo->query("SELECT * FROM products")->fetchall(PDO::FETCH_ASSOC);
foreach($sql as $row):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
</li>
</ul>
</div>
</div>
<h5><?php echo $row['item_name'];?></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
<p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="<?php echo $row['item_name'];?>" />
<input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
<?php
endforeach;
?>
OR
<?php
$sql = $pdo->query("SELECT * FROM products");
while ($row = $sql->fetchall(PDO::FETCH_ASSOC)):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
<div class="hs-wrapper hs-wrapper2">
<img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" />
<div class="hs_bottom hs_bottom_sub1">
<ul>
<li>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
</li>
</ul>
</div>
</div>
<h5><?php echo $row['item_name'];?></h5>
<div class="simpleCart_shelfItem">
<p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
<p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
<form action="#" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="item" value="<?php echo $row['item_name'];?>" />
<input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>
<button type="submit" class="cart">Add to cart</button>
</form>
</div>
</div>
<?php
endwhile;
?>
Where $pdo is your database connection string.
This will fetch data and generates the div for each product.
If you not using prepared statements for your application, I would advice you to use them more especially pdo prepared statements, below are the places you can learn pdo prepared statements
https://phpdelusions.net/pdo#foreach
http://jayblanchard.net/demystifying_php_pdo.html

store username to use in a later form

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');
}

Categories