How use JQuery closest() in Post and comment system - php

I have a post and comment system. Something like facebook post and comment system. The post display properly but the comments only display for the first post i.e the post displayed at the top. Am on able to submit comment on all other posts except the first post.
The problem on which I need your assistance is as follows:
-The comments for each of the posts should display correspondently.
-To be able to submit comments for the other posts.
These what I have done.
View:
<div class="box-footer" style="display: block;">
<form id="com" class="com" method="post">
<div class="img-push">
<input type="hidden" class="status_id" id="status_id" name="status_id" value="<?php echo $post['spid']; ?>">
<textarea name="comment" id="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"></textarea>
<div class="box-footer box-form">
<btn class="btn btn-azure btn-sm pull-right commentbt" id="commentbt">Comment</btn>
<ul class="nav nav-pills">
<li><i class="fa fa-bullhorn"></i></li>
</ul>
</div>
</div>
</form>
</div>
<?php endforeach;?>
Jquery:
$(document).ready(function(){
$(".commentbt").click(function(){
var comment = $(this).closest("div.img-push").find("input[name='comment']").val();
alert(comment);
});
});
All am getting is "Undefined"
I need to get the value of the input field and the textarea. Thanks

I can notice, initially, the next errors on your code:
1) You forgot the close div for <div class="box-footer" style="display:block;">
2) The attribute name of the input element is status_id, not comment
3) The usage of the same ID attribute for multiple elements.
4) On JQuery, I will replace the search of the input by matching by class. Also, I added the search of textarea value too.
So, try next modifications:
PHP
<?php foreach ($statuspost as $post): ?>
<div class="box-footer" style="display:block;">
<form class="com" method="post">
<div class="img-push">
<input type="hidden" class="status_id" name="status_id" value="<?php echo $post['spid'];?>">
<textarea name="comment" class="form-control input-sm comment" placeholder="Press enter to post comment"></textarea>
<div class="box-footer box-form">
<btn class="btn btn-azure btn-sm pull-right commentbt">Comment</btn>
<ul class="nav nav-pills">
<li><i class="fa fa-bullhorn"></i></li>
</ul>
</div>
</div>
</form>
</div>
<?php endforeach:?>
JQuery
$(document).ready(function()
{
$(".commentbt").click(function()
{
var statusID = $(this).closest("div.img-push").find("input.status_id").val();
alert(statusID);
var comment = $(this).closest("div.img-push").find("textarea.comment").val();
alert(comment);
});
});

Related

Cannot get value of bootstrap dropdown on Submit

I have the following code of which i want to get the value of the text displayed on the bootstrap dropdown button when i click on the submit button, and also the i want the POST to be sent to a codeigniter controller to perform certain operations on it.
I need help please.
<form method="post" action="#" id="user-order-form">
<div class="users-order col-md-5">
<label class=""> <strong>ORDER BY </strong></label>
<div class="dropdown form-control" style="padding:0px; margin-left: 20px; margin-right:10px">
<button class="btn btn-default dropdown-toggle form-control" type="button" id="user-order-dropdown" name="user-order-dropdown" data-toggle="dropdown">
Surname
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelled-by="user-order-dropdown" id="capton">
<li>Surname </li>
<li>First Name </li>
<li>Company Name </li>
</ul>
</div>
<label class="radio-inline"><input type="radio" name="order-radio" checked>ASC</label>
<label class="radio-inline"><input type="radio" name="order-radio">DESC</label>
<button type="submit" class="btn" name="order-submit-btn" id="order-submit-btn">Order</button>
</div>
</form>
This is the jquery code that displays the selected dropdown item:
$('.dropdown').each(function (key, dropdown) {
var $dropdown = $(dropdown);
$dropdown.find('.dropdown-menu a').on('click', function () {
$dropdown.find('button').text($(this).text()).append(' <span class="caret"></span>');
});
});
To get the value of dropdown: You need to create a hidden input field and every time the value of dropdown changes, you need to update
it.
To get the POST data in the controller: You need to create a controller(obviously) and a function within that controller. Then
give that URL to the form action attribute.
I've written a reference code for you with necessary explaining in the comments, see if it helps you.
View
<form method="post" action="<?php echo base_url('some_controller/some_function');?>" id="user-order-form">
<!-- give the action where controller name(some_controller) and function name(some_function) -->
<div class="users-order col-md-5">
<label class=""> <strong>ORDER BY </strong></label>
<div class="dropdown form-control" style="padding:0px; margin-left: 20px; margin-right:10px">
<button class="btn btn-default dropdown-toggle form-control" type="button" id="user-order-dropdown" name="user-order-dropdown" data-toggle="dropdown">
Surname
<span class="caret"></span>
</button>
<!-- make a hidden field, give it a default value(which is initially selected), give it a name(it will be used to get the post data)-->
<input type="hidden" name="dropdown" value="Surname"id="dropdown_input"/>
<ul class="dropdown-menu" aria-labelled-by="user-order-dropdown" id="capton">
<li>Surname </li>
<li>First Name </li>
<li>Company Name </li>
</ul>
</div>
<label class="radio-inline"><input type="radio" name="order-radio" checked>ASC</label>
<label class="radio-inline"><input type="radio" name="order-radio">DESC</label>
<button type="submit" class="btn" name="order-submit-btn" id="order-submit-btn">Order</button>
</div>
</form>
JQuery
$('.dropdown').each(function (key, dropdown) {
var $dropdown = $(dropdown);
$dropdown.find('.dropdown-menu a').on('click', function () {
$dropdown.find('button').text($(this).text()).append(' <span class="caret"></span>');
$('#dropdown_input').val($(this).text()); // change the value of hidden input field
});
});
Controller(Some_controller.php)
function some_function(){
$dropdown = $this->input->post('dropdown'); // get dropdown value
$order_radio = $this->input->post('order-radio'); // get radio value
/* Do whatever you want to with that data here(Eg - Save in DB) -- your logic */
}

