online shopping cart for payment - php

hi i want to know about the addcart shopping. im doing the payment process named TRER. i have a problem with clicking button.i setup every code is correct eventhough i could not see any change.
i mentioned my product in radio button, i have a 5 radio buttons which has different amount like 20$ 40$ 58.99$ 70$ and 100$. this is the value of 5 radio button. if i clicks the 2 nd button that amount should add to shopping cart.
i have the little confusion with this. i want to know the action on radio button.
<input name="rmr" type="radio" value="20" onclick="add_payment_value()" />
<input name="rmr" type="radio" value="40" onclick="add_payment_value()" />
<input name="rmr" type="radio" value="58.99" onclick="add_payment_value()" />
<input name="rmr" type="radio" value="70" onclick="add_payment_value()" />
<input name="rmr" type="radio" value="100" onclick="add_payment_value()" />
i want to know the ajax function. should i use jquery and ajax togather.
could guys any one post some code else idea.
Wishing you a happy NewYear
thanks in advance mariya

HTML:
<input name="rmr" type="radio" value="20" />
<input name="rmr" type="radio" value="40" />
<input name="rmr" type="radio" value="58.99" />
<input name="rmr" type="radio" value="70" />
<input name="rmr" type="radio" value="100" />
JS:
var rbRmr = $('input[name="rmr"]');
$(rbRmr).bind('change', function(ev) {
var amount = $(this).val();
$(rbRmr).attr('readonly', 'readonly'); //block until the query ends Ajax
$.ajax({
...
data: {value: amount},
complete: function(xhr, sts) {
$(rbRmr).removeAttr('readonly'); //unblock
},
...
});
});

You should try jQuery $.ajax function! If you want to add price to shopping cart you could do something like:
HTML:
<input name="rmr" type="radio" value="20" />
<input name="rmr" type="radio" value="40" />
<input name="rmr" type="radio" value="58.99" />
<input name="rmr" type="radio" value="70" />
<input name="rmr" type="radio" value="100" />
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_price.php", // file where you can add price to your database
data: "",
success: function(){} // return something on success
});
}

Related

I cant ajax post checkbox value in laravel 5.1

I have many checkbox and other input like text,select ,textarea ,... in my form
I post all data of inputs but checkbox not post data
I saw this link
In Laravel 5.1 Can't post value of checkbox
But I do not want to use hidden button
Is there another way ?????
My form
<form id='test-form'>
<input name='text1'>
<input name='text2'>
<textarea name='text3'></textarea>
<input type="checkbox" name="test1" >
<input type="checkbox" name="test2" >
<input type="checkbox" name="test3" >
<input type="checkbox" name="test4" >
<input type="checkbox" name="test5" >
<input type="checkbox" name="test6" >
<input type="button" id='send'>
</form>
<script>
$(document).on("click", "#send", function () {
$.ajax({
type: 'post',
url: 'add',
data: $("#test-form").serialize(),
success: function (result) {
$('#ajax_div').html(result);
},
})
})
</script>
Your checkboxes will not send any data because they do not have the value attribute.
Change your html to:
<input type="checkbox" name="test[]" value="1" >
<input type="checkbox" name="test[]" value="2" >
<input type="checkbox" name="test[]" value="3" >
<input type="checkbox" name="test[]" value="4" >
<input type="checkbox" name="test[]" value="5" >
<input type="checkbox" name="test[]" value="6" >
<input type="button" id='send'>
Note that I also changed the names to test[], this will send an array of all the checkboxes that are checked.
For example: I check checkbox 1 and checkbox 3, the value that the server will receive is in $_REQUEST['test'] (either POST or GET), and will be an array with content [1,3].
If none of the checkboxes is marked, the $_REQUEST['test'] will be not set which you can check server side using isset().

form submit with two actions

