I wanted to learn how to make a cms. I know crud operations but when I want to take some value from ckeditor and put it in MySQL database is not working. Is making me nervous I know I did a mistake but I don't know where.
<?php
if (isset($_POST['submit'])){
if(isset($_POST['editor']) && !empty($_POST['editor'])){
$content = $_POST['editor'];
}else{
$empty_error = "didnt send to database";
}
if (isset($content) && !empty($content)) {
$insert_q = "INSERT INTO `contact`(continut) VALUES ('$content')";
if (mysqli_query($conn,$insert_q)) {
// code...
}else{
$submit_error ="submit didnt work";
}
}
}
?>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="card-title">Add</div>
<hr>
<form action=" " method="post" enctype="multipart/form-data">
<textarea name="editor" class="ckeditor" rows="8" cols="80"></textarea>
<button type="submit">Save</button>
</form>
</div>
</div>
</div>
</div>
Related
Quick rundown:
Everything ran perfectly as it should when I had my own custom textarea field to send data
I had a custom text editor widget for inputs, but tried to add CKEditor for more functionality
When I added CKEditor package (https://ckeditor.com/ckeditor-4/download/?undefined-addons=) the editor was there, but when I clicked "Add" button and sent data, the input was doubled and there was a space between inputs (for example - input was:"test" and output was "test<br><br>test"), as if I put <br> tag and multiplied my input somewhere.
When I stopped trying with CKEditor, I went back to original code and my own original custom text editor, which before worked perfectly. However, to my surprise, it now still doubles the input string, but it doesn't put a <br> tag in between.
In DB the string value is normal and not doubled.
I have no idea how this happened, I've been going over the code for a hour or so now, trying to see if I overlooked something, but I even created a separate file with functioning code, before I was trying with CKEditor in case something broke and I could just replace it with old code and try tomorrow, but now it's messed up and I have no idea how and where.
notifications.php
<form class="form-inline" action="/action_page.php">
</form>
</nav>
<div class="container-fluid m-0 p-0">
<div class="row justify-content-center">
<div class="col-md-10">
<?php if (isset($_SESSION['response'])) { ?>
<div class="alert alert-<?= $_SESSION['res_type']; ?> alert-dismissible text-center">
<button type="button" class="close" data-dismiss="alert">×</button>
<b><?= $_SESSION['response']; ?></b>
</div>
<?php } unset($_SESSION['response']); ?>
</div>
</div>
<div class="row">
<div class="">
<?php
$query = 'SELECT * FROM crud';
$stmt = $conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
?>
<table class="table table-hover" id="data-table">
<tbody>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td></td>
<td class="pt-2"><?=
$longString=$row['name'];
$link = $row['id'];
$longStringshortcut = strlen($longString);
//echo substr($longString, 0, 100).'... Read More';
if ($longStringshortcut > 250) {
echo substr($longString, 0, 250).".. <a href='details.php?details=$link'><strong>Preberi več...</strong></a>"; }
else {
echo $longString;
}
?>
Details
Delete
Edit
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="col-md-4 p-0">
<h5 class="">Add notification:</h5>
<form action="action.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= $id; ?>">
<div class="form-group">
<textarea name="name" value="<?= $name; ?>" class="form-control" placeholder="This is the default text" required></textarea>
</div>
<div class="form-group">
<?php if ($update == true) { ?>
<input type="submit" name="update" class="btn btn-success btn-block" value="Change notification">
<?php } else { ?>
<input type="submit" name="add" class="btn btn-primary btn-block" value="Add">
<?php } ?>
</div>
<div class="form-group">
<input type="hidden" name="oldimage" value="<?= $photo; ?>">
<input type="file" name="image" class="custom-file">
<img src="<?= $photo; ?>" width="120" class="img-thumbnail">
</div>
</form>
</div>
</div>
</div>
</div>
action.php
<?php
session_start();
include 'config.php';
$update=false;
$id="";
$name="";
$photo="";
if(isset($_POST['add'])){
$name=$_POST['name'];
$photo=$_FILES['image']['name'];
$upload="uploads/".$photo;
$query="INSERT INTO crud(name,photo)VALUES(?,?)";
$stmt=$conn->prepare($query);
$stmt->bind_param("ss",$name,$upload);
$stmt->execute();
move_uploaded_file($_FILES['image']['tmp_name'], $upload);
header('location:index.php');
$_SESSION['response']="Successfully Inserted to the database!";
$_SESSION['res_type']="success";
}
The problem was with the <?= syntax (echoing), <?php solved it.
Good morning, I am dealing with an existing code and I need to solve a problem:
When you write a URL without logging into the Web, you can see the page in question (except the layout), what I need is to redirect to the login to avoid this.
So you can see the page without logging in
Checking the code I have seen that it is like this:
layout.php
<body>
<?php if(isset($_SESSION["user_id"])):?>
HTML code (Logo, menu...)
<?php else:?>
<?php
View::load("login");
?>
<?php endif;?>
</body>
If I change "View::load("login");" to "header("Location: index.php"); exit();", it causes an Infinite loop and does not load anything.
I have investigated a lot in Stackoverflow and other websites but I do not find the solution. Any ideas?
UPDATE:
index-view.php
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>LegoBox</h1>
</div>
</div>
</div>
login-view.php
<?php
if(Session::getUID()!=""){
print "<script>window.location='index.php?view=home';</script>";
}
?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php if(isset($_COOKIE['password_updated'])):?>
<div class="alert alert-success">
<p><i class='glyphicon glyphicon-off'></i> Se ha cambiado la contraseña exitosamente.</p>
<p>Pruebe iniciar sesion con su nueva contraseña.</p>
</div>
<?php setcookie("password_updated","",time()-18600);
endif; ?>
<div class="card">
<div class="card-header" data-background-color="lemon">
<h4 class="title">Acceder</h4>
</div>
<div class="card-content table-responsive">
<form accept-charset="UTF-8" role="form" method="post" action="index.php?view=processlogin">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Usuario" name="mail" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Contraseña" name="password" type="password" value="">
</div>
<input class="btn btn-primary btn-block" type="submit" value="Iniciar Sesion" style="background-color:#339c24">
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
processlogin-view.php
<?php
if(Session::getUID()=="") {
$user = $_POST['mail'];
$pass = sha1(md5($_POST['password']));
$base = new Database();
$con = $base->connect();
$sql = "select * from user where (email= \"".$user."\" or username= \"".$user."\") and password= \"".$pass."\" and is_active=1";
//print $sql;
$query = $con->query($sql);
$found = false;
$userid = null;
while($r = $query->fetch_array()){
$found = true ;
$userid = $r['id'];
}
if($found==true) {
// print $userid;
ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);
$_SESSION['user_id']=$userid;
// setcookie('userid',$userid);
// print $_SESSION['userid'];
print "Cargando ... $user";
print "<script>window.location='index.php?view=home';</script>";
}else {
print "<script>window.location='index.php?view=login';</script>";
}
}else{
print "<script>window.location='index.php?view=home';</script>";
}
?>
I would like to know how to run 2 or more input files in the same form, I have to upload some documents by using php, I made separate forms and they work, but I need to all together however I dont know how. I need to put only two forms as example actually I need to put 3 but the 3rd is larger so it would be much code to read with an example putting only two I would be able to do the rest.
Note: Form 1 and Form to upload data to different tables.
Form 1
<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
$fileName=$_FILES['myFile']['name'];
$fileTmpName=$_FILES['myFile']['tmp_name'];
$fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);
$allowedType = array('csv');
if(!in_array($fileExtension,$allowedType)){?>
<div class="alert alert-danger">
INVALID FILE
</div>
<?php }else{
$handle = fopen($fileTmpName, 'r');
$k = 0;
$energies = array ();
while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
$k++;
if ( $k > 1 ) {
$energies[] = $myData[3];
}
}
list($e1, $e2, $e3) = $energies;
$query = "INSERT INTO metlab.resultados_impacto_junta (energy1, energy2, energy3) VALUES ($e1, $e2, $e3)";
$run = mysql_query($query);
if(!$run){
die("error in uploading file".mysql_error());
}else{ ?>
<div class="alert alert-success">
SUCCESS
</div>
<?php }
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<h3 class="text-center">
RESULTS
</h3></hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="file" name="myFile" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="submit" name ="uploadBtn" class="btn btn-info">
</div>
</div>
</div>
</form>
Form 2
<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
$fileName=$_FILES['myFile']['name'];
$fileTmpName=$_FILES['myFile']['tmp_name'];
//RUTA DEL ARCHIVO
$fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);
//FORMATOS DE ARCHIVO PERMITIDOS
$allowedType = array('csv');
if(!in_array($fileExtension,$allowedType)){?>
<div class="alert alert-danger">
INVALID FILE
</div>
<?php }else{
$handle = fopen($fileTmpName, 'r');
$k = 0;
while (($myData = fgetcsv($handle,1000,','))!== FALSE){
$k++;
if ( $k > 4 ) {
$valor_dureza = $myData[3];
$query = "INSERT INTO metlab.resultados_tension_junta (size,yield,tensile,ra,elongacion)
VALUES ('".$valor_dureza."')";
$run = mysql_query($query);
}
}
if(!$run){
die("error in uploading file".mysql_error());
}else{ ?>
<div class="alert alert-success">
SUCCESS
</div>
<?php }
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<h3 class="text-center">
RESULTS
</h3></hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="file" name="myFile" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="submit" name ="uploadBtn" class="btn btn-info">
</div>
</div>
</div>
</form>
I would like a form like this:
With the Fk I would know which number the 3 docs belong to.
I solved the problem myself I just changed the vars of the forms and thats all, Its a dirty and bad solution but it works for now.
I am trying to handle the Post request in the same page. But i am facing some issues when handling post parameters.I am always getting 'value as empty'.
Below is my code.
<body>
<?php include 'Header.html' ?>
<div class="container">
<div style="margin:10% 30% 0% 30%;">
<div class="panel panel-default">
<div class="panel-heading">
<h4 align="center" style="font-family: monospace;"><p>Upload External Device Report</p></h4>
</div>
<div class="panel-body">
<div class="container col-lg-12">
<form class="form-horizontal" action="UploadExternalReport.php" role="form" method='post' enctype="multipart/form-data">
<div class="input-group">
<input type="file" class="filestyle" data-buttonName="btn-primary" name="fileToUpload" accept=".xls ,.xlsx" required="required" >
</div>
<br/>
<button type="submit" class="btn btn-default">Upload</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
// require_once '../PHPExcel/IOFactory.php';
if(isset($_POST["fileToUpload"]) && trim($_POST["fileToUpload"]) != ""){
echo $_POST['fileToUpload'];
}
else
{
echo'value is empty';
}?>
I tried !empty($_POST["fileToUpload"]) in if condition. But still its getting as 'value is empty'.
You are supposed to use the $_FILES array, not $_POST.
So make use of $_FILES['fileToUpload'].
http://php.net/manual/en/reserved.variables.files.php
Gudday guys,I have a little challenge posting a form successful to a database after loading the form remotely using jquery's load() method,it worked in google chrome but not working using firefox.Thanks in anticipation of your help.
PHP Code.
<?php
include 'login.php';
if( isset($_POST['c_title']) && isset($_POST['c_code']) && isset($_POST['staff_id'])&& isset($_POST['staff_fulname'])
&& isset($_POST['c_desc'])&& isset($_POST['unit']) ){
$c_title = sanitize_string($_POST['c_title']);
$c_code = sanitize_string($_POST['c_code']);
$staff_id = sanitize_string($_POST['staff_id']);
$staff_fulname= sanitize_string($_POST['staff_fulname']);
$c_desc = sanitize_string($_POST['c_desc']);
$unit = sanitize_string($_POST['unit']);
$sql = " insert into course values('$c_title','$c_code','$staff_id','$staff_fulname','$c_desc','$unit') ";
$result = mysql_query($sql);
if($result){
// echo '<br/> <div align="center"><small class="alert alert-success">√ Saved Successfully!</small></div>';
echo '<script>alert("Saved Successfully!");</script>';
}
else{
echo '<script>alert("Error Occured!");</script>';
}
}
function sanitize_string($var){
$var = mysql_real_escape_string($var);
$var = htmlentities($var);
$var = stripslashes($var);
return $var;
}
?>
<body>
<div class="container5">
<div align="center"><h3 class="btn-danger">Course Details</h3></div>
<div class="row">
<div class="span12">
<br/>
<div class="row">
<form id ="course_form" action="" method="post">
<div class="span6">
<div align="center">
Course Title:<input type="text" name="c_title" required/>
</div></div>
<div align="right">
<div class="span6">
Course Code:<input type="text" name="c_code" required/>
</select>
</div>
</div></div></div>
</div>
<div class="row">
<div class="span12">
<div class="row">
<div class="span6">
<div align="center">
Staff ID:<input type="text" name="staff_id" required/>
</div></div>
<div class="span6">
<div align="right">
Staff FullName:<input type="text" name="staff_fulname" required/>
</div></div>
<br/>
</div></div></div>
<div class="row">
<div class="span12">
<div class="row">
<div class="span6">
<div align="center">
Course Description:<textarea name ="c_desc" rows="3"></textarea>
</div></div>
<div class="span6">
<div align="right">
Unit Load:<select name="unit"> <option>1</option><option>2</option><option>3</option><option>4</option><option>6</option> </select></div>
</div>
<br/>
</div></div></div>
<div align="center"><button id="save2" class=" btn btn-danger">Save</button>
</form>
</div>
</body>
<script>
$(document).ready(function() {
$("#save2").click(function(){
$.ajax({
type: "POST",
url: "course.php",
data: $('#course_form').serialize(),
success: function(msg){
alert("Saved Successfully");
},
error: function(){
alert("Error has occurred");
return
}
});
})
});
</script>