how to add simple editor for my comment box - php

Hello guys,
i'am new in php to Add something new, and i need help
to add simple editor for my Add comment box ?
Thank you I wish I could get help Here.
Thank you I wish I could get help Here.
like this
enter image description here
<!-- Start Add Comment -->
<?php
if (isset($_SESSION['user'])) {
$stopitem = $con->prepare("SELECT opitem FROM items");
// Execute The Statement
$stopitem->execute();
// Assign To Variable
$opitem = $stopitem->fetch();
if ($opitem['opitem'] == 0) {
?>
<div class="row">
<div class="col-md-offset-1">
<div class="add-comment">
<h3>
Add Your Comment
</h3>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] . '?itemid=' . $item['Item_ID'] ?>">
<textarea name="comment" required="required"></textarea>
<input class="btn btn-primary" type="submit" value="Add Comment" name="">
</form>
<?PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
$userid = $item['Member_ID'];
$itemid = $item['Item_ID'];
if (!empty($comment)) {
$stmt = $con->prepare("INSERT INTO comments(comment, status, comment_date, item_id, user_id) VALUE(:zcomment, 0, NOW(), :zitemid, :zuserid)");
$stmt->execute(array(
'zcomment' => $comment,
'zitemid' => $itemid,
'zuserid' => $userid
));
if ($stmt) {
echo "Comment Added";
}
} else {
echo '<div class="alert alert-danger">You Must Add Comment</div>';
echo '<script language="javascript">';
echo 'alert("You Must Add Comment")';
echo '</script>';
}
}
?>
</div>
</div>
</div>
<?php
} else {
echo "This Item is closed.";
}
} else {
echo 'Regester or Log In To Add Comment';
}
?>
<!-- End Add Comment -->

Just add this into your head section.
<script src="http://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>

Related

how can i fix the jquery removeClass for checkbox ? (add class works)

