Cant update quantity using select option with the help of ajaxHow do you remove all the options of a select box and then add one option and select it with jQuery?What is the best way to add options to a select from as a JS object with jQuery?How do you select a particular option in a SELECT element in jQuery?Adding options to a <select> using jQuery?How to make the first option of <select> selected with jQueryjQuery Get Selected Option From DropdownAJAX json data does not print in PHPSet select option 'selected', by valuehow can i write an algorithm to multiply the total * quantity in a shopping cart using php $_SESSION?Android: HttpPost not work
What is this unknown executable on my boot volume? Is it Malicious?
Does a gnoll speak both Gnoll and Abyssal, or is Gnoll a dialect of Abyssal?
Are there any non-WEB re-implementation of TeX Core in recent years?
Is there a reliable way to hide/convey a message in vocal expressions (speech, song,...)
Double it your way
My research paper filed as a patent in China by my Chinese supervisor without me as inventor
Why do sellers care about down payments?
What officially disallows US presidents from driving?
Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?
Sample Inverse Color in Photoshop
Gravity on an Orbital Ring
Is English tonal for some words, like "permit"?
Why is the Digital 0 not 0V in computer systems?
Can the UK veto its own extension request?
Selecting 2 column in an Inner join
How do email clients "send later" without storing a password?
Glue or not to glue boots
How is Team Scooby Doo (Mystery Inc.) funded?
Were Roman public roads build by private companies?
Leaving out pronouns in informal conversation
When was the earliest opportunity the Voyager crew had to return to the Alpha quadrant?
Sol Ⅲ = Earth: What is the origin of this planetary naming scheme?
Why does Coq include let-expressions in its core language
Why isn't `typename` required for a base class that is a nested type?
Cant update quantity using select option with the help of ajax
How do you remove all the options of a select box and then add one option and select it with jQuery?What is the best way to add options to a select from as a JS object with jQuery?How do you select a particular option in a SELECT element in jQuery?Adding options to a <select> using jQuery?How to make the first option of <select> selected with jQueryjQuery Get Selected Option From DropdownAJAX json data does not print in PHPSet select option 'selected', by valuehow can i write an algorithm to multiply the total * quantity in a shopping cart using php $_SESSION?Android: HttpPost not work
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am creating a simple add to cart function where when the user has successfully added their product to cart they can view their cart and update the quantity using the select option in the cart page, but it seems that i can only update the first product that has been added to cart,if i add a second product i cant update that second product
cart.php
<?php
if(isset($_COOKIE["shopping_cart"]))
$total = 0;
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
?>
<?php
foreach($cart_data as $keys => $values)
?>
<form id="myForm">
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" id="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
$("#qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $( '#myForm' ).serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
);
</script>
I have define the URLROOT as define('URLROOT', 'http://localhost/vlake');
cookiesave function
public function cookiesave()
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
if($cart_data[$keys]["item_id"] == $_POST["hidden_id"])
$cart_data[$keys]["item_quantity"] = $_POST["qty"];
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30) ,'/');
javascript php jquery html ajax
add a comment
|
I am creating a simple add to cart function where when the user has successfully added their product to cart they can view their cart and update the quantity using the select option in the cart page, but it seems that i can only update the first product that has been added to cart,if i add a second product i cant update that second product
cart.php
<?php
if(isset($_COOKIE["shopping_cart"]))
$total = 0;
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
?>
<?php
foreach($cart_data as $keys => $values)
?>
<form id="myForm">
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" id="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
$("#qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $( '#myForm' ).serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
);
</script>
I have define the URLROOT as define('URLROOT', 'http://localhost/vlake');
cookiesave function
public function cookiesave()
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
if($cart_data[$keys]["item_id"] == $_POST["hidden_id"])
$cart_data[$keys]["item_quantity"] = $_POST["qty"];
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30) ,'/');
javascript php jquery html ajax
add a comment
|
I am creating a simple add to cart function where when the user has successfully added their product to cart they can view their cart and update the quantity using the select option in the cart page, but it seems that i can only update the first product that has been added to cart,if i add a second product i cant update that second product
cart.php
<?php
if(isset($_COOKIE["shopping_cart"]))
$total = 0;
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
?>
<?php
foreach($cart_data as $keys => $values)
?>
<form id="myForm">
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" id="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
$("#qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $( '#myForm' ).serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
);
</script>
I have define the URLROOT as define('URLROOT', 'http://localhost/vlake');
cookiesave function
public function cookiesave()
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
if($cart_data[$keys]["item_id"] == $_POST["hidden_id"])
$cart_data[$keys]["item_quantity"] = $_POST["qty"];
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30) ,'/');
javascript php jquery html ajax
I am creating a simple add to cart function where when the user has successfully added their product to cart they can view their cart and update the quantity using the select option in the cart page, but it seems that i can only update the first product that has been added to cart,if i add a second product i cant update that second product
cart.php
<?php
if(isset($_COOKIE["shopping_cart"]))
$total = 0;
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
?>
<?php
foreach($cart_data as $keys => $values)
?>
<form id="myForm">
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" id="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
$("#qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $( '#myForm' ).serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
);
</script>
I have define the URLROOT as define('URLROOT', 'http://localhost/vlake');
cookiesave function
public function cookiesave()
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
if($cart_data[$keys]["item_id"] == $_POST["hidden_id"])
$cart_data[$keys]["item_quantity"] = $_POST["qty"];
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30) ,'/');
javascript php jquery html ajax
javascript php jquery html ajax
edited Mar 28 at 9:51
ADyson
29.7k12 gold badges28 silver badges46 bronze badges
29.7k12 gold badges28 silver badges46 bronze badges
asked Mar 28 at 8:50
J. DoeJ. Doe
421 silver badge6 bronze badges
421 silver badge6 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
$("#qty")
will only ever identify the first element with that ID. So it just doesn't handle events on any of the others. Having multiple elements with the same ID is invalid in HTML - after all, if an ID does not uniquely identify something, then by definition it's not an ID! So JavaScript / jQuery will simply ignore any duplicates after the first one. You'll have the same problem with $( '#myForm' )
as well.
You need to use a class to identify the <select>
, and then traverse the DOM to find the parent form:
<form>
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" class="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
... and ...
$(".qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $(this).closest("form").serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
N.B. Just as a design point...I note that you reload the page as soon as AJAX has completed. The whole point of AJAX is to allow you to stay on the same page without re-loading. To avoid this unnecessary duplication of HTTP requests, you could either
a) forget about using AJAX for this, and just do a normal postback to update the quantity, or
b) when the AJAX completes, use a little bit of JavaScript just to update the cookie client-side instead.
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
1
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393407%2fcant-update-quantity-using-select-option-with-the-help-of-ajax%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$("#qty")
will only ever identify the first element with that ID. So it just doesn't handle events on any of the others. Having multiple elements with the same ID is invalid in HTML - after all, if an ID does not uniquely identify something, then by definition it's not an ID! So JavaScript / jQuery will simply ignore any duplicates after the first one. You'll have the same problem with $( '#myForm' )
as well.
You need to use a class to identify the <select>
, and then traverse the DOM to find the parent form:
<form>
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" class="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
... and ...
$(".qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $(this).closest("form").serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
N.B. Just as a design point...I note that you reload the page as soon as AJAX has completed. The whole point of AJAX is to allow you to stay on the same page without re-loading. To avoid this unnecessary duplication of HTTP requests, you could either
a) forget about using AJAX for this, and just do a normal postback to update the quantity, or
b) when the AJAX completes, use a little bit of JavaScript just to update the cookie client-side instead.
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
1
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
add a comment
|
$("#qty")
will only ever identify the first element with that ID. So it just doesn't handle events on any of the others. Having multiple elements with the same ID is invalid in HTML - after all, if an ID does not uniquely identify something, then by definition it's not an ID! So JavaScript / jQuery will simply ignore any duplicates after the first one. You'll have the same problem with $( '#myForm' )
as well.
You need to use a class to identify the <select>
, and then traverse the DOM to find the parent form:
<form>
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" class="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
... and ...
$(".qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $(this).closest("form").serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
N.B. Just as a design point...I note that you reload the page as soon as AJAX has completed. The whole point of AJAX is to allow you to stay on the same page without re-loading. To avoid this unnecessary duplication of HTTP requests, you could either
a) forget about using AJAX for this, and just do a normal postback to update the quantity, or
b) when the AJAX completes, use a little bit of JavaScript just to update the cookie client-side instead.
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
1
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
add a comment
|
$("#qty")
will only ever identify the first element with that ID. So it just doesn't handle events on any of the others. Having multiple elements with the same ID is invalid in HTML - after all, if an ID does not uniquely identify something, then by definition it's not an ID! So JavaScript / jQuery will simply ignore any duplicates after the first one. You'll have the same problem with $( '#myForm' )
as well.
You need to use a class to identify the <select>
, and then traverse the DOM to find the parent form:
<form>
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" class="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
... and ...
$(".qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $(this).closest("form").serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
N.B. Just as a design point...I note that you reload the page as soon as AJAX has completed. The whole point of AJAX is to allow you to stay on the same page without re-loading. To avoid this unnecessary duplication of HTTP requests, you could either
a) forget about using AJAX for this, and just do a normal postback to update the quantity, or
b) when the AJAX completes, use a little bit of JavaScript just to update the cookie client-side instead.
$("#qty")
will only ever identify the first element with that ID. So it just doesn't handle events on any of the others. Having multiple elements with the same ID is invalid in HTML - after all, if an ID does not uniquely identify something, then by definition it's not an ID! So JavaScript / jQuery will simply ignore any duplicates after the first one. You'll have the same problem with $( '#myForm' )
as well.
You need to use a class to identify the <select>
, and then traverse the DOM to find the parent form:
<form>
<input type="hidden" name="hidden_id" value="<?php echo $values["item_id"];?>">
<select name="qty" class="qty" class="form-control">
<option style="display:none;" selected><?php echo $values["item_quantity"];?></option>
<?php
for($i=1; $i<=$values["item_qty"]; $i++)
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
?>
</select>
</form>
... and ...
$(".qty").change(function()
var url = "<?php echo URLROOT; ?>"
var form = $(this).closest("form").serialize();
$.ajax(
type: "POST",
url: url + '/shops/cookiesave',
data: form,
beforeSend: function()
//do something here like load a loading spinner etc.
,
)
.done(function()
window.location.reload(true);
)
);
N.B. Just as a design point...I note that you reload the page as soon as AJAX has completed. The whole point of AJAX is to allow you to stay on the same page without re-loading. To avoid this unnecessary duplication of HTTP requests, you could either
a) forget about using AJAX for this, and just do a normal postback to update the quantity, or
b) when the AJAX completes, use a little bit of JavaScript just to update the cookie client-side instead.
answered Mar 28 at 9:57
ADysonADyson
29.7k12 gold badges28 silver badges46 bronze badges
29.7k12 gold badges28 silver badges46 bronze badges
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
1
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
add a comment
|
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
1
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
Is it possible that you could show me how to do option b
– J. Doe
Mar 28 at 12:02
1
1
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
developer.mozilla.org/en-US/docs/Web/API/Document/cookie . But you know, you might just be better to use the PHP $_SESSION to store the cart instead of explicitly setting cookies yourself.
– ADyson
Mar 28 at 13:16
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393407%2fcant-update-quantity-using-select-option-with-the-help-of-ajax%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown