How to Pass Knockout ahref value into jquery variable - php

Knockout js Data
<a data-bind="attr: { href: URLPath }" type="submit">Download</a>
I get Value of "A tag" as www.ex.com/sp/file.zip
script
function DataItem(data) {
this.URLPath = ko.observable(data.path); //www.ex.com/sp/file.zip
}
jQuery Script
$("input[type=submit]").click(function () {
$.post('fs.php', 'sp=' + $(this).val(), function (data) {
-----------
-----------
});
});
I need data-bind="attr: { href: URLPath }" (www.ex.com/sp/file.zip) Value in jQuery Variable "SP="(www.ex.com/sp/file.zip) is that possible?

function DataItem(data) {
this.URLPath = ko.observable(data.path);
}
var mymodel = new DataItem(data);
$(document).ready(function() {
ko.applyBindings(mymodel);
$("input[type=submit]").click(function (event) {
$.post('fs.php', 'sp=' + mymodel.URLPath(), function (data) {
});
});
});

Related

Hide data in url when using window.open in jquery

i was performed onclick to download pdf in below there is my jQuery code
$("#mlt_voucher").on("click", function () {
var checkedId = [];
var deduction = $('#dynamicmodel-deduction_type').val();
// alert(deduction);
$.each($("input:checkbox[class=chkItem]:checked"), function(){
checkedId.push($(this).attr("id"));
});
window.open('$deductiondown?checkedId='+checkedId+'&deduction='+deduction, '_blank');
});
there is the url look like
http://localhost/fads/admin/master-beneficiary/deductiondown?checkedId=29155,29152&deduction=1
i want hide my data in that url what can i do for that ?
and also i don't want to use that code in below
$("#mlt_voucher").on("click", function () {
var checkedId = [];
var deduction = $('#dynamicmodel-deduction_type').val();
alert(deduction);
$.each($("input:checkbox[class=chkItem]:checked"), function(){
checkedId.push($(this).attr("id"));
});
$.post(
'$deductiondown',
{ checkedId:checkedId,deduction:deduction},
// function(response){
// // console.log(response);
// location.reload();
// }
);
});

jQuery automatically updated value on view when button clicked

I was create like button with current value on database and when the button has been clicked the value must be automatically updated on the view.
Currently, the value was successfully updated on database, but not automatically updated on view.
Here is my jquery code:
<script type="text/javascript">
$(document).ready(function(){
$('#like').on('click', function(e){
var id = '{{$news->id}}';
$.get('{{ url('view/like')}}/'+id, function(data){
console.log(id);
console.log(data);
$('#like_data').empty();
$.each(data, function(index, element){
$('#like_data').append("<p>"+this.like+"</p>");
});
});
});
});
</script>
Here is my controller:
public function like($id){
$news = News::find($id);
$news->like += 1;
$news->save();
return $news;
}
You need to pass JSON response from your controller. Like this
public function like($id){
$news = News::find($id);
$news->like += 1;
$news->save();
return response()->json([$news], 200);
}
you don't need to use $.each() for this response because you send only single object response. Just simply write this code:
$('#like_data').html("<p>"+data.like+"</p>");
if you want to use each just simply write:
$.each(data, function (index, element) {
$("#like_data").append("<p>" + element.like + "</p>");
});
Try this
<script type="text/javascript">
$(document).ready(function(){
$('#like').on('click', function(e){
var id = '{{$news->id}}';
$.get('{{ url('view/like')}}/'+id, function(data){
console.log(id);
console.log(data);
$('#like_data').empty();
$.each(data, function (index, element) {
$("#like_data").append("<p>" + element.like + "</p>");
});
});
});
});
</script>

How to set selected value of jQuery chosen?

<script>
$(document).ready(function () {
$("#state").change(function () {
var stateId = $("#state").val()
phpurl = "sample_url" + stateId;
$.ajax({
url: phpurl,
success: function (data) {
$("#district").html(data).trigger("chosen:updated");
},
error: function(data) {
}
});
});
});
</script>
Above is my code to generate district based on state, and it works fine. I just want set the value what is selected by user. At present the selected value is cleared after selecting a state and submit.
You can use a global to hold selected value
var selectedDistrict = 0;
$("#district").change(function () {
selectedDistrict = $("#district").val();
});
then do:
$("#district")
.html(data)
.trigger("chosen:updated")
.val(selectedDistrict)
.trigger("chosen:updated");
in your ajax success.
Means your code will become:
<script>
var selectedDistrict = 0;
$(document).ready(function () {
$("#district").change(function () {
selectedDistrict = $("#district").val();
});
$("#state").change(function () {
var stateId = $("#state").val()
phpurl = "sample_url" + stateId;
$.ajax({
url: phpurl,
success: function (data) {
$("#district").html(data)
.trigger("chosen:updated")
.val(selectedDistrict)
.trigger("chosen:updated");
},
error: function(data) {
}
});
});
});
</script>

Wordpress AJAX call response 0

