how to get id value from bootstrap 4 dropdown from table row? - php

PHP Code:
<td>
<div class="dropdown">
<button class="btn btn-warning dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
More
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" id="demolist">
<a class="dropdown-item " id="achieve" value="<?php echo $value['user_login_id'] ?>">Achivements</a>
<a class="dropdown-item" id="skill" value="<?php echo $value['user_login_id'] ?>">Skills</a>
<!--
<a class="dropdown-item">Remove</a>
<a class="dropdown-item">View Applicant</a>
-->
</div>
</div>
</td>
Here I have a dropdown button in each row where each dropdown has a list of achievements / skills. When clicking an achievement or skill I want to pass that row's id to ajax.
How should I send with ajax?

Using this method you can easily get the dropdown value:
Pass the id or class by using the .value method.
var submitForm = function() {
var name = document.querySelector('.name').value;
console.log(name);
}
document.querySelector('.btn').addEventListener('click', submitForm);
<input type="text" placeholder="name" class="name"><button class="btn" value="submit">Submit</button>

I have solution like this code i think you enough smart to understand it but let me know if you cant get it.
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" id="demolist">
<a class="dropdown-item achivementClass" id="achieve" value="value1">Achivements</a>
<a class="dropdown-item" id="skill" >Skills</a>
<a class="dropdown-item">Remove</a>
<a class="dropdown-item">View Applicant</a>
</div>
This your script code
<script>
$(function() {
$('.achivementClass').click(function(){
id =$(this).attr('value');
console.log(id);
});
});
</script>

Related

Ajax returns only first result in a PHP while loop

I am building a chat application and when I click the available users list (which is generated from a php while loop) the chat area is supposed to carry the information of that user and previous chat history (if any). However, for some reason ajax onclick keeps returning just the first user on the list into the chat area.
I have tried different methods of getting each user's id from the users-list (a separate php file) and passing it to the active-chat area (another php file) using ajax. I believe I'm missing something or doing something wrong. Every suggestion or different approach will be very much appreciated.
User's List PHP Code:
<?php
// this php is an include in another php file if certain conditions are met.
while($users = mysqli_fetch_assoc($sql)){
// the list of users generated into the home page
$output .= '<li id="contact" class="get-user-chats">
<input type="hidden" id="getUserId" value="'.$users['user_id'].'"/>
<span class="avatar"><img src="'.$users['display_pic'].'" height="42" width="42" alt="Generic placeholder image" />
<span class="'.$users['online_status'].'"></span>
</span>
<div class="chat-info flex-grow-1">
<h5 class="mb-0">'.$users['full_name'].'</h5>
<p class="card-text text-truncate">
This is just a test message to see if the truncate feature works
</p>
</div>
<div class="chat-meta text-nowrap">
<small class="float-end mb-25 chat-time">4:14 PM</small>
<span class="badge bg-danger rounded-pill float-end">3</span>
</div>
</li>';
}
?>
Ajax Code:
$(document).ready(function(){
$('.users-list').click(function(){
//users-list is a class from the home page where the users list displays in a div
var $container = $(this).find(".get-user-chats");
var chatid = $container.find("#getUserId").val();
$.ajax({
url: './code/active-chat.php',
type: 'POST',
data: {
chatuserid: chatid
},
success: function(response){
//alert(chatid)
$('.get-active-chat-user').html(response);
},
dataType: 'text'
});
});
});
active-chat.php
<?php
// active-chat.php
session_start();
define('BASEPATH', true);
require "../../../../private/engine.php";
$chatid = $_POST['chatuserid'];
$sql = "SELECT * FROM users WHERE user_id = '{$chatid}'";
$fetchdata = mysqli_query($connection, $sql);
while($sub = mysqli_fetch_array($fetchdata)){
?>
<header class="chat-header">
<div class="d-flex align-items-center">
<div class="sidebar-toggle d-block d-lg-none me-1">
<i data-feather="menu" class="font-medium-5"></i>
</div>
<div class="avatar avatar-border user-profile-toggle m-0 me-1">
<img src="<?php echo $sub['display_pic']; ?>" alt="avatar" height="36" width="36" />
<span class="<?php echo $sub['online_status']; ?>"></span>
</div>
<h6 class="mb-0"><?php echo $sub['full_name'] ?></h6>
</div>
<div class="d-flex align-items-center">
<i data-feather="phone-call" class="cursor-pointer d-sm-block d-none font-medium-2 me-1"></i>
<i data-feather="video" class="cursor-pointer d-sm-block d-none font-medium-2 me-1"></i>
<!-- <i data-feather="search" class="cursor-pointer d-sm-block d-none font-medium-2"></i> -->
<div class="dropdown">
<button class="btn-icon btn btn-transparent hide-arrow btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i data-feather="more-vertical" id="chat-header-actions" class="font-medium-2"></i>
</button>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="chat-header-actions">
<a class="dropdown-item" href="#">View Contact</a>
<a class="dropdown-item" href="#">Mute Notifications</a>
<a class="dropdown-item" href="#">Block Contact</a>
<a class="dropdown-item" href="#">Clear Chat</a>
<a class="dropdown-item" href="#">Report</a>
</div>
</div>
</div>
</header>
<?php } ?>
Screenshot of the Chat Screen:
Chat Screen
I have researched on related topics but none of them seems to address my specific issue.

