show data in div on radio button click through ajax - php

I want to show selected data by user on ajax call.
Scenario is that I have two radio buttons, and when user clicks on either one it should show the respective data on the right side, in the response div.
HTML:
<form id="package-call" method="post" accept-charset="utf-8">
<div class='pakges'>
<div class="sub-pakge">
<div class="sub-pakge-content">
<span class="content-title">North America</span><br>
<span class="content-title">Basic</span>
<div class="content-time">150 Minutes</div>
<span class="content-price">Rs.500</span>
<div class="pakges-radio"><input type="radio" name="package" id="package" > </div>
</div>
</div>
<div class="sub-pakges">
<div class="sub-pakges-content">
<span class="content-title">North America</span><br>
<span class="content-title">Max</span>
<div class="content-time">400 Minutes</div>
<span class="content-price">Rs.200</span>
<div class="pakges-radio"><input type="radio" name="package" id="package"> </div>
</div>
</div>
</div>
</form>
AJAX:
$("#package").click(function(e){
$.ajax( {
type: "POST",
url: "package _selection.php",
data: $('#package_call').serialize(),
success: function( response ) {}
});
});
This is the div where i want to show my response:
<div class="form-results">
<div class="rslt-content-main">
<div class="rslt-content">Selected Pakages / Bolts</div>
<div id="package_response"></div>
</div>
</div>
And these are the result div's that i want to get through ajax call from package _selection.php and want to display respectively
<div class="radio_one_rslt">
<div>voice:</div><br />
<div>IDD Asia - Rs 200</div>
</div>
<div class="radio_two_rslt">
<div>voice:</div><br />
<div>IDD Asia - Rs 500</div>
</div>

For listening to a radio button, use the 'change' event
$("#package").on('change', function(e){
Then, assuming your response is the HTML you want to append, append it in your success callback
$.ajax( {
type: "POST",
url: "package _selection.php",
data: $('#package_call').serialize(),
success: function( response ) {
$('#package_response').append(response);
}
});
A blog post for reference: http://blog.stephenborg.com/?p=37

Related

Laravel/Ajax CRUD: show option value on edit button click

I have an edit modal with three drop down select options
Currently having trouble showing the appropriate option on edit button click, my input fields have no issue.
heres my working add function for reference, all three option tags are working in this way.
$(document).on('click', '.add_category', function(e){
e.preventDefault();
var category_data = {
'category_name': $('.category_name').val(),
'category_description': $('.category_description').val(),
'category_description': $('.category_description').val(),
'config_view_type': $('.config_view_type').val(),
'config_edit_type': $('.config_edit_type').val(),
'bbrmode_view_type': $('.bbrmode_view_type').val(),
}
//token taken from laravel documentation
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url: "/clinical/bbr-category-configuration",
data: category_data,
dataType: "json",
success: function (response){
console.log(response);
if(response.status == 400) {
$('#category_formCheck').html("");
$('#category_formCheck').addClass('alert alert-danger');
$.each(response.errors, function (key, err_values) {
$('#category_formCheck').append('<li>'+err_values+'</li>');
});
}
else
{
$('#category_notif').html("");
$('#category_notif').addClass('alert alert-success');
$('#category_notif').text(response.message);
$('#createCategory').modal('hide');
fetchcategory();
}
}
});
});
also, in my edit category button, I went ahead and used console.log to show that the option values can be read form the table:
I tried to add the three in my edit display
$(document).on('click','.edit_category',function (e) {
e.preventDefault();
var cat_id = $(this).val();
console.log(cat_id);
$('#editCategoryModal').modal('show');
$.ajax({
type: "GET",
url: "/clinical/bbr-category-configuration-edit/"+cat_id,
success: function (response) {
console.log(response);
if(response.status == 404) {
$('#success_message').html("");
$('#success_message').addClass('alert alert-danger');
$('#success_message').text(response.message);
} else {
$('#edit_category_name').val(response.category_edit.category_name);
$('#edit_category_description').val(response.category_edit.category_description);
$('#edit_cat_id').val(response.category_edit.category_id);
//new code
$('#edit_config_view_type').val(response.category_edit.config_view_typ);
$('#edit_config_edit_type').val(response.category_edit.config_edit_type);
$('#edit_bbrmode_view_type').val(response.category_edit.bbrmode_view_type);
}
}
});
});
modal fields:
<input type="text" id="edit_category_name" class="form-control form-group w-100" placeholder="Category Name">
<textarea id="edit_category_description" class="form-control w-100" placeholder="Category Description" cols="50" rows="10"></textarea>
<div class="container">
<div class="row mb-4">
<div class="col-md-4">
<small class="font-weight-bold">Config Access View Type:</small>
</div>
<div class="col-md-4">
<div class="form-group">
<select id="edit_config_view_type" class="edit_config_view_type form-control w-100" name="config_view">
{{-- <option id="edit_category_name" class="form-control form-group w-100" value="asdasd"></option> --}}
<option>Public</option>
<option>Restricted</option>
</select>
</div>
</div>
<div class="col-md-4 text-center">
<button id="edit_config_view_type_r" type="submit" class="btn btn-outline-secondary" data-toggle="modal" data-target="#new_category"><i class="fas fa-plus-circle "></i> Add Recipient</button>
</div>
</div>
</div>
tried adding <option id="edit_config_view_type" ></option> but to no avail.
Notes:
have the option value show up in the edit button like the name/description textfields.
the option needs to be the same as the column values from the table.
category_name and category_description are already read from the table, I just need to have that effect on a dropdown
also added a shortcut on the three columns when added: P & R and they get displayed as Public and restricted
This question is a bit on the advice side, any help would be appreciated, thank you

