How to give the value in data jquery? - php

I now am working with my project. I have the problem with the data. I have knowledge on jquery
$(document).ready(function(){
$("input[type='radio']").click(function(){
var price = $(this).val();
add_payment_value(price);
});
});
function add_payment_value(price){
// here you can use $.ajax function to add your 'price value' to your cart
$.ajax({
type: "POST",
url: "add_payment.php",
// file where you can add price to your database
data: "",
success: function(){} // return something on success
});
}
I have 4 values which is stored in radio buttons ordered 4, 6.5, 8, 11. My question is how to write the values in data and var price = $(this).val();? Can anyone fill this? I gave some value but that is not working.

Change the add_payment_value function to accept a name and then uaw the code below:
$(document).ready(function(){
$("input[type='radio']").click(function(){
var price = $(this).attr("value");
var name = $(this).attr("name");
add_payment_value(price, name);
});
});
function add_payment_value(price, name){
// here you can use $.ajax function to add your 'price value' to your cart
$.ajax({
type: "POST",
url: "add_payment.php", // file where you can add price to your database
data: name + "=" + price,
success: function(){} // return something on success
});
}
Another alternative is to format the data string in the click handler and send it to add_payment_value function
$(document).ready(function(){
$("input[type='radio']").click(function(){
var price = $(this).attr("value");
var name = $(this).attr("name");
add_payment_value(name + "=" + price);
});
});
function add_payment_value(priceData){
// here you can use $.ajax function to add your 'price value' to your cart
$.ajax({
type: "POST",
url: "add_payment.php", // file where you can add price to your database
data: priceData,
success: function(){} // return something on success
});
}

use radio button id like ("#rb1").value instead of this.

Related

Form loaded via ajax wont append data to parent window

So I am basically creating a Restaurant Menu editor for their website. The issue I am having is when the click a category named Brunch, I load the file edit_cat.php via ajax onto the page. All data is passed correctly, but on success of the form being filled out I would like to post a success message on the parent window and am having no luck. Coincidentally, upon success the alert(data) does popup with the response.
edit_cat.php both handles the form submission and is the form
$(document).ready(function(){
$('.editBtn').on('click', function(e) {
e.preventDefault();
var $this = $(this),
id = $this.data('id'),
type = $this.data('type');
if (type == 'cat') {
data = "&cat=" + id;
$.ajax({
data: data,
type: 'POST',
url: 'edit_cat.php',
success: function(data) {
$('#contentLeft').html(data);
}
});
}
});
$('#contentLeft').on('click', 'input[type="submit"]', function(e) {
e.preventDefault();
var $this = $(this),
frm = $('#edit-cat-frm'),
id = $('form').data('id'),
name = $(frm).find('input[name="name"]').val(),
desc = $(frm).find('textarea[name="desc"]').val(),
send = $(frm).find('input[name="submit"]').val();
data = "&cat=" + id + "&name=" + name + "&desc=" + desc + "&submit=" + send;
$.ajax({
data: data,
type: 'POST',
url: 'edit_cat.php',
success: function(data) {
window.parent.$('.left-container-head').innerHtml(data);
}
});
});
});
Can you please try this as simply,
$('.left-container-head').html(data);
Try this:
$(".left-container-head", window.parent.document).html(data);

jQuery, ajax multiple checkboxes into $_POST array

I'm trying to send $_POST data to another page to add sessions for a simple shopping cart.
I have a multiple forms within a PHP while loop each with multiple checkboxes, everything works apart from the checkboxes.
My question is how do I change this piece of code to change "item_extras" into an array?
if(this.checked) item_extras = $(this).val();
I have tried the following but, this just creates one line with all the values instead of another row within the array. If this is too confusing I could create a sample if it helps.
if(this.checked) item_extras += $(this).val();
$('form[id^="add_item_form"]').on('submit', function(){
//alert("On Click Works");
event.preventDefault();
addItem($(this));
});
function addItem(ele) {
//alert("I'm in the addItem Function");
var item_id = ele.parent().parent().find("input[name=item_id]").val(); // get item id
var item_name = ele.parent().parent().find("input[name=item_name]").val(); // get item name
var item_options = ele.parent().parent().find('#options').val(); // get selected option
var item_extras = "";
$item_extras = ele.parent().parent().find('input[name^="extra"]'); // find all extras
$item_extras.each(function() {
if(this.checked) item_extras = $(this).val(); // how do i make this into an array???
});
alert("BEFORE AJAX");
var dataString = 'item_id=' + item_id + '&item_name=' + item_name + '&item_options=' + item_options + '&item_extras[]=' + item_extras;
alert(dataString);
$.ajax({
type: "POST",
cache: false,
url: "includes/cart.php",
data: dataString,
success: function () {
$.ajax({
url: 'includes/cart.php',
success: function(data) {
$('#cart').html(data);
alert("AJAX SUCCESS");
}
});
}
});
return false;
}
you can use serialize method. Form.serialize()
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
var data = $(this).serialize();
});

jQuery, items not being deleted form cart correctly