I'm tryng to set up a panel, this one
The menu on the top calls different ajax functions in order to display a thumb. Clicking on the thumb you can see the details in the last box of the panel.
I have this php functions
function GetPostPartner(){
$catPartner = "loop_pb_partner";
get_template_part($catPartner);
wp_die();
}
add_action('wp_ajax_nopriv_GetPostPartner', 'GetPostPartner');
function GetPostEnte(){
$catEnte = "loop_pb_ente";
get_template_part($catEnte);
wp_die();
}
add_action('wp_ajax_nopriv_GetPostEnte', 'GetPostEnte');
function GetPostColl(){
$catColl = "loop_pb_coll";
get_template_part($catColl);
wp_die();
}
add_action('wp_ajax_nopriv_GetPostColl', 'GetPostColl');
function GetPostMedia(){
$catMedia = "loop_pb_media";
get_template_part($catMedia);
wp_die();
}
add_action('wp_ajax_nopriv_GetPostMedia', 'GetPostMedia');
function GetPostDetails(){
$pb_details = $_POST['postURL'];
get_template_part($pb_details);
wp_die();
}
add_action('wp_ajax_nopriv_GetPostDetails', 'GetPostDetails');
And those are called by these ajax functions
$(document).delegate('h4.homus-partners-global-ajax[data-pb- cat*=pb_partner]', 'click', function(event) {
event.preventDefault();
var pb_cat = "pb_partner";
var data = {
'action': 'GetPostPartner',
catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
$( 'ul.homus-partners-section-slide' ).html(response);
});
});
$(document).delegate('h4.homus-partners-global-ajax[data-pb-cat*=pb_ente]', 'click', function(event) {
event.preventDefault();
var pb_cat = "pb_ente";
var data = {
'action': 'GetPostEnte',
catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
$( 'ul.homus-partners-section-slide' ).html(response);
});
});
$(document).delegate('h4.homus-partners-global-ajax[data-pb-cat*=pb_coll]', 'click', function(event) {
event.preventDefault();
var pb_cat = "pb_coll";
var data = {
'action': 'GetPostColl',
catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
$( 'ul.homus-partners-section-slide' ).html(response);
});
});
$(document).delegate('h4.homus-partners-global-ajax[data-pb-cat*=pb_media]', 'click', function(event) {
event.preventDefault();
var pb_cat = "pb_media";
var data = {
'action': 'GetPostMedia',
catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
$( 'ul.homus-partners-section-slide' ).html(response);
});
});
$(document).delegate('li.homus-partners-section-single', 'click', function(event) {
event.preventDefault();
var pb_post_id = $(this).data('post-id');
var data = {
'action': 'GetPostDetails',
postURL : "single_pb_post_details",
post_id: pb_post_id
};
$.post(ajaxURL, data, function(response) {
$( '.homus-partners-detalis' ).html(response);
console.log(pb_post_id);
console.log(data.postURL);
console.log(response);
});
});
the response that I have is always 0 even if the console of the last ajax call here above return the right postid. You can find the whole project in this repo
https://github.com/wanbinkimoon/homus-web.git
in the code
function GetPostPartner(){
$catPartner = "loop_pb_partner";
get_template_part($catPartner);
wp_die();
}
add_action('wp_ajax_nopriv_GetPostPartner', 'GetPostPartner');
instead of including the file by get_template_part paste the code here and then try

jQuery use ajax and json to switch page with button

Hi so I have a JS file with a function for my button, this button get value from different checkbox in a table. But now i want to get these value on another page (for invoice treatement).
Here is my Script :
$("#boutonfacturer").click(function () {
var checked = $('input[name="id_commande[]"]:checked');
var tab = [];
var jsonobj = {};
checked.each(function () {
var value = $(this).val();
jsonobj.value = value;
tab.push(jsonobj);
});
var data= { recup : tab };
console.log(data);
$.ajax({
type: 'POST',
url: 'genererfacture-facture_groupee.html',
data: data,
success: function (msg) {
if (msg.error === 'OK') {
console.log('SUCCESS');
}
else {
console.log('ERROR' + msg.error);
}
}
}).done(function(msg) {
console.log( "Data Saved: " + msg );
});
});
i use an MVC architecture so there is my controller :
public function facture_groupee() {
$_POST['recup'];
var_dump($_POST['recup']);
console.log(recup);
$this->getBody()->setTitre("Facture de votre commande");
$this->getBody()->setContenu(Container::loader());
$this->getBody()->setContenu(GenererFacture::facture_groupee());
and for now my view is useless to show.
I have probably make mistake in my code.
Thank you.
Nevermind after thinking, I have used my ajax.php page which get my another page thanks to a window.location :
my JS :
$("#boutonfacturer").click(function () {
var checked = $('input[name="id_commande[]"]:checked');
var tab = [];
checked.each(function () {
var value = $(this).val();
tab.push(value);
});
var data = {recup: tab};
console.log(data);
$.ajax({
type: 'POST',
url: 'ajax.php?action=facture_groupee',
data: data,
success: function (idfac) {
console.log("Data Saved: " + idfac);
var id_fac = idfac;
window.location = "ajax.php?action=facture_groupee=" + id_fac;
}
});
});
my php :
public function facture_groupee() {
foreach ($_POST['recup'] as $p){
echo $p; }

Categories