How do I access the value of the link button outside the form and pass it into the form via post method?

This are the two options. I want to select one of the button link and have to get the value via post method when I submit the form
Please, how do I go about it? To catch the value of the link having a data-value for rent if it is clicked and vice versa for the data-value having for-sale, and then when I click on the submit button, I want to catch that value and relate it to the search button.
<div class="mxw-670 position-relative z-index-2">
<input class="search-field" type="hidden" name="status" value="for-sale" data-default-value="">
<ul class="nav nav-pills property-search-status-tab">
<li class="nav-item" role="presentation">
<a class="nav-link btn shadow-none rounded-bottom-0 fs-13 letter-spacing-087 bg-dark-opacity-05 text-white hover-white text-uppercase bg-active-primary active"
data-toggle="pill" data-value="for-sale" href="#" role="tab" aria-selected="true" form="message">
sale
</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link btn shadow-none rounded-bottom-0 fs-13 letter-spacing-087 bg-dark-opacity-05 text-white hover-white bg-active-primary text-uppercase"
data-toggle="pill" data-value="for-rent" href="#" role="tab" aria-selected="false" form="message">
rent
</a>
</li>
</ul>
<form action="" method="post" id="message" class="d-flex">
<div class="position-relative w-100">
<i class="far fa-search text-dark fs-18 position-absolute pl-4 pos-fixed-left-center"></i>
<input type="text"
class="rounded-bottom-right-lg w-100 pl-8 py-4 bg-white border-0 fs-13 font-weight-500 text-gray-light rounded-0 lh-17"
placeholder="Enter an address, neighborhood" name="search">
</div>
<button type="submit" name="submit" class="btn btn-primary fs-16 font-weight-600 rounded-left-0 rounded-lg">
Search
</button>
</form>
</div>
Make your button an input type button and submit the form with jQuery Ajax call
<input type="button" id="submitButton" name="submit" class="btn btn-primary fs-16 font-weight-600 rounded-left-0 rounded-lg" value="Search" />
And in your javascript code,
$('input#submitButton').click( function() {
$.post( 'some-url', $('form#myForm').serialize(), function(data) {
// ... do something with the response from the server
},
'JSON' // I expect a JSON response
);
});
$('input#submitButton').click( function() {
$.ajax({
url: 'some-url',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
// ... do something with the data...
}
});
});
In the data field in the ajax call, you can add the value of the link having a data-value attribute using $(element).attr('data-value')

Simple food tracker using ajax and jquery

I am trying to implement a simple food tracker for my restaurant website which updates food status. What I want to do is that whenever I update the food status for an order, the icon becomes green for that stage. I need to perform a query from my website and get the food status value and update it without page refresh. How do I do that? Please help me, it is very important
<div class="progressbar">
<ul >
<li>
<img style="width:130px;height:130px;margin-left:100px"src="foodStatusImages/waiting.png">
<i style="margin-left:150px" class="fa fa-check" id="iconstatus"></i>
<h5 style="margin-left:80px;display: inline-block;white-space: nowrap;">Waiting for
confirmation</h5>
</li>
<li>
<img style="width:130px;height:130px;margin-left:200px;"src="foodStatusImages/confirmed.png">
<i style="margin-left:250px" class="fa fa-check" id="iconstatus"></i>
<h5 style="margin-left:190px;display: inline-block;white-space: nowrap;">Order Confirmed</h5>
</li>
<li>
<img style="width:130px;height:130px;margin-left:350px"src="foodStatusImages/preparing.png">
<i style="margin-left:400px"class="fa fa-check" id="iconstatus"></i>
<h5 style="margin-left:350px;display: inline-block;white-space: nowrap;">In preparation...
</h5>
</li>
<li>
<img style="width:130px;height:130px;margin-left:530px;"src="foodStatusImages/Ready.png">
<i style="margin-left:590px" class="fa fa-check" id="iconstatus"></i>
<h5 style="margin-left:520px;display: inline-block;white-space: nowrap;">Your order is
ready</h5>
</li>-->
<ul>
</div>
<script>
$(document).ready(function()
{
$(function()
{
$("ul li:nth-child(1) i.fa").css("background","#148e14");
var icons = $('#iconstatus'); //does not work
console.log(icons);
$.ajax({
url:"action.php",
data:{icons:icons},
cache:false,
method:"POST",
sucess:function(result)
{
}
});
}
);
});
</script>

