Joe
Joe

Reputation: 45

A Href onclick update value

How to update the value in shopping cart when someone input the promocode and click apply (using href) without redirect to another page using php and javascript? pls help. Thank you

My code :

<td colspan="2" align="left"><input style="width:200px;" type="text" name="_PromoCode" placeholder="Insert Promo Code" maxlength="20" /></td>
<td align="left"><a href=# onclick="return get_promo();">apply</a></td>
<td>&nbsp;</td>
<td><font color="#FF0000"><?php echo $hasil['Currency_ID'];?></font></td>
<td><font color="#FF0000"><?php echo "<span id='promocode' style='float:right'>". number_format($total_promo)."</span>";?></font></td>

script :

<script>

function get_promo()
{
    var x = <?php echo $total_promo ?> ;

    document.getElementById("promocode").innerHTML = formatNumber(x);

}

</script>

screenshot : enter image description here

Upvotes: 0

Views: 109

Answers (2)

Vishnu Prasad
Vishnu Prasad

Reputation: 727

Use Ajax for this kind of operation. You can send and receive the processed data to the page without refreshing it.

You can find more help for beginners using AJAX can see here

Ajax using jquery and php beginners help tutorial Link

Upvotes: 0

odannyc
odannyc

Reputation: 718

If youre getting the promo code from a database such as mysql. I suggest you do an AJAX call within your javascript code to a PHP script that will retreive the promo code information and update the total and such. With ajax you have the ability to not redirect to another page or refresh the current page.

Upvotes: 1

Related Questions