JQuery Form Validator's toggleDisabled module does not work on buttons that are not type 'submit'

I have a form that uses the jquery form validator plugin (http://www.formvalidator.net/) to perform client side pre-submit validation. I have the toggleDisabled module activated so that the submit button is disabled until all required fields are filled out and formatted correctly. My jquery then sends the form data to a processing page via ajax. The storeData.php code stores the data in a table. On success, the ajax should open a modal. I have verified that the data is being stored in my table.
The issue lies (I suspect) with the form submit button. In order for my toggleDisabled module to work correctly, the button has to be of type 'submit.' But because of the nature of a submit button the success function of my ajax is effectively being bypassed so that the modal will never be displayed.
I have tested this by changing the submit button to a regular button. At the expense of my toggleDisabled module not functioning this way, my modal is displayed.
I have found many solutions here for enabling/disabling buttons and also for preventing form submit by changing the button type to button. However, I want to use the validator module to disable/enable the button because it is designed to listen to the data-validation attributes for my form fields. But it won't work unless it's a submit button. Is there a simple solution that I'm overlooking?
index.php
<form method="post" name="talentForm" id="talentForm">
<div class="form-row">
<div class="col-auto redtext">*</div>
<div class="col">
<input type="text" id="first" class="form-control" placeholder="First name" data-validation="required">
</div>
<div class="col-auto"> </div>
<div class="col-auto redtext">*</div>
<div class="col">
<input type="text" id="last" class="form-control" placeholder="Last name" data-validation="required">
</div>
</div>
<div class="row rowtm20"></div>
<div class="form-row">
<div class="col-auto redtext">*</div>
<div class="col">
<input type="text" id="email" class="form-control" placeholder="E-mail" data-validation="email">
</div>
<div class="col-auto"> </div>
<div class="col-auto"> </div>
<div class="col">
<input type="text" id="phone" class="form-control" placeholder="Phone">
</div>
</div>
<div class="form-row">
<button type="submit" id="registerButton" class="btn btn-primary mb-2 biggertext">Register</button>
</div>
</form>
<script>
$.validate({
modules : 'security,toggleDisabled',
showErrorDialogs : false
});
$('#registerButton').on('click', function(){
var inputData = $('#last').val()+"|"+$('#fist').val()+"|"+$('#email').val()+"|"+$('#phone').val();
$.ajax({
type: 'post',
url: 'storeEntry.php',
data: {registration:inputData},
success: function(response){
if(response == "1"){
$("#thankyouModal").modal("show");
}
else{
alert("Error");
}
}
});
});
</script>
storeEntry.php
if(isset($_POST)){
$data = explode("|",$_POST['registration']);
$addRegistration = "insert into talent (Last,First,email,Phone) values('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."')";
$entry = $dbConn->query($addRegistration) or die ("Error performing addRegistration query: ".mysqli_error($dbConn));
if($entry){
echo "1";
} else{
echo "0";
}
}
Well, I found the answer. I added an argument to the click function and then called the preventDefault method, which will effectively 'turn off' the submit action of a submit button. This allows my toggleDisabled module in the validator to function correctly while also allowing my modal to appear and my ajax to execute. Hre is my revised click function:
$('#registerButton').on('click', function(e){
e.preventDefult();
var inputData = $('#last').val()+"|"+$('#fist').val()+"|"+$('#email').val()+"|"+$('#phone').val();
$.ajax({
type: 'post',
url: 'storeEntry.php',
data: {registration:inputData},
success: function(response){
if(response == "1"){
$("#thankyouModal").modal("show");
}
else{
alert("Error");
}
}
});
});

im not able to call my ajax request in new added div in angular js

i have a three dropdowns like country,state,city it's using ajax reuest.
then i add new dropdowns div using my buttion , myfirst dropdown div's data passed and displayed and it's work fine, but others div's dropdown data not passed and not display and ajax request can't send .
it's my ajax request
<script>
function getstatedetails(id)
{
//alert('this id value :'+id);
$.ajax({
type: "POST",
url: 'ajax_get_finish/'+id,
data: id='cat_id',
success: function(data){
// alert(data);
$('#old_state').html(data);
},
});
}
function getcitydetails(id)
{
$.ajax({
type: "POST",
url: 'ajax_get_size/'+id,
data: id='st_id',
success: function(data){
$('#old_city').html(data);
},
});
}
it's my angular js code
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('shanidkvApp', []);
app.controller('MainCtrl', function($scope) {
$scope.choices = [{id: 'choice1'}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removeChoice = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};
});
and its my html
<div class="content-wrapper" ng-app="shanidkvApp">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Add Challan Details
<i class="fa fa-list"></i> challan Details List
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row"> <div class="col-md-12 display_alert"></div></div>
<div class="row">
<div class="col-md-6">
<div class="box box-primary" ng-app="angularjs-starter" ng-controller="MainCtrl">
<div class="box-body table-responsive">
<form action="insert" method="post" data-parsley-validate novalidate enctype="multipart/form-data">
<div data-ng-repeat="choice in choices">
<div class="form-group">
<label for="form-address">Model Name</label>
<select name="country_details" class="form-control countries" id="country_details" onChange="getstatedetails(this.value)">
<option value="" selected="selected" >Select Model</option>
<?php foreach($groups as $count): ?>
<option value="<?php echo $count->model_id; ?>"><?php echo $count->model_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="form-address">Finish Name</label>
<select name="select_state" class="form-control countries" id="old_state" onChange="getcitydetails(this.value)">
<option selected="selected">Select Finish</option>
</select>
</div>
<div class="form-group">
<label for="form-address">Size</label>
<select name="selectcity" class="form-control countries" id="old_city" >
<option selected="selected">Select Size</option>
</select>
</div>
<div class="btn btn-primary" ng-show="$last" ng-click="removeChoice()">Remove</div>
</div>
<div class="btn btn-primary" ng-click="addNewChoice()">Add fields</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
</section>
<!-- /.content -->
data in ajax should be in object form
function getstatedetails(id)
{
//alert('this id value :'+id);
$.ajax({
type: "POST",
url: 'ajax_get_finish/'+id,
data: id='cat_id',
success: function(data){
// alert(data);
$('#old_state').html(data);
},
});
}
pls try
$.ajax({
...
data: { id: 'cat_id' }
})
Stop using JQuery and AngularJS together.
If you want your data, use AngularJS too.
For instance,
app.factory('requestFactory', function($q, $http) {
var ret = {
getStateDetails: getStateDetails,
getCityDetails: getCityDetails
};
return ret;
function getStateDetails(id) {
//Use of promises
var deffered = $q.defer();
$http.post('ajax_get_finish/' + id, {id: 'cat_id'})
.then(function(success) {
deferred.resolve(success.data);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
}
function getCityDetails(id) {
//Use of promises
var deffered = $q.defer();
$http.post('ajax_get_size/' + id, {id: 'st_id'})
.then(function(success) {
deferred.resolve(success.data);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
}
});
Then, load your data in your controller, and use the two-way binding to display it.

textboxes inside loop only first one returns value jquery php

i need to put quantity for my products on my database. My products are displayed inside a foreach loop including the textbox for quantity. but only the first textbox is working. Can you please help me? Thanks a lot!
<div class="col-md-9 cartbox">
<?php foreach($result as $row): ?>
<div class="col-md-12 smallbox">
<div class="col-md-3 item_in_box">
<div class="cart_img_div">
<img class="img-responsive prodimage" src="<?php echo $row['prod_image']; ?>">
</div>
</div>
<div class="col-md-3 item_in_box">
<div class="cart_prod_name" data-name="<?php echo $row['prod_name']; ?>">
<?php echo strtoupper($row['prod_name']); ?>
</div>
</div>
<div class="col-md-3 item_in_box">
Quantity: <input type="text" class="cart_txtquantity"/><br>
<input type="button" class="btn btn-warning updateqty" value="Update"/>
</div>
<div class="col-md-3 item_in_box">
<div class="cart_prod_price">
<input type="hidden" class="hidden_price" value="<?php echo $row['price']; ?>"
<b> PHP:</b> <?php echo $row['total_amount']; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<script>
$(document).ready(function () {
$(".updateqty").click(function () {
$.ajax ({
url: "../controller/order/updatequantity.php",
method: "POST",
data: {
prod_name: $(".cart_prod_name").attr('data-name'),
quantity: $(".cart_txtquantity").val(),
price: $(".hidden_price").val()
},
success: function(result) {
alert(result);
},
error: {
function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
}
});
});
});
</script>
When you are selecting the values for the ajax, you are searching the entire document for the classes cart_prod_name, cart_txt_quantity, and hidden_price. If you look look at the ajax result carefully, you will see that it is always the data from the first <div class=smallbox"> in the request. This is because when you call .val() on a jQuery selection of more then one object, it returns the value of the first selected object (the value from the first row).
So, to fix this, you need a context (subset of nodes in the DOM) for the query to search for the specific classes.
For example
$(document).ready(function(){
$(".updateqty").click(function () {
var context = $(this).parents('.smallbox');
$.ajax ({
...
data: {
prod_name: $(".cart_prod_name", context).attr('data-name'),
quantity: $(".cart_txtquantity", context).val(),
price: $(".hidden_price", context).val()
},
...
});
});
});

Show HTML for specific div on click

I have some HTML and Ajax set up so that if you click a certain image (the reply-quote class below), it initiates some Ajax to echo HTML elsewhere on the page.
As shown below, the HTML file contains the same block of divs multiple times. My problem is that, if a user clicks the image, how can I make Ajax show the HTML (within show-reply div) for that specific block and not all of them?
<!-- BLOCK ONE -->
<div class="style1">
<div class="style2">
<img src="image.png" class="reply-quote" />
<div class="style3">
<div class="style4">
<div id="show-reply"></div>
<div class="reply-box">
<form class="reply-container no-margin" method="POST" action="">
<textarea class="reply-field" name="new_comment" placeholder="Write a reply..."></textarea>
<button name="submit" class="btn" type="submit">Post</button>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- BLOCK TWO -->
<div class="style1">
<div class="style2">
<img src="image.png" class="reply-quote" />
<div class="style3">
<div class="style4">
<div id="show-reply"></div>
<div class="reply-box">
<form class="reply-container no-margin" method="POST" action="">
<textarea class="reply-field" name="new_comment" placeholder="Write a reply..."></textarea>
<button name="submit" class="btn" type="submit">Post</button>
</form>
</div>
</div>
</div>
</div>
</div>
Here's the JQuery I have right now:
$(document).ready(function() {
$(".reply-quote").click(function() {
$.ajax({
url: 'assets/misc/show_reply.php', // this file simply echoes back the HTML to the `show-reply` div
type: "POST",
data: {post_id: $(this).attr('id')},
success: function(data) {
$("#show-reply").append(data); // this is where the problem lies
}
});
});
});
To my understanding, I have to somehow implement $(this), parent(), find(), etc. instead of the current line I'm using ($("#show-reply").append(data);). The question is, what should that line be so that if I click the image, it only shows the HTML for that specific block?
Please help!
First: ID should be unique in a document, use class attribute instead for show-reply and other elements where id is repeated
<div class="show-reply"></div>
then you need to find the show-reply next to the clicked reply-quote image
$(document).ready(function() {
$(".reply-quote").click(function() {
var $reply = $(this);
$.ajax({
url: 'assets/misc/show_reply.php', // this file simply echoes back the HTML to the `show-reply` div
type: "POST",
data: {post_id: $(this).attr('id')},
success: function(data) {
$reply.closest('.comment-container').find(".show-reply").append(data);
}
});
});
});
try this
$("div").click(function(e) {
if($(e.target).is('.reply-quote')){
e.preventDefault();
return;
}
alert("work ");
});
You should change the id="show-reply" to class="show-reply" in html, and then in JS change $("#show-reply").append(data); to $(this).parent(".show-reply").append(data);

Categories