I am trying to create a page where the form submit will execute two actions. In index.php,First action is it will use ajax to send data to be saved in the database. Second action is it will change page to index1.php. The code I created successfully saves data to the database but it does not change to index1.php. What is the problem with my code?
index.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var radio1 = $("input[name=group1]:checked").val();
var radio2 = $("input[name=group2]:checked").val();
var radio3 = $("input[name=group3]:checked").val();
var radio4 = $("input[name=group4]:checked").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = '&submit1='+ radio1 + '&submit2='+ radio2 + '&submit3='+ radio3 + '&submit4='+ radio4;
if(radio1==''||radio2==''||radio3==''||radio4=='')
{
alert("Please Fill All Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
return false;
});
});
</script>
<form action="index1.php" method="post">
<hr>
<label>Alignment: </label>
<input type="radio" name="group1" value="5"> 5
<input type="radio" name="group1" value="4"> 4
<input type="radio" name="group1" value="3"> 3
<input type="radio" name="group1" value="2"> 2
<input type="radio" name="group1" value="1"> 1
<hr>
<label>Blend: </label>
<input type="radio" name="group2" value="5"> 5
<input type="radio" name="group2" value="4"> 4
<input type="radio" name="group2" value="3"> 3
<input type="radio" name="group2" value="2"> 2
<input type="radio" name="group2" value="1"> 1
<hr>
<label>Warp: </label>
<input type="radio" name="group3" value="5"> 5
<input type="radio" name="group3" value="4"> 4
<input type="radio" name="group3" value="3"> 3
<input type="radio" name="group3" value="2"> 2
<input type="radio" name="group3" value="1"> 1
<hr>
<label>Overall: </label>
<input type="radio" name="group4" value="5"> 5
<input type="radio" name="group4" value="4"> 4
<input type="radio" name="group4" value="3"> 3
<input type="radio" name="group4" value="2"> 2
<input type="radio" name="group4" value="1"> 1
<hr>
<input type="submit" id="submit" name="submit" value="Submit" class="button">
</form>
You need to change location after ajax success:
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
window.location = '/index1.php';
}
});

how to select specfic hidden element from a form in a loop in javascript?

So basically, my loop outputs four times the html below with different values in $show_id.
Everyform returns the $show_id value of the first form instead of the value of the form when the function is called. How do I fix that ?
my javascript
<script type="text/javascript" >
function addScore() {
var show_id = $('#show_id').val();
var user_id = $('#user_id').val();
var score = $('input[name=tvshowrating]:checked').val();
if(score=='')
{
alert('PleaseEnter A Score');
}
else
{
$("#flash").show();
$("#flash").html('<img src="ajax-loader.gif" />Loading Score...');
$.ajax({
type: "POST",
url: "showscoreajax.php",
data:{
"show_id" : show_id,
"user_id" : user_id,
"score" : score //we are passing the name value in URL
},
cache: false,
success: function(data){
$("#flash").html('Added');
}
});
}
return false;
};
</script>
myhtml
<div id="flash"></div>
<form id="form3B">
<div class="your-score">
<div class="">Your Score</div>
<input class="hover-star" type="radio" name="tvshowrating" value="1" title="1"/>
<input class="hover-star" type="radio" name="tvshowrating" value="2" title="2"/>
<input class="hover-star" type="radio" name="tvshowrating" value="3" title="3"/>
<input class="hover-star" type="radio" name="tvshowrating" value="4" title="4"/>
<input class="hover-star" type="radio" name="tvshowrating" value="5" title="5"/>
<input class="hover-star" type="radio" name="tvshowrating" value="6" title="6"/>
<input class="hover-star" type="radio" name="tvshowrating" value="7" title="7"/>
<input class="hover-star" type="radio" name="tvshowrating" value="8" title="8"/>
<input class="hover-star" type="radio" name="tvshowrating" value="9" title="9"/>
<input class="hover-star" type="radio" name="tvshowrating" value="10" title="10"/>
<input type="hidden" id="show_id" value="<?php echo $row[0]; ?>" />
<input type="hidden" id="user_id" value="<?php echo $user_id ?>" />
<span id="hover-test" style="margin:0 0 0 20px;"></span>
<input id="submitscore" type="submit" value="Submit scores!" onclick="addScore()" />
</div>
</form>
</div>
</div>
This is because you cannot have duplicate id's unlike duplicate class you need different ids for each form. something like this
<input type="hidden" id="show_id-form3B" value="<?php echo $row[0]; ?>" />
Then it will be possible for you to get unique value of each different forms. Some thing like this
function addScore(formId) {
var show_id = $('#show_id-'+formId).val();
var user_id = $('#user_id-'+formId).val();
//add your code
}