Save option loaded with "foreach" and send var php

So, in my database, I have a table that can receive any amount of categories, and when the teacher is creating a course he has to choose from one of them, so I have to show them all and they are loaded into a "select" using a foreach. The problem I have is that the value is always the last one and I can't send to another PHP file to insert it into another table. It's easier with the code:
<form action="addCurso.php" method="post" name="addcurso" id="addcurso">
<h3 class="col-md-3">Curso:</h3>
<div class="col-md-9">
<input id="addCursoNome" class="form-control" type="text" name="curso" placeholder="Curso">
</div>
</div>
<div class="col-md-12">
<h3 class="col-md-3">Thumbnail:</h3>
<div class="col-md-9">
<input id="addCursoImg" class="form-control" type="file" name="pic">
</div>
</div>
<div class="col-md-12">
<h3 class="col-md-3">Categoria:</h3>
<div class="col-md-9">
<select class="form-control selectpicker">
Categoria
<?php
foreach ($categorias as $categoria) {
?>
<option class="col-md-12" value="<?php $categoria['ID']?>" ><?=$categoria['Nome']?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-md-12">
<button id="addCurso" type="submit" class="btn btn-custom btn-lg btn-block">Criar</button>
</div>
</form>
I only have 2 options:
ID | Nome
--------------------
1 | Programação
--------------------
2 | Matemática
It always sends ID: 2 doesn't matter what option I have selected and in addCurso.php I always recieve: null
your submit button is wrapped into a link (a), where you have the last category that was set in the foreach (addCurso.php?ID=<?=$categoria['ID'] ?>).. you should remove the a tag and let the form post the data to addCurso.php which is the form action. it should look like this: <button id="addCurso" type="submit" class="btn btn-custom btn-lg btn-block">Criar</button> without the a tag..
Honestly, the code you created is very messy, but I try to explain what you have:
When you do foreach in select, it will doing :
<option class="col-md-12" value="1">Programação</option>
<option class="col-md-12" value="2">Matemática</option>
And, it will send last record for your button/link, because the loop is complete :
<a href="addCurso.php?ID=2">
<button id="addCurso" type="submit" class="btn btn-custom btn-lg btn-block">Criar</button>
</a>
I don't understand why you need <a> (before <button>) to submit?
You will always recieve NULL, because you don't give a name for your select.

Cannot handle a form in php

I'm learning now php and i'm stuck in one place with handling form submit action.
Im my input i'm trying to store user name in $_GET['firstname'] variable. But it's empty. I mean, that after checking if $_GET['firstname'] isset I get false. Where is my mistake?
<body>
<div class="container">
<div class="section back">
<form class="form myform" action="addcustomer.php" method="get">
<span class="myformtitle">
Add new User
</span>
<div class="form-group">
<div class="col validate-input">
<span class="label-input">Firstname</span>
<input id="firstname" class="input myinput" type="text" name="firstname" placeholder="Enter your firstname" value="<?php if (isset($_GET['firstname'])) echo $_GET['firstname']?>">
<span class="focus-input"></span>
</div>
</div>
<div class="col col-btn">
<button type="button" name="submit" class="btn sb-btn btn-block">
<span class="btn-sp">
Submit
</span>
</button>
</div>
</form>
</div>
</div>
<?php
if (isset($_GET['firstname'])) {
echo '<script>console.log("' . $_GET['firstname'] . '")</script>';
} else {
echo '<script>console.log("no name")</script>';
}
?>
</body>
Change your button type from button to submit.
button types are for Javascript (JS).
submit types are used to process PHP (form) directives.

Submit button doesn't fire

My submit button suddenly stopped working. It submits data into a MySQL database.
While I was doing some design changes, it suddenly stopped working.
Are there any apparent errors/mistakes in the code below?
I'm a noob who's currently trying to learn some PHP and so on, so any help would be much appreciated. :)
<section id="moviesearch">
<!-- Section -->
<div class="subcribe2">
<div class="container">
<!-- Container -->
<div class="row">
<!-- Row -->
<div class="col-md-4">
</div>
<body ng-app="myApp">
<div ng-controller="MyCtrl" class="col-md-4">
<form class="form-watch-list-create">
<input required="required" placeholder="Movie title" type="text" class="form-control" typeahead="item.Title for item in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-min-length="2" typeahead-wait-ms="1000" typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html" ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</body>
</div>
<!-- End Row -->
</div>
<!-- End Container -->
</div>
<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label | typeaheadHighlight:query" style="width: auto;"></span>
({{match.model.Year}})
<!-- <img ng-src="{{match.model.Poster}}" width="120"> -->
</a>
</script>
<div ng-controller="MyCtrl">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
</section>
Edit
Here's the code that sends data into the MYSQL database:
function addWatchList() {
if (isset($_GET['addtowatchlist'])) {
$name = $_GET['name'];
$conn = dbmysql();
$query ="INSERT INTO watch_list values(null,'{$name}','{$_SESSION['user_id']}','NOW()')";
if (mysqli_query($conn,$query)) {
$last = "SELECT * FROM watch_list WHERE created_by = '{$_SESSION['user_id']}' order by id desc limit 1";
$data = mysqli_query($conn,$last);
while ($row = mysqli_fetch_assoc($data)) {
include "_view.php";
}
}else {
echo "An error occurred. Please try again.";
}
exit;
}
}
I am looking to your code, missing attribute action="somefile.php" and method="get" if you are planning on submitting it using the form, you should put it or if you are planning on submitting your code using javascript you can use <form onsubmit="myFunction()"> That is what you are missing. I am not seeing you addtowatchlist name from your input so that php can catch it to your isset.
The submit button isn't inside the form.
You're missing the </form> tag to end the <form>. But you have </div> that matches the <div> just before </form>, so it's implicitly closing the <form> tag as well. Then you have <input type="submit"> after it. Since the submit button isn't inside the form, and has no form tag associating it with the form, it doesn't know which form it should submit when you click on it.
Try this:
<form class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>
This puts the form around both columns.
Use the method attribute in form tag and end the form tag properly
<form role="form" method="get" class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>