the body
(many pieces of code were taken from a guide on youtube where everything worked, i
I did everything in the same way but it doesn't work).
the removeClass works only when i refresh the page
<body>
<div class="container">
<div class="todo">
<h1>todo app with php</h1>
<h3>add a new to do</h3>
<form action="index.php" method="POST">
<div class="form-group">
<input type="text" class="form-control" name="todo" placeholder="todo name">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="add a new todo task list">
</div>
</form>
<?php while ($row = mysqli_fetch_array($result)) { ?>
<div class="todo-item">
<?php if ($row['checkbox']) { ?>
<input type="checkbox" class="check-box" data-todo-id="<?php echo $row['id']; ?>" checked />
<h2 class="checked"><?php echo $row['t_name'] ?></h2>
<?php } else { ?>
<input type="checkbox" data-todo-id="<?php echo $row['id']; ?>" class="check-box" />
<h2><?php echo $row['t_name'] ?></h2>
<?php } ?>
<small>created: <?php echo $row['t_date'] ?></small>
<!--<button>delete</button> -->
</div>
<?php } ?>
</div>
</div>
in the script maybe I should enter something like e.stopPropagation() but i dont understand where and how. the addClass works but i can't turn back removing checked class.
<script>
$(document).ready(function() {
// checkbox toggle function
$(".check-box").on('click', function(e) {
//Get Task id from Task Table using checkbox
const id = $(this).attr('data-todo-id');
$.post('check.php', {
id: id
},
(data) => {
if (data != 'error') {
const h2 = $(this).next();
if (data === '1') {
h2.removeClass('checked');
// If Data Response id 1 means if checkbox is checked this alert will be show
} else {
h2.addClass('checked');
// If Data Response id 0 means if checkbox is unchecked this alert will be show
}
}
}
);
});
});
</script>
the check.php where i have data
<?php
if (isset($_POST['id'])) {
require 'db.inc.php';
// Get Id From Post Method
$id = $_POST['id'];
if (empty($id)) {
echo 'error';
} else {
// Select Task From Info table
$record = mysqli_query($connection, "SELECT * FROM todo WHERE id=$id");
$todo = mysqli_fetch_array($record);
$uId = $todo['id'];
$checked = $todo['checkbox'];
$uChecked = $checked ? 0 : 1;
//Update Task Completed Status
$res = mysqli_query($connection, "UPDATE todo SET checkbox=$uChecked WHERE id=$uId");
if ($res) {
echo $checked;
} else {
echo "error";
}
$connection = null;
exit();
}
} else {
header("Location: ../index.php?mess=error");
}

Hiding the form if the field in database is null in php

Hi i have a registration form in my website.if the particular field in the db is null then the form should not be displayed to the user.Here if the payment_category_upload field is empty then the form should not displayed to the user otherwise the form should be displayed.
<?php include 'includes/db.php';
$sql = "SELECT * FROM users WHERE username = '$_SESSION[user]' AND user_password = '$_SESSION[password]' AND payment_category_upload!='' ";
$oppointArr =array();
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0)
{
if(isset($_POST['submit_user'])|| isset($_POST['save_users']))
{
$formsubmitstatus = isset($_POST['submit_user'])?1:0;
if($_FILES["affidavits_upload"]["tmp_name"]!="")
{
$pname = rand(1000,10000)."-".str_replace("-"," ",$_FILES["affidavits_upload"]["name"]);
$affidavits_upload = $_FILES["affidavits_upload"]["tmp_name"];
$uploads_dir = '../admin/images/uploads';
move_uploaded_file($affidavits_upload, $uploads_dir.'/'.$pname);
}
else
{
$pname = $_POST['hid_affidavits_upload'];
}
$id= $_POST['users_id'];
$ins_sql = "UPDATE users set affidavits_upload='$pname',status='3',affidavitsupload_submit_status='$formsubmitstatus' WHERE users_id = $id";
$run_sql = mysqli_query($conn,$ins_sql);
$msg = 'Your Application successfully submitted. ';
$msgclass = 'bg-success';
}
else
{
$msg = 'Record Not Updated';
$msgclass = 'bg-danger';
}
}
else
{
echo "Please make the payment to enable Affidavits";
}
?>
FORM :
<form class="form-horizontal" action="affidavits.php" method="post" role="form" enctype="multipart/form-data" id="employeeeditform">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='users_id'>
<div class="form-group">
<label for="affidavits_upload" class="col-sm-4 control-label">Affidavits Upload</label>
<div class="col-sm-8">
<input type="hidden" value="<?php echo $oppointArr['affidavits_upload'];?>" name="hid_payment_category_upload">
<input type="file" name="affidavits_upload" id="affidavits_upload">
<?php if(!empty($oppointArr['affidavits_upload'])){?>
<div>
<?php echo $oppointArr['affidavits_upload'];?>
</div>
<?php }?>
<span class="text" style="color:red;">Please upload PDF Format Only</span>
</div>
</div>
<div class="col-sm-offset-2">
<?php if($oppointArr['affidavitsupload_submit_status'] == 0){ ?>
<button type="submit" class="btn btn-default" name="save_users" id="save_users">Save</button>
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit Application</button>
<?php } ?>
</div>
</form>
//Add this Css class
.hiddenBlock {
display:none
}
<div class="<?php echo isset(test_field)?"":"hiddenBlock"; ?>">
<form>...<form>
</div>
You can do it like this for your field.

How to pass selected option from drop-down to form for record update

