I've tried so many methods from stackoverflow and other websites but whenever i succeed in hiding the div. No search result is displayed at all.
I've tried the :empty selector and fiddling around with the php code and js code. but since i'm very new to this i just can't seem to crack the error. What am i doing wrong?
My HTML
<div class='search'>
<form class="searchbox" action='index.php' method='post' autocomplete='off'>
<input type='text' name='search' class='searchform' placeholder='Søg efter skole...' onkeyup="searchq();">
</form>
<div id="output"></div>
</div>
PHP
<?php
include("connection.php");
$output = '';
//collect
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace("#[^a-zA-Z0-9æøå]#i"," ", $searchq);
$query = mysqli_query($mysqli, "SELECT * FROM `products` WHERE name LIKE '%$searchq%'") or die("could not search");
$count = mysqli_num_rows($query);
if($_POST['searchVal'] == NULL) {
$output = '';
} else {
while($row = mysqli_fetch_array($query)) {
$name = $row['name'];
$id = $row['id'];
$output .= ''.$name.'<br>';
}
}
}
echo "<div class='output'>$output</div>";
?>
And JS
function searchq() {
var searchTxt = $("input[name='search']").val();
$.post("search.php", {
searchVal: searchTxt
}, function(output) {
$("#output").html(output);
});
}
HTML
<div class='search'>
<form class="searchbox" action='index.php' method='post' autocomplete='off'>
<input type='text' name='search' class='searchform' placeholder='Søg efter skole...' onkeyup="searchq();">
</form>
<div id="output" style="display: none;"></div>
</div>
PHP
.....
echo $output;
JS
function searchq() {
var searchTxt = $("input[name='search']").val();
$.post("search.php", {
searchVal: searchTxt
}, function(output) {
$("#output").html(output);
if(output.length > 0) {
$("#output").show();
}
});
}
Related
When I click the button to submit the form it sends me to the PHP file (Php/modify-recipes.php). I'm using AJAX with e.preventDefault();, but it doesn't seem to work. I have a similar form on my other page and it works fine. This one is a little bit different, the form is generated from another PHP file (it takes values from the database).
// AJAX (in HTML head tag)
<script>
$(function () {
$("#search-form").on('click', 'p.product', function(e){
var product = $(this).attr('id');
e.preventDefault();
$.ajax({
type: "post",
url: 'Php/display-modify-recipes.php',
data: { "id": product },
success: function(response) {
$(".modify-recipes").html(response);
$(".search-results").html("");
}
});
}
)});
$(function () {
$('#add_recipes_form').on('click', '#add', function (e) {
e.preventDefault();
let sendForm = true;
$('.required').each(function(){
if ($(this).val() == ''){
$(this).addClass('error', 4000);
$('#placeholder-text').html("Fill in the form");
$('#placeholder-text').addClass('error');
$('#add').addClass('error', 4000);
sendForm = false;
} else {
$(this).removeClass('error');
$('#placeholder-text').removeClass('error');
$('#add').removeClass('error');
}
})
$('.required2').each(function(){
if ($(this).val() == ''){
$('#placeholder-text').html("Fill in the form");
$('#placeholder-text').addClass('error');
$('#add').addClass('error');
$('#file_style').addClass('error');
sendForm = false;
} else {
$('#file_style').removeClass('error');
$('#placeholder-text').removeClass('error');
$('#add').removeClass('error');
}
})
if (sendForm) {
$.ajax({
type: "post",
url: 'Php/modify-recipes.php',
data: new FormData(this),
processData: false,
contentType: false,
success: function () {
},
error: function () {
}
});
}
});
});
</script>
// HTML
<form method="POST" enctype='multipart/form-data' id="search-form" class="aaa">
<input type="text" name="search-bar" class="search-bar" placeholder="Search">
<div class="search-results"></div>
</form>
<div class="modify-recipes"></div>
// Php/display-modify-recipes.php
<?php
function display_mod_form(){
$con = mysqli_connect("localhost", "root", "", "cookbook");
$product = $_POST["id"];
$sql = "SELECT * FROM product WHERE id = '$product'";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($res)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$type = $row['type'];
$difficulty = $row['difficulty'];
$image = $row['image'];
?>
<form method="POST" action="Php/modify-recipes.php" enctype="multipart/form-data" id="add_recipes_form" class="add-form">
<input type="text" name="id" id="<?php echo $id ?>" value="<?php echo $id ?>" style="display:none;">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="required" maxlength="28" value='<?php echo $name ?>'>
<label for="description">Desc.</label>
<input type="text" name="description" id="description" class="required" maxlength="128" value='<?php echo $description ?>'>
<label for="type">Type</label>
<select name="type" class="required">
<option value="dishes" <?php if($type == 'dishes') {echo "selected=selected"; } ?>>
Dishes
</option>
<option value="desserts" <?php if($type == 'desserts') {echo "selected=selected"; } ?>>
Desserts
</option>
<option value="snacks" <?php if($type == 'snacks') {echo "selected=selected"; } ?>>
Snacks
</option>
<option value="other" <?php if($type == 'other') {echo "selected=selected"; } ?>>
Other
</option>
</select>
<label for="difficulty">Difficulty</label>
<select name="difficulty" class="required">
<option value="easy" <?php if($difficulty == 'easy') {echo "selected=selected"; } ?>>
Ease
</option>
<option value="moderate" <?php if($difficulty == 'moderate') {echo "selected=selected"; } ?>>
Moderate
</option>
<option value="hard" <?php if($difficulty == 'hard') {echo "selected=selected"; } ?>>
Hard
</option>
</select>
<label for="image">Image (jpg, png)</label>
<label for="upload_image" id="file_style">Choose image</label>
<input type="file" style="display:none;" id="upload_image" name="image" accept="image/*" onchange="loadFile(event)" class="required2">
<div class="send-btn">
<p id="placeholder-text"></p>
<input type="submit" name="add" id="add" value="Send">
<div class="send-block"></div>
</div>
</form>
<?php
}
mysqli_close($con);
}
display_mod_form();
?>
// Php/modify-recipes.php
<?php
$con = mysqli_connect("localhost", "root", "", "cookbook");
$id = $_POST["id"];
$name = $_POST["name"];
$description = $_POST["description"];
$type = $_POST["type"];
$difficulty = $_POST["difficulty"];
if ($_FILES['image']['name'] == ""){
$sql_var = "SELECT image FROM product where id = '$id'";
$var = mysqli_query($con,$sql_var);
while($row = mysqli_fetch_assoc($var)) {
$image = $row['image'];
}
} else {
$image = $_FILES['image']['name'];
}
$sql = "
UPDATE product SET
name = '$name',
description = '$description',
type = '$type',
difficulty = '$difficulty',
image = '$image',
last_mod_date = '$mod_date'
WHERE id = '$id';
";
$target = "../Uploads/".basename($image);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
}
$var = mysqli_multi_query($con,$sql); <!-- There are more queries -->
mysqli_close($con);
?>
(Sorry for any mistakes, it's a shortened version)
There is a third ajax function and PHP file, but it's not important (simple search bar). I tried other things like action="", no action at all, etc., but it didn't work.
How to fix this?
Edit: There is only one form (when you click on p.product it displays the form according to the data in the database). The problem occurs ONLY when I'm submitting the "add_recipes_form", there is no problem with the search box. Please don't mark this question as a duplicate of a 10 year old post that is irrelevant to my problem.
Ajax code for language switching from database is not working in safari and Firefox, but its perfectly working on chrome and edge. Safari doesn't respond to ajax call but in firefox it only work after a lot of attempts to change language.
My code is
<?php
session_start();
if($_SESSION)
{
$bid=$_SESSION['latest'];
}
else
{
$bid=1;
}
?>
<select name="sources" id="language_id" onclick="" onchange="location.reload();langchange();">
<?php
$language = mysqli_query($link, "select * from language_reference");
while ($lang = mysqli_fetch_array($language)) {
?>
<option value="<?php echo $lang['lang_id']; ?>"
<?php
if($lang['lang_id']==$bid)
{
echo 'selected="selected"' ;
}
?>
><?php echo $lang['lang_name']; ?>
</option>
<?php } ?>
</select>
<div class="row" >
<?php
$result = mysqli_query($link, "SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id IS NULL && lang_id='1' ");
$var = 0;
while ($row = mysqli_fetch_array($result))
{
?>
<div class="col-md-3 col-sm-3 col-xs-6 hvr-shrink" style="position: relative; margin-top: 50px;">
<a href="#" style="text-decoration: none;">
<div>
<img src="admin/uploads/category/<?php echo $row['cat_home_image']; ?>" class="img-responsive center-block" width="50px" />
<p class="category-caption" style="padding-top: 20px; text-align: center;" id="cat_desc<?php echo $var; ?>"></p>
</div>
</a>
</div>
<?php
$var++;
}
?>
ajax script
<script>
function langchange() {
var language_id = $('#language_id').val();
$.ajax({
type: "POST",
url: "ajax_lang_change.php",
data: "id=" + language_id,
success: function (value)
{
var data = value.split("|_,_|");
for (var i = 0; i < data.length; i++) {
$("#cat_desc" + i).html(data[i]);
}
}
});
$.ajax({
type: "POST",
url: "ajax_get_session.php",
data: "bid=" + language_id,
success: function (value) {
var data = value;
}
});
}
$(window).ready(function () {
langchange()
});
</script>
php script to get data from db
ajax_lang_change.php
<?php
include 'db_connect.php';
$id = $_POST['id'];
$result = mysqli_query($link, "SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id IS NULL && lang_id='$id'");
while($row=mysqli_fetch_array($result))
{
echo $row['category_description']."|_,_|";
}
?>
ajax_get_session.php
<?php
include 'db_connect.php';
session_start();
$bid = $_POST['bid'];
$_SESSION['latest']=$bid;
if(isset($_SESSION['latest']))
{
echo 'success';
}
else
{
echo 'failed';
}
?>
is there anyway to solve this problem? should I have to write ajax script for various browsers?
I would like to know how a multiple update in MySQL with checkbox because I'm not getting the id attribute checkbox someone could help me?
HTML code
<form method="post" action="../sys/fav.php">
<input type="hidden" id="id_photo" name="id" value="">
<div class="col-md-12">
<p align="right"><button class="btn btn-primary" id="final" name="final">Send</button></p>
</div>
<?php
require "../sys/connect.php";
$email = $_GET['email'];
$sql = mysqli_query($mysqli, "SELECT * FROM gallery WHERE email ='$email'");
while($row = mysqli_fetch_array($sql)){
$id = $row['id'];
$name = $row['nome_galeria'];
$emailcontact = $row['email_contact'];
$pass = $row['pass'];
$email = $row['email'];
$img = $row['img'];
print"<div class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12\">
<input type=\"hidden\" name=\"contact\" value=\"$emailcontact\">
<input type=\"hidden\" name=\"login\" value=\"$email\">
<input type=\"hidden\" name=\"pass\" value=\"$pass\">
<input type=\"hidden\" name=\"name\" value=\"$name\">
<div class=\"hovereffect\">
<img id=\"he\" class=\"img-responsive\" src=\"../images/images/gallery/big/$img\" alt=\"$name\">
<div class=\"overlay\">
<div class=\"btn-group\" data-toggle=\"buttons\">
<label class=\"btn btn-primary cke\">
<input type=\"checkbox\" name=\"ck[]\" class=\"ck\" value=\"not\" id=\"ck_$id\"><i class=\"fa fa-heart\"></i>
</label>
</div>
</div>
</div>
</div>";
}
mysqli_close($mysqli)
?>
</form>
PHP Code
require "conexao.php";
if(isset($_POST['final'])){
$id = $_POST['id'];
foreach($_POST['ck'] as $ck){
$check = $ck;
$sql = mysqli_query($mysqli,"UPDATE gallery SET fav = '$check' WHERE id = '$id'")or die(mysqli_error($mysqli));
}
if($sql)
echo "Success!";
else
echo "Fail!";
}
Code Js to get the id of the checkbox
$("#final").click(function(){
var str = "";
var boxes = $(".ck");
for(var i = 0; i< boxes.length; i++){
if(boxes[i].checked == true){
var tmp = boxes[i].id.split("_");
str+=(i ? "," : "")+tmp[1];
}
}
document.getElementById('id_fotos').value=str;
});
You can get values of checkbox using below code and pass through ajax
var ckArray = new Array();
$.each($('input[name="ck[]"]:checked'),
function(index, ele){
ckArray.push($(ele).val());
});
I'm trying to send a variable from Javascript to PHP using AJAX.
The HTML (index.html):
<div class="table-popup">
<ul>
<li id="edit-toggle">Bearbeiten</li>
<li>Zu Favoriten hinzufügen</li>
<li>Datei öffnen</li>
<li>Im Ordner öffnen</li>
<li>Löschen</li>
</ul>
</div>
<div class="main-content">
<h2 class="main-content-header">Datenbank</h2>
<div id="table">
<table>
<thead>
<tr class="table-row" tabindex="1">
<th class="fixed-header"></th>
<th>Dateiname</th>
<th>Benutzer</th>
<th>Erstelldatum</th>
<th>Änderungsdatum</th>
<th>Erste Zeile</th>
<th>Kategorie</th>
<th>Projekt</th>
</tr>
</thead>
<?php
include_once('connect.php');
$result = $connect->query("SELECT file.name AS 'filename', file.description AS 'filedescription', category.name AS 'categoryname', project.name AS 'projectname', user.name AS 'username', idFile
FROM file, category, project, file_has_project, file_has_category, user, user_has_project, user_has_category
WHERE file.idFile = file_has_project.file_idFile AND file_has_project.project_idProject = project.idProject AND file.idFile = file_has_category.file_idFile AND file_has_category.category_idCategory = category.idCategory AND user.idUser = user_has_project.user_idUser AND user_has_project.project_idProject = project.idProject AND user.idUser = user_has_category.user_idUser AND user_has_category.category_idCategory = category.idCategory AND user.idUser = '".$_SESSION['userid']."'");
//echo "<tbody><td>".$result->num_rows."</td></tbody>";
if ($result->num_rows > 0) {
echo "<tbody>";
while($row = $result->fetch_assoc()) {
echo "<tr class='table-row' tabindex='1' id='".$row['idFile']."'>";
echo "<th class='table-edit-button fixed-header'><img src='images/open.gif' /></th>";
echo "<td>".$row['filename']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>-</td>";
echo "<td>-</td>";
echo "<td>".$row['filedescription']."</td>";
echo "<td>".$row['categoryname']."</td>";
echo "<td>".$row['projectname']."</td>";
echo "</tr>";
}
echo "</tbody>";
}
?>
</table>
</div>
</div>
The Javascript which is sending the AJAX request with jQuery (functions.js):
$(document).ready(function() {
var fileID;
$('.table-edit-button').click(function() {
fileID = $(this).parent().attr('id');
});
$('#edit-toggle').click(function() {
$.ajax({
url: 'edit.php',
type: 'post',
data: { fileID : fileID },
success: function(data) {
alert("Success");
}
});
});
});
The PHP file (edit.php):
<?php
if (isset($_POST['fileID']))
$fileID = $_POST['fileID'];
?>
<div class="edit-content">
<h2 class="edit-content-header">Bearbeiten<img src="images/cross.gif" /></h2>
<div>
<form action="" method="post">
<?php echo $fileID; ?>
<input type="text" placeholder="Dateiname"/>
<input type="text" placeholder="Benutzer"/>
<textarea placeholder="Erste Zeile" rows="7em" cols="100"></textarea>
<select name="Kategorie">
<option disabled selected>Kategorie</option>
<?php
include_once('connect.php');
$result = $connect->query("SELECT name FROM category");
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
}
?>
</select>
<select name="Projekt">
<option disabled selected>Projekt</option>
<?php
$result = $connect->query("SELECT name FROM project");
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
}
?>
</select>
<img id="savebtn" src="images/save.gif" />
</form>
</div>
</div>
The HTML displays data from a database in a table and each row has a button next to it. With jQuery I get the id of the row where the button has been clicked. I want to send this id to my php file to do some stuff there (irrelevant for now).
The problem I'm having is that I can't access the send variable (fileID) in my edit.php file.
The alert in the success part of the AJAX call gets executed.
What do I need to fix? I thought I had everything right.
I also tried to change the url of the AJAX call to ../edit.php but that didn't work either. Then the success part wouldn't be executed.
And different variable names didn't work either.
The project structure is as follows:
project (*)
edit.php
index.php
scripts (*)
functions.js
(*) directories
Edit:
The error message: Notice: Undefined variable: fileID in C:\xampp\htdocs\kuhlnotesweb\edit.php on line 10
AJAX returns the content of the page in the data success variable. Trying console.log(data) and you should see you variable has been echoing into the returned HTML.
If not, check in the dev tools that the fileID parameter is actually attached to the request.
UPDATE
<div class="edit-content">
<h2 class="edit-content-header">Bearbeiten<img src="images/cross.gif" /></h2>
<div>
<form action="" method="post">
<?php
if (isset($_POST['fileID'])) {
echo $_POST['fileID'];
}
?>
<input type="text" placeholder="Dateiname"/>
<input type="text" placeholder="Benutzer"/>
<textarea placeholder="Erste Zeile" rows="7em" cols="100"></textarea>
<select name="Kategorie">
<option disabled selected>Kategorie</option>
<?php
include_once('connect.php');
$result = $connect->query("SELECT name FROM category");
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
}
?>
</select>
<select name="Projekt">
<option disabled selected>Projekt</option>
<?php
$result = $connect->query("SELECT name FROM project");
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
}
?>
</select>
<img id="savebtn" src="images/save.gif" />
</form>
</div>
</div>
The problem is a syntax error in your js when you try to pass data
your code. see js fiddle example http://jsfiddle.net/mdamia/njd8m0g5/4/. how to get the value from the tr.
data: ({ fileID : fileID }),
should be data:
{ fileID : fileID } // no ()
Tested and it works.
from jQuery AJAX
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
This is my solution now:
functions.js:
$(document).ready(function() {
var fileID, fileName, fileDescription, fileCategory, fileProject;
$('.table-edit-button').click(function() {
fileID = $(this).parent().attr('id');
});
$('#edit-toggle').click(function() {
$.ajax({
url: 'ajax-edit.php',
type: 'post',
data: { fileID : fileID },
dataType: 'json',
success: function(data) {
fileName = data.filename;
fileDescription = data.filedescription;
fileCategory = data.categoryname;
fileProject = data.projectname;
$('#edit-fileid').val(fileID);
$('#edit-filename').val(fileName);
$('#edit-description').val(fileDescription);
$('#edit-projectname').val(fileProject);
$('#edit-categoryname').val(fileCategory);
}
});
});
});
ajax-edit.php:
<?php
if (isset($_POST['fileID'])) $fileID = $_POST['fileID'];
include_once('connect.php');
$result = $connect->query("SELECT file.name AS 'filename', file.description AS 'filedescription', project.name AS 'projectname', category.name AS 'categoryname'
FROM file, project, category, file_has_project, file_has_category
WHERE file.idFile = file_has_project.file_idFile AND file_has_project.project_idProject = project.idProject AND file.idFile = file_has_category.file_idFile AND file_has_category.category_idCategory = category.idCategory AND file.idFile = '".$fileID."'");
$result = $result->fetch_assoc();
echo json_encode($result);
?>
edit.php:
<div class="edit-content">
<h2 class="edit-content-header">Bearbeiten<img src="images/cross.gif" /></h2>
<div>
<form action="edit.php" method="post">
<input type="hidden" id="edit-fileid" name="edit-fileid"/>
<input type="text" id="edit-filename" name="edit-filename" placeholder="Dateiname"/>
<input type="text" id="edit-username" name="edit-username" placeholder="Benutzer"/>
<textarea id="edit-description" name="edit-description" placeholder="Erste Zeile" rows="7em" cols="100"></textarea>
<select id="edit-categoryname" name="edit-categoryname">
<option disabled selected value="category-first">Kategorie</option>
<?php
include_once('connect.php');
$result = $connect->query("SELECT category.name FROM category,user,user_has_category WHERE user.idUser = user_has_category.user_idUser AND user_has_category.category_idCategory = category.idCategory AND user.idUser = '".$_SESSION['userid']."'");
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
}
?>
</select>
<select id="edit-projectname" name="edit-projectname">
<option disabled selected value="project-first">Projekt</option>
<?php
$result = $connect->query("SELECT project.name FROM project,user,user_has_project WHERE user.idUser = user_has_project.user_idUser AND user_has_project.project_idProject = project.idProject AND user.idUser = '".$_SESSION['userid']."'");
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
}
?>
</select>
<input type="image" name="submit-button" id="savebtn" src="images/save.gif" />
</form>
</div>
</div>
I think there were many misunderstanding in what I was trying to achieve and how I was not able to understand how AJAX works exactly. edit.php is only a small part of HTML that gets included in index.php and the AJAX call I'm making gets made in the same file, so I was not able to use the variable I sent via AJAX in my edit.php file because at the point where I wanted to use it it wasn't even sent and thus the $_POST['fileID'] was undefined.
My solution to this was that I created a seperate php file (ajax-edit.php) where I retrieve the information I need from the database and return it as a JSON object. With this I am able to use the information from the JSON object to change the value of the input fields.
Thank you for the help everybody and I am sorry that I was so stubborn ^^.
So i am haveing this page where it is displaying articles andunderneet each article it will have a textarea asking allowing the user to insert a comment.I did the AJAX and it works fine.Some of the validation works fine aswell(Meaning that if the textarea is left empty it will not submit the comment and display an error).The way i am doing this validation is with the ID.So i have multi forms with the same ID.For the commets to be submited it works fine but the validtion doesnt work when i go on a second form for exmaple it only works for the first form
AJAX code
$(document).ready(function(){
$(document).on('click','.submitComment',function(e) {
e.preventDefault();
//send ajax request
var form = $(this).closest('form');
var comment = $('#comment');
if (comment.val().length > 1)
{
$.ajax({
url: 'ajax_comment.php',
type: 'POST',
cache: false,
dataType: 'json',
data: $(form).serialize(), //form serialize data
beforeSend: function(){
//Changeing submit button value text and disableing it
$(this).val('Submiting ....').attr('disabled', 'disabled');
},
success: function(data)
{
var item = $(data.html).hide().fadeIn(800);
$('.comment-block_' + data.id).append(item);
// reset form and button
$(form).trigger('reset');
$(this).val('Submit').removeAttr('disabled');
},
error: function(e)
{
alert(e);
}
});
}
else
{
alert("Hello");
}
});
});
index.php
<?php
require_once("menu.php");
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script src="comments.js" type="text/javascript" ></script>
<?php
$connection = connectToMySQL();
$selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";
$result = mysqli_query($connection,$selectPostQuery)
or die("Error in the query: ". mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
?>
<div class="wrapper">
<div class="titlecontainer">
<h1><?php echo $row['Title']?></h1>
</div>
<div class="textcontainer">
<?php echo $row['Content']?>
</div>
<?php
if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
{
?>
<div class="imagecontainer">
<img src="images/<?php echo "$row[ImagePath]"; ?>" alt="Article Image">
</div>
<?php
}
?>
<div class="timestampcontainer">
<b>Date posted :</b><?php echo $row['TimeStamp']?>
<b>Author :</b> Admin
</div>
<?php
#Selecting comments corresponding to the post
$selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";
$commentResult = mysqli_query($connection,$selectCommentQuery)
or die ("Error in the query: ". mysqli_error($connection));
#renderinf the comments
echo '<div class="comment-block_' . $postid .'">';
while ($commentRow = mysqli_fetch_assoc($commentResult))
{
?>
<div class="commentcontainer">
<div class="commentusername"><h1>Username :<?php echo $commentRow['Username']?></h1></div>
<div class="commentcontent"><?php echo $commentRow['Content']?></div>
<div class="commenttimestamp"><?php echo $commentRow['Timestamp']?></div>
</div>
<?php
}
?>
</div>
<?php
if (!empty($_SESSION['userID']) )
{
?>
<form method="POST" class="post-frm" action="index.php" >
<label>New Comment</label>
<textarea id="comment" name="comment" class="comment"></textarea>
<input type="hidden" name="postid" value="<?php echo $postid ?>">
<input type="submit" name ="submit" class="submitComment"/>
</form>
<?php
}
echo "</div>";
echo "<br /> <br /><br />";
}
require_once("footer.php") ?>
Again the problem being is the first form works fine but the second one and onwaord dont work properly
try this:
var comment = $('.comment',form);
instead of
var comment = $('#comment');
That way you're targeting the textarea belonging to the form you're validating
ps.
remove the id's from the elements or make them unique with php, all element id's should be unique