jquery append() function not working

I am creating my own custom chat bar all the related coding i have done, database and all the php functions working fine but when after submit the chat input message i am trying to append the input box data with jquery append function its not working at all.
html:
<div class="chat-bar-wrapper">
<div class="chat-bar-head">
<div class="chat-bar-icons-top">
<i class="fa fa-minus"></i>
</div>
<div class="chat-bar-title">
<p>Chat Online</p>
</div>
</div>
<div class="chat-bar-form">
<div class="chat-bar-text-display" id="messages_output"></div>
<form class="message-box" id="chat-form">
<div class="chat-bar-text-inputs">
<div class="chat-bar-text-input">
<input type="text" class="form-control" name="messages_input" placeholder="Enter your text here">
</div>
<div class="chat-bar-text-enter">
<input type="submit" class="btn btn-warning form-control" value="#">
</div>
</div>
</form>
</div>
</div>
js:
$(function(){
$("#messages_output").load('ChatMsgLoad.php');
$("#chat-form").submit(function(){
$.post('chatMsgPost.php', $('#chat-form').serialize(), function(data){
$('#messages_output').append(data);
});
return false;
});
});
Try this appendTo() instead of append().
$( data ).appendTo( "#messages_output");
And check in the console whether data variable contains something.
Read more here appendTo() in jQuery

Categories