using jQuery get the selected list value from dynamically created item

im trying to get the value of selected item, my items are dynamically generated
foreach ($result as $value){
?>
<a href="#" class="badge badge-info filteredcat" value="<?php echo $value['id'] ?>">
<?php echo $value['categorie'] ?>
</a>
<?php
}
?>
so after it loads the html looks like this
<a href="#" class="badge badge-info filteredcat" value="5">
Semnalizare rutieră </a>
<a href="#" class="badge badge-info filteredcat" value="4">
Probleme de mediu </a>
<a href="#" class="badge badge-info filteredcat" value="6">
Spații verzi </a>
<a href="#" class="badge badge-info filteredcat" value="7">
Câini comunitari </a>
using jquery i want to get the value of selected item
$('.filteredcat').click(function(){
let categorie = $(this).val();
//$.fn.startLoader();
console.log(categorie);
});
but i cant get the value ... if i use $(this).text() its working ... but not the value
You should be able to grab it with let categorieVal = $(this).attr("value"); since it is one of the elements' attributes. See .attr() | jQuery API Documentation

bootstrap input field and dropdown button submit mail form

I have the following problem. I have a form where I can choose from three options. The problem is that it does not send the selected option while the other sections Name, surname, etc. send. Below I am sending the code:
thank you in advance
<div class="col-sm-4">
<div class="dropdown">
<button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="drp-name" data-bind="label">Choose</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="service-type">
<li>x1</li>
<li>x2</li>
<li>x3</li>
</ul>
</div>
</div>
php code:
<?php
$to = 'xxx#xx.com';
$subject = "Appointment Form";
$servicetype = stripslashes($_REQUEST['service-type']);
$msg = "";
$msg .= "Service Type: ".$servicetype."\r\n\n"; // add sender's sources to the message
$mail = #mail($to, $subject, $msg, "From:".$email);
if($mail) {
header("Location:index.html");
} else {
echo 'Message could not be sent!';
}
?>
I also added JS but it still does't work
Dropdown Select
$( document.body ).on( 'click', '.dropdown-menu li', function( event ) {
var $target = $( event.currentTarget );
$target.closest( '.dropdown' )
.find( '[data-bind="label"]' ).text( $target.text() )
.end()
.children( '.dropdown-toggle' ).dropdown( 'toggle' );
return false;
});
HTML:
<div class="col-sm-4">
<div class="dropdown">
<button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="drp-name" data-bind="label">Rodzaj usługi</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="service-type">
<li>x1</li>
<li>x2</li>
<li>x3</li>
</ul>
</div>
<input type='hidden' name='service-type'> <!-- ADD THIS -->
</div>
JavaScript:
$(document.body).on('click', '.dropdown-menu li', function(event) {
event.preventDefault();
var $target = $(event.currentTarget);
$target.parents('.dropdown').sibling('[name=service-type]').val($target.text());
});
you cant pass values to php without name attribute and form tag . you can directly set it with select tag
<form action="file.php" method=POST>
<select name=option>
<option value=0 selected>0</option>
<option value=1> 1</option>
</select><input type=button value=submit>
</form>
Suppose the name of form is mail_form, and there is a hidden form element to hold the choice information with id service-type-holder and name service-type. Then the following code could be used with inline Javascript:
<form name="mail_form" id="mail_form" action="" method="post">
<input type="hidden" name="service-type" id="service-type-holder">
<div class="col-sm-4">
<div class="dropdown">
<button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="drp-name" data-bind="label">Choose</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="service-type">
<li onclick="$('#service-type-holder').val('x1'); $('#mail_form').submit()">x1</li>
<li onclick="$('#service-type-holder').val('x2'); $('#mail_form').submit()">x2</li>
<li onclick="$('#service-type-holder').val('x3'); $('#mail_form').submit()">x3</li>
</ul>
</div>
</div>

Categories