If a radio value is ==1 then request ajax

I have a registration form that i'm creating. In my html file, I have two radio boxes,
<input type="radio" name="whatever" value="0" id="whatever" checked//> No
<input type="radio" name="whatever" value="1" id="whatever"> Yes
If the value is 1 it should send an email using ajax, here is what I have,
if($("input[name='whatever']").val()=='2'){
$.ajax({
type: 'POST',
url: base + 'email',
data: vData,
success: function() {
// alert( "I'm the callback")
}
});
}
Thanks in advance for the help.
Start by fixing your markup so that you don't have duplicate ids:
<input type="radio" name="whatever" value="0" checked="checked" /> No
<input type="radio" name="whatever" value="1" /> Yes
and then you could use the :checked selector to detect the value of the currently selected radio button:
if($(':radio[name="whatever"]:checked').val() == '1') {
// the Yes radio button was checked =>
// you could do your AJAX request here ...
}
or give your radio buttons unique ids:
<input id="no" type="radio" name="whatever" value="0" checked="checked" /> No
<input id="yes" type="radio" name="whatever" value="1" /> Yes
and then:
if($('#yes').is(':checked')) {
// the Yes radio button was checked =>
// you could do your AJAX request here ...
}

How do I construct query strings for ajax url with jquery or javascript?

I'm currently using ajax and jquery to update search results with a form that has different categories for filtering.
My problem is that I want to be able to pass different variables to the url I'm using for my ajax from checkboxes and other form elements from the current page.
I have the url of "http://example.com/search/results/?cat=tech&min=5&max=30" in my ajax call below and I need that url to update to something like "http://example.com/search/results/?cat=service&min=10&max=30" or even remove a param if it's not even selected like "http://example.com/search/results/?min=5"
<form>
<input type="radio" name="cat" value="" /> None<br />
<input type="radio" name="cat" value="service" /> Service<br />
<input type="radio" name="cat" value="tech" /> Tech<br />
<input type="radio" name="cat" value="other" /> Other<br />
<input type="radio" name="min" value="5" /> 5<br />
<input type="radio" name="min" value="10" /> 10<br />
<input type="radio" name="min" value="15" /> 15<br />
<input type="radio" name="max" value="5" /> 5<br />
<input type="radio" name="max" value="10" /> 10<br />
<input type="radio" name="max" value="15" /> 15<br />
</form>
<div class="result"></div>
<script type="text/javascript">
$(document).ready(function(){
$('.filter').live('click focus', function(){
$.ajax({
url: http://example.com/search/results/?cat=tech&?min=5&max=30,
context: document.body,
success: function(data) {
$('.result').html(data);
}
});
});
});
</script>
Some how the url in the ajax call above needs to have the params update in the url. Also, not every param will always be included or needed.
It would be nice if I could use something like PHP's http_build_query function that would automatically know what values of the array to replace or something like that but I'm really stuck as to what to do. I'm not sure if I need to create an array and concatantate
Ideally I would be able to have something like "http://example.com/search/results/cat/tech/min/5/30" if that's at all doable.
url: 'http://example.com/search/results/?' + $('form').serialize(),
or
url: 'http://example.com/search/results/?' + $('form').serialize().replace(/&=/, '/'),
Use jQuery's serialize() method.
jsFiddle.
Well, what you're really trying to do is submit the form without refreshing the page. So here's how to do that:
Add a submit button to the form
Hook into the submit event of the form
Cancel the default action which is to refresh the page with the response
Submit via ajax and handle the response
Like this:
<form id="MyFilterForm" action="/myBaseUrl" method="POST">
...
<input type="submit" value="Filter"/>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('#MyFilterForm').submit(function(event){
// cancel the default action
event.preventDefault();
var theForm = $(this);
$.post(theForm.attr('action'), theForm.serialize(), function (data){
$('.result').html(data);
});
});
});
</script>
You can check which radio inputs have been selected and build the url accordingly e.g:
<input id="radCat" type="radio" name="cat" value="service" /> Service<br />
string url = "http://example.com/search/results/?";
if($("#radCat").attr("checked").val() == "checked") {
url = url + "cat=" + $("#radCat").attr("value").val();
}

Categories