Reputation: 43
I think this question has been asked a few times and I did find a few on google too.
However I am not trying to change the whole form action. I just need to change it partially.
Here is what I mean:
I have the following form:
<form action="order.php?add=<?php echo $product_name; ?>&price=<?php echo $price; ?>&qty=1" method="post">
<input type="text" name="qty" id="qty" class="qty" size="12" />
<input type="submit" class="button2" name="buttonadd" id="button" value="Add to cart" />
</form>
what I need to do is to change the qty=1
in the form action so it reflects the <input type="text" name="qty" id="qty" class="qty" size="12" />
value!
How can I achieve this?
Upvotes: 1
Views: 1764
Reputation: 4201
Here an alternate way that working perfectly DEMO
$("#qty").keyup(function (){
var first=$("form").attr("action").substring(($("form").attr("action").indexOf("qty")),0)
$("form").attr("action",first+"qty="+$(this).val());
console.log($("form").attr("action"))
});
Upvotes: 0
Reputation: 15213
In plain JS you could do :
window.onload = function() {
document.getElementById('qty').addEventListener('input', function(event) {
var action = this.form.getAttribute('action','');
action = action.replace(/qty=\d+/, 'qty=' + this.value);
this.form.setAttribute('action',action);
});
}
Upvotes: 1
Reputation: 11375
OP requested something specific.
$('#qty').change( function() {
var value = $(this).val();
$('#form').attr('action', 'order.php?qty=' + value);
alert($('#form').attr('action'));
});
<form id="form" action="order.php?qty=1" method="post">
<input type="text" name="qty" id="qty" class="qty" size="12" />
<input type="submit" class="button2" name="buttonadd" id="button" value="Add to cart" /></form>
I've added id="form"
.
I don't agree with this method, and you should use POST, but OP asked for it;
guys this is not going to be integrated into any payment gataway. everything is internal.. so please answer the actual question please.
I've edited the form action just so we can demo it on JsFiddle. By all means, add in the PHP :)
Upvotes: 0
Reputation: 6578
Set the Form method to 'get' (the thing that's now POST)
<form action="order.php" method="get">
<input type="hidden" name="add" value="<?php echo $product_name ?>" />
<input type="hidden" name="price" value="<?php echo $price ?>" />
<input type="text" name="qty" id="qty" class="qty" size="12" />
<input type="submit" class="button2" name="buttonadd" id="button" value="Add to cart" />
</form>
And there you have it! :)
(I also removed the other URL parameters and put them into hidden inputs. In my opinion, this is a much cleaner way to do this.)
Upvotes: 5