php - Using ajax with mysql to validate before rendering -
i pretty new ajax might ask might simple of you.
i looking best way check user posting checkdata.php
verily price
in database.
i have long database works find need validation.
product
pid name size price 1 chocolate 12 30.00
i have form , in form posting pid
name
, size
in pdo statement doing this
select pid, price, name, size product pid=:pid , size=:size , name=:name
this works fine.
but want use ajax check user same whats in database , equal same price in database.
if not same should have alert message saying "don't mess code."
i have long table. sorry cant show code long
in table have name echo database
size
dropdown list
and pid
hidden input.
i have jquery
$(document).ready(function(){ $('#selected').hide(); $('#button').click(function(){ var pid = $('#pid').val(); var size = $('#size').val(); var qty = $('#qty').val(); var price = '\u00a3' + parseint($('#pricetag').text().replace(/^\d/, ''), 10) * qty; var size = $('#size').val(); if (!/^[1-9]\d?$/.test(qty)){ alert('quantity should not below 1 or null'); return false; // don't continue } else { $('#sprice').text(price); $('#ssize').text(size); $('#selected').slidedown(); } $.ajax({ url: 'checkdata.php', type: 'post', data: { pid:pid, size:size, qty:qty}, success: function(data) { } }); }); }); <?php session_start() if(isset($_post['pid']) && isset($_post['size']) && isset($_post['qty']) && isset($_post['name'])){ $pid = $_post['pid']; $size = $_post["size"]; $qty = $_post['qty']; $name= $_post['name']; . . . somewhere in php tag have statement right price based on data getting posted select pid, price, name, size product pid=:pid , size=:size , name=:name ?>
summary how can use ajax check getting posted same on database before render page.
you shouldn't round-tripping prices through client anyways. it's 1 thing pull price , display user, should never accept price value user. fetch fresh copy database when comes time calculate costs.
if want "fix" price if item in shopping cart prolonged duration, , you're changing prices in database, keep price in user's session. that's purely server-side data structure, , can't meddled directly.
that'd let things "item price has changed since added cart. accept new updated price or remove cart?".
Comments
Post a Comment