Answer the question
In order to leave comments, you need to log in
Sessions and Ajax request?
DD. I use sessions in projects, I work with them for the first time, I need to transfer data from the form to the php script via ajax, but if I write ssesion start in the script file, the database entry does not occur, and if I remove it, the Undefined index.
Here are both files:
Extract from index
<?php
session_start();
if(!isset($_SESSION["session_username"])):
header("location:index.php");
else:
?>
<!DOCTYPE html>
<?php
include("includes/connection.php");
?>
<?php if (!empty($message)) {echo "<p class=\"error\">" . "MESSAGE: ". $message . "</p>";} ?>
<!DOCTYPE html>
<html class="home" lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-2.1.4.js"></script>
<script src="js/main.js"></script>
<script type="text/javascript">
$(function(){
$("#send").click(function(){
var title = $("#title").val();
var color = $("#color").val();
var project = $("#project").val();
var shorttext = $("#shorttext").val();
var task = $("#task").val();
$.ajax({
type: "POST",
url: "includes/tasks.php",
data: {"title": title, "color": color,"project": project,"shorttext": shorttext,"task": task},
cache: false,
success: function(response){
var messageResp = new Array('Ваше сообщение отправлено','Сообщение не отправлено Ошибка базы данных','Нельзя отправлять пустые сообщения');
var resultStat = messageResp[Number(response)];
if(response == 0){
$("#title").val("");
$("#shorttext").val("");
$("#color").val("");
$("#project").val("");
$("#task").val("");
}
$("#resp").text(resultStat).show().delay(1500).fadeOut(800);
}
});
return false;
});
});
</script>
<?php
session_start();
var_dump($_SESSION,$_POST)
?>
<?php
include("connection.php");
header("Content-type: text/html; charset=UTF-8");
//**********************************************
if(empty($_POST['js'])){
if($_POST['title'] != '' && $_POST['task'] != '' && $_POST['shorttext'] != '' && $_POST['color'] != '' && $_POST['project'] != ''){
$title = @iconv("UTF-8", $_POST['title']);
$title = addslashes($title);
$title = htmlspecialchars($title);
$title = stripslashes($title);
$title = mysql_real_escape_string($title);
$task = @iconv("UTF-8", $_POST['task']);
$task = addslashes($task);
$task = htmlspecialchars($task);
$task = stripslashes($task);
$task = mysql_real_escape_string($task);
$shorttext = @iconv("UTF-8", $_POST['shorttext']);
$shorttext = addslashes($shorttext);
$shorttext = htmlspecialchars($shorttext);
$shorttext = stripslashes($shorttext);
$shorttext = mysql_real_escape_string($shorttext);
$color = @iconv("UTF-8", $_POST['color']);
$color = addslashes($color);
$color = htmlspecialchars($color);
$color = stripslashes($color);
$color = mysql_real_escape_string($color);
$project = @iconv("UTF-8", $_POST['project']);
$project = addslashes($project);
$project = htmlspecialchars($project);
$project = stripslashes($project);
$project = mysql_real_escape_string($project);
$date = date("d-m-Y в H:i:s");
$result = mysql_query("INSERT INTO tasks (task, title, shorttext, color, project, date) VALUES ('$title', '$task', '$shorttext', '$color', '$project')");
if($result == true){
echo 0; //Ваше сообшение успешно отправлено
}else{
echo 1; //Сообщение не отправлено. Ошибка базы данных
}
}else{
echo 2; //Нельзя отправлять пустые сообщения
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question