Trying to build a record update form where I need to pass a value from a drop-down list to an update query. The drop-down list options come from a select query. I am close to accepting defeat but thought I'd first reach out to the collective brain-trust. I've console logged where the trouble seems to be. With the reference error, the "lat_pulldown" is one of the drop-down list items. I'm confused as to why it would need to be defined, and have not been able to identify the cause of the syntax error.
Much thanks for any help or suggestions!
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$stationtype = $selectstatype = "";
$stationtype_err = "";
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
// Get hidden input value
$id = $_POST["id"];
// Validate stationtype
$input_stationtype = trim($_POST["stationtype"]);
//$input_selectstatype = trim($_POST["stationtype"]);
echo "<script> console.log('log1 ') </script>";
echo "<script> console.log($input_stationtype) </script>"; //log1
if(empty($input_stationtype )){
$stationtype_err = "Please select a station type.";
} else{
$stationtype = $input_stationtype ;
}
// Check input errors before inserting in database
if(empty($stationtype_err)){
// Prepare an update statement
$sql = " UPDATE stactrak_WPCPH.reptbl1 SET stationtype=?, reading_time=reading_time WHERE setfnsecs=? ";
echo "<script> console.log('log2 ') </script>";
echo "<script> console.log($sql) </script>"; //log2
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssi", $param_stationtype, $param_setfnsecs);
// Set parameters
$param_stationtype = $stationtype;
$param_setfnsecs = $id;
echo "<script> console.log('log3 ') </script>";
echo "<script> console.log($param_setfnsecs) </script>"; //log3
echo "<script> console.log($param_stationtype) </script>"; //log3
// Attempt to execute the prepared statement
//echo "<script> console.log('log3 ') </script>";
//echo "<script> console.log(mysqli_stmt_execute($stmt)) </script>"; //log3
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
echo "<script> location='https://stactraklog.com/set-crud/index.php' </script>";
} else{
echo "Something went wrong. Please try again later.";
}
}
}
}
?>
<!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 Set Record</h2>
</div>
<p>Please edit the station type value and submit to update the set.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($stationtype_err)) ? 'has-error' : ''; ?>">
<label>current stationtype</label>
<input type="text" name="stationtypeold" readonly class="form-control" value="<?php echo $stationtype; ?>">
</div>
<div class="form-group <?php echo (!empty($stationtype_err)) ? 'has-error' : ''; ?>">
<label>enter station type ...</label>
<select class="form-control" name="stationtype" id="stationtype">
<?php
// Include config file
require_once "config.php";
$sql = "SELECT ".
" s.stationtype ".
"FROM ".
" stactrak_WPCPH.reptbl1 r, ".
" stactrak_WPCPH.stations s ".
"WHERE".
" 1 = 1 ".
" and r.machname = s.machname ".
" and r.setfnsecs = $id ".
"UNION ".
"SELECT ' ' stationtype ".
"ORDER BY 1";
$result = mysqli_query($link, $sql);
while($rows = mysqli_fetch_assoc($result)) {
$optionstatype = $rows['stationtype'];
echo "<option value='$optionstatype'>$optionstatype</option>";
}
?>
</select>
<span class="help-block"><?php echo $stationtype_err;?></span>
</div>
<div class="form-group <?php echo (!empty($stationtype_err)) ? 'has-error' : ''; ?>">
<label>new stationtype</label>
<input type="text" name="newstationtype" class="form-control" value="<?php echo $stationtype; ?>">
<span class="help-block"><?php echo $stationtype_err;?></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
Cancel
</form>
</div>
</div>
</div>
</div>`enter code here`
</body>
</html>

Submitting form to database