I'm quite new to jQuery. I am building a shopping cart and trying to delete products from a cart. If I have one item in the basket and then remove it, it will disappear, but if I add another item to the cart, the item I deleted will reappear. If I have a card with more than one object, when I attempt to delete it, it will only change the total.
$('.addToCart').click(function(){
var id = this.id;
var price = parseFloat($('#priceAdd-'+id).val()).toFixed(2);;
var name = $('#name-'+id).val();
var total =(parseFloat($('#total').text())+parseFloat(price)).toFixed(2);
cartSize++;
$('#product-'+id).fadeOut('fast');
$('#added-'+id).delay(500).fadeIn('fast');
$.ajax({
type: "POST",
url: 'resources/addToCart.php',
data: { itemId: id , name: name, price: price},
cache: false,
success: function(html){
$("ol").append(html).slideDown("slow");
$('#noItems').fadeOut('fast');
$('#total').html(total);
$('#mainCart').fadeIn('fast');
}
});
});
$('ol').on("click",'.del',function(e){
var id= e.target.id;
var productId = $('#productId-'+id).text();
var productPrice = $('#price-'+id).text();
var total = $('#total').text();
var newPrice = (parseFloat(total) - parseFloat(productPrice)).toFixed(2);
var dataString = 'id='+ id;
cartSize--;
alert(id);
$('#total').html(newPrice);
$("#cart-"+id).remove();
$.ajax({
type: "POST",
url: "resources/del.php",
data: { id: id },
cache: false,
success: function(html){
$('#added-'+productId).fadeOut('Fast');
$('#product-'+productId).delay(600).fadeIn('Fast');
if(cartSize == 0){
$('#noItems').fadeIn('Fast');
$('#mainCart').fadeOut('Fast');
}
}
});
return false;
});
Any input would be appreciated
Thank you
edit: is there anyone else who can help me?
I think when you are calling to "resources/del.php", this is not deleting the item in Server (only visually) and then when you add another with "resources/addToCart.php", this is returning ALL the items (even the one you think you remove)

there is something wrong with this Jquery post?

im trying to pass on the id attribute to the file.php, but its giving me 0 every time, when i try to insert it into the database, the javascript and the html is provided!
$(function() {
$(".follow").click(function(){
var element = $(this);
var I = element.attr("id");
var info = 'id=' + I;
$.ajax({
type: "POST",
url: "file.php",
data: info,
success: function(){}
});
$("#follow"+I).hide();
$("#remove"+I).show();
return false;
});
});
html file:
<div id="follow1"><span class="follow_b"> Follow </span></div>
p.s. it deos insert the value in the database
file.php:
<?php
$id =$_POST['id'];
msql_insert.........
?>
It may not matter in this case, but the ID of an element is not supposed to start with a number.

Rebind dymanically created forms after jQuery ajax response

I'm kinda new to jQuery but understand it for the most part. My problem is that when my ajax call which refreshes the entire div is done, all my dynamically created forms don't work. If you try and submit them, the event doens't work properly and just tries to do a normal form submit. I have all the other items such as links bound using the .live() which seem to work great. Just the form dies.
How do I rebind the dynamically created forms after the ajax call? They all have id of formname_id. I tried to use bind but it doesn't work as below. Any help is appreciated.
Here is the code
jQuery(document).ready(function(){
jQuery("form[id^='commentform_']").each(function(){
var id = parseInt(this.id.replace("commentform_", ""));
jQuery(this).bind('submit', function(e) {
var action = jQuery('#action_' + id).attr('value');
var act_id = ('1');
jQuery.ajax({
type: "POST",
url: "ajax/modify.php",
data: "action="+ action +"& act_id="+ act_id,
success: function(response){
jQuery('#CommentsContainer_' + id).html(response);
jQuery('#commentform_' + id)[0].reset();
}
});
return false;
});
});
});
Try doing something like this:
jQuery("form[id^='commentform_']").live('submit',function(){
var id = parseInt(this.id.replace("commentform_", ""));
var action = jQuery('#action_' + id).attr('value');
var act_id = ('1');
jQuery.ajax({
type: "POST",
url: "ajax/modify.php",
data: {"action": action, "act_id": act_id},
success: function(response){
jQuery('#CommentsContainer_' + id).html(response);
jQuery('#commentform_' + id)[0].reset();
}
});
return false;
});
No need to loop over the forms to bind to them. If you can use delegate instead of live do so.
Why don't you over-ride the normal form submit:
function addNewitem() {
$('#new_item_form').submit(function() {
$.get("includes/ItemEdit.php", {
newItem: true
},
function(msg) {
isNewItem = true;
$("#new_item").hide();
$('#item_list').hide();
$("#item_edit").html( msg );
$("#item_edit").show();
editActiveEvent();
});
return false;
});
}
Don't forget to return false. or do a .preventDefault
I have gotten this to work adding the event in the function call and using event.preventDefault(); BUT of course only in FF. Doesn't work in IE7..
jQuery("form[id^='commentform_']").live('submit',function(event){
var id = parseInt(this.id.replace("commentform_", ""));
var action = jQuery('#action_' + id).attr('value');
var act_id = ('1');
jQuery.ajax({
type: "POST",
url: "ajax/modify.php",
data: {"action": action, "act_id": act_id},
success: function(response){
jQuery('#CommentsContainer_' + id).html(response);
jQuery('#commentform_' + id)[0].reset();
}
});
event.preventDefault();});
But IE7 still tries to sumbit the action. arrgggh.. Anything I'm doing wrong??

Categories