I've got everything worked out for my Support Ticket website, except my newticket form isn't posting the values to the database. Here is what I have:
<?php
ob_start();
session_start();
include 'dbconnect.php';
if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
$error = false;
}
if ( isset($_POST['btn-cancel']) ) {
header("Location: home.php");
exit;
}
if ( isset($_POST['btn-signup']) ) {
// get form results
$text = $_POST['description'];
$text = strip_tags($text);
$userid = $_POST['user'];
$problem = $_POST['problem'];
$room = $_POST['room'];
$status = 1;
$datetime = date('Y-m-d G:i:s');
// description validation
if (empty($text)) {
$error = true;
$textError = "Please describe the problem.";
} else if (strlen($text) > 200) {
$error = true;
$textError = "Description must be less than 200 characters in length.";
}
// dropdown validation
if ($problem < 1){
$error = true;
$problemError = "Please choose a category and problem.";
} else if ($room < 1) {
$error = true;
$roomError = "Please choose a building and a room number.";
// if there's no error, continue to signup
if( !$error ) {
$query = "INSERT INTO job (User_UserID,Problem_ProblemID,Status_StatusID,Room_RoomID,Description,Date_Time) VALUES({$userid},{$problem},{$status},{$room},'{$text}',{$dateTime})";
echo '$query';
$res = mysqli_query($conn,$query);
if ($res) {
$errTyp = "success";
$errMSG = "Successfully submited ticket";
unset($text);
unset($problem);
unset($room);
unset($datetime);
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later.";
}
}
}
}
?>
HTML:
<!DOCTYPE html>
<html>
<head>
<SCRIPT language=JavaScript>
<!--
//function reload(form)
{
//var val=form.type.options[form.type.options.selectedIndex].value;
//self.location='newticket.php?type=' + val ;
}
//function reload2(form)
{
//var val=form.building.options[form.building.options.selectedIndex].value;
//self.location='newticket.php?building=' + val ;
}
function disableselect()
{
<?Php
if(isset($type) and strlen($type) > 0){
echo "document.f1.problem.disabled = false;";}
else{echo "document.f1.problem.disabled = true;";}
if(isset($building) and strlen($type) > 0){
echo "document.f1.room.disabled = false;";}
else{echo "document.f1.room.disabled = true;";}
?>
}
//-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dog Tracks - Login & Registration System</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body onload=disableselect();>
<div class="container">
<div id="login-form">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
<div class="col-md-12">
<div class="form-group">
<h2 class="">Create New Ticket</h2>
</div>
<div class="form-group">
<hr />
</div>
<?php
if ( isset($errMSG) ) {
?>
<div class="form-group">
</div>
</div>
<?php
}
?>
<div class="form-group">
<div class="input-group">
<?php
//Getting the data for first list box
$quer2="SELECT problem_typeid,problem_type FROM problem_type ORDER BY problem_type";
echo "<select name='type' onchange=\"reload(this.form)\"><option value=''>Pick problem category</option>";
//while($result2 = mysql_fetch_array($quer2)) {
foreach (mysqli_query($conn,$quer2) as $result2) {
if($result2['problem_typeid']==$type){echo "<option selected value='$result2[problem_typeid]'>$result2[problem_type]</option>"."<BR>";}
else{echo "<option value='$result2[problem_typeid]'>$result2[problem_type]</option>";}
}
echo "</select>";
$type=$_GET['type'];
// for second drop down list
if(isset($type) and strlen($type) > 0){
$quer="SELECT problemid,problem FROM problem WHERE
Problem_Type_Problem_TypeID={$type} order by problem";
}else {
$quer="SELECT problemid,problem FROM problem order by problem";
}
echo "<select name='problem'><option value=''>What is the problem?</option>";
//while($result = mysql_fetch_array($quer)) {
foreach (mysqli_query($conn,$quer) as $result) {
echo "<option value='$result[problemid]'>$result[problem]</option>";
}
echo "</select>";
//// Add your other form fields as needed here/////
?>
</div>
<span class="text-danger"><?php echo $problemError; ?></span>
</div>
<div class="form-group">
<div class="input-group">
<?php
#$building=$_GET['building'];
//Getting the data for first list box
$quer3="SELECT buildingid,building FROM building ORDER BY building";
// for second drop down list
if(isset($building) and strlen($building) > 0){
$quer4="SELECT roomid,roomNum FROM room WHERE
building_buildingID=$building order by roomNum";
}else {
$quer4="SELECT roomid,roomNum FROM room order by roomNum";
}
echo "<select name='building' onchange=\"reload2(this.form)\"><option value=''>In which building?</option>";
//while($result2 = mysql_fetch_array($quer3)) {
foreach (mysqli_query($conn,$quer3) as $result3) {
if($result3['buildingid']==#$building){echo "<option selected value='$result3[buildingid]'>$result3[building]</option>"."<BR>";}
else{echo "<option value='$result3[buildingid]'>$result3[building]</option>";}
}
echo "</select>";
echo "<select name='room'><option value=''>In what room?</option>";
//while($result = mysql_fetch_array($quer)) {
foreach (mysqli_query($conn,$quer4) as $result4) {
echo "<option value='$result4[roomid]'>$result4[roomNum]</option>";
}
echo "</select>";
//// Add your other form fields as needed here/////
?>
</div>
<span class="text-danger"><?php echo $roomError; ?></span>
</div>
<div class="form-group">
<div class="input-group">
<textarea cols='72' id='description' rows='6' placeholder="Description">
Please describe the problem here. </textarea>
</div>
<span class="text-danger"><?php echo $textError; ?></span>
</div>
<div class="form-group">
<hr />
</div>
<div class="form-group">
<button type="submit" class="btn btn-block btn-primary" name="btn-submit">Submit</button>
<button type="submit" class="btn btn-block btn-primary" name="btn-cancel">Cancel</button>
</div>
<div class="form-group">
<hr />
</div>
</form>
</div>
</div>
</body>
</html>
<?php
mysqli_close($conn);
ob_end_flush();
?>
First
if ($problem < 1){
$error = true;
$problemError = "Please choose a category and problem.";
} else if ($room < 1) {
$error = true;
$roomError = "Please choose a building and a room number.";
Oops, you don't close that else if, so your INSERT is always skipped.
The next problem
One part of the code that looks dodgy are these lines:
$datetime = date('Y-m-d G:i:s');
//...
$query = "INSERT INTO job (User_UserID,Problem_ProblemID,Status_StatusID,Room_RoomID,Description,Date_Time)
VALUES({$userid},{$problem},{$status},{$room},'{$text}',{$dateTime})";
^^^^^^^^^^^
Your $dateTime will be something like 2016-12-09 11:43:42, so the SQL statement will be:
INSERT INTO job (...) VALUES(..., 'This is my text', 2016-12-09 11:43:42)
This is a syntax error. Note that your $text field has quotes around it to keep it as a single element, but your date does not. Using '{$dateTime}' will fix this problem, although there is no guarantee that there is no other issue...

Why is my error message not echoing to the screen?

When the user submits a form, the PHP will check to see if the $post was empty, if so, it will set an $error_message variable; thus database INSERT query will not execute. I then attempt to show the error message further on in the code, however it will not display. I have been trying to figure this out for hours and I still cannot find the solution. The funny thing is, this used to work, however I must have implemented something in this file which is preventing the code to execute correctly.
Why is my error message not echoing to the screen (last few lines of code show attempt to echo the error_message)?
<?php require("inc/db.php"); ?>
<?php include("inc/functions.php"); ?>
<?php include ("inc/ChromePhp.php"); ?>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("html_errors", 1);
session_start();
if(!isset($_GET['id'])) {
header("Location: ?id=".getUserId($_SESSION['U_Email']));
}
if(isset($_SESSION["U_Email"])) {
$usersData = getUserInfo($_GET['id']);
$postCount = getPostCount($_GET['id']);
} else {
header('Location: login.php');
}
if($_SERVER["REQUEST_METHOD"] == "POST") {
$post = trim(filter_input(INPUT_POST,"user_post",FILTER_SANITIZE_SPECIAL_CHARS));
if($post == "") {
$error_message = "Please enter something before submitting";
}
if(!isset($error_message)) {
$post = $db->real_escape_string($post);
$postStore = $db->query("INSERT INTO `Post`(P_Body, P_Dateadded, User_U_ID) VALUES ('{$post}', NOW(), '{$_SESSION['U_ID']}' )");
}
}
$dashNav = true;
$cap = true;
$pageTitle = 'Profile';
$name = $usersData['U_Forename'] . " " . $usersData['U_Surname'];
$gender = $usersData['U_Gender'];
$bio = $usersData['U_Biography'];
$team = $usersData['U_Team'];
$city = $usersData['U_City'];
include("inc/header.php");
?>
<?php if(userExists($_GET['id'])) { ?>
<section>
<div class="wrapper">
<?php
if($_SESSION['U_ID'] != $_GET['id']) {
$testFollow = "SELECT `Following`.F_ID, `Following`.U_ID FROM `Following`
WHERE U_ID = '{$_SESSION['U_ID']}' AND F_ID = '{$_GET['id']}'";
$testFollowResult = $db->query($testFollow);
if ($testFollowResult->num_rows > 0) {
echo "<button class='lift' href='#'>Unfollow Driver</button>";
} else {
echo "<button class='lift' href='#'>Follow Driver</button>";
}
}
?>
<?php if (isset($error_message)) {
// This code will not echo!
echo "<h2>".$error_message."</h2>";
}
?>
</div>
<div class="section-b">
<div class="grid">
<div class="row">
<div class="col-wd-12">
<div class="col">
<form id="share" name="share" action="profile.php" method="post">
<textarea id="post" name="user_post" placeholder="What's happening?"> </textarea>
<span id="errorpost" class="error">You must input something something before sending</span>
<?php
echo "<div class='g-recaptcha' data- sitekey='6LfNTB0TAAAAAKEw9zfnFzvGCXF9MuYTkdB144x1
'></div>"; ?>
<button onclick="return postCheckValidate();" type="submit">Share</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php } ?>

Categories