php


javascript empty field validation


I have used the following code to display product details in my shopping cart website.
<div id="inner_right">
<form name="product_form" id="product_form" method="post" onsubmit="form_quantity(<?php echo $productid; ?>);">
<input type="hidden" name="hidden_<?php echo $productid; ?>" id="hidden_<?php echo $productid; ?>" />
<h1>Product Details of <?php echo $fetchproductname; ?></h1>
<div> </div>
<div id="product_left"><img src="<?php echo $path.$fetchimage; ?>" alt="" width="400" height="300" /></div>
<div id="product_right">
<div><strong>Category Name:</strong> <?php echo $categoryname; ?></div>
<p><strong>Product Number:</strong> <?php echo $fetchproductno; ?></p>
<p><strong>Price:</strong> <span class="price">$<?php echo $fetchproductprice; ?></span></p>
<p><strong>Stock:</strong> <?php echo $fetchproductstock; ?> nos</p>
<?php
$select_quantity = "SELECT * FROM `tbl_cart` WHERE `intProductid` = '".$productid."' AND `intSessionid` = '".$globalsessionid."'";
$select_quantity_res = mysql_query($select_quantity);
$sel_qty_num = mysql_num_rows($select_quantity_res);
$fetch_quantity = mysql_fetch_array($select_quantity_res);
$fetch_proid = $fetch_quantity['intProductid'];
$fetch_exqty = $fetch_quantity['intQuantity'];
?>
<p><strong>Quantity:</strong> <input name="quantity_<?php echo $productid; ?>" id="quantity_<?php echo $productid; ?>" value="<?php echo $fetch_exqty; ?>" class="quantity" type="text" /></p>
<div class="submit">
<button id="registerButton" type="submit">Add To Cart</button>
</div>
<input type="hidden" name="cart" id="cart" value="<?php echo $productid; ?>" />
</div>
<div class="clear"> </div>
</form>
</div>
There is an quantity field and add to cart button in my page. If the buyer click add to cart button without entered the quantity field an error should popup. For that i used the following javascript code.
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
if(enteredqty =='')
{
alert(Please enter quantity);
}
}
But it doesn't work. I couldn't trace the error. How can i correct my code?
You've got a string literal with no quotes:
alert(Please enter quantity);
You need to say:
alert("Please enter quantity");
// OR
alert('Please enter quantity');
(When you say you couldn't trace your error, what did you actually try? If using Chrome it has built-in debugging tools, or for Firefox you can download Firebug, and these tools can easily tell you about errors like this.)
And to preempt your next question, once you fix the above error you'll find that although the alert displays the form still submits. You need to update your onclick to return the result of your form_quantity() function, and return false when you don't want the submit to go ahead (i.e., when there's a validation error):
<form name="product_form" id="product_form" method="post"
onsubmit="return form_quantity(<?php echo $productid; ?>);"></form>
<script>
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
if(enteredqty === '') {
alert('Please enter quantity');
return false;
}
}
</script>
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
alert(enteredqty);// check the givel value is right.
if(enteredqty ==''){
alert("Please enter quantity");// double qute added.
}
}
Check this, you may find some path.

Related Links

echo issues with datetime-local format
php ldap search for loop query
Using consolibyte QuickBooks PHP framework - cannot import all employees 0x80040400 error
MySql is not Updating columns phpmyadmin
Ratchet is only processing one message at a time
Docker impossible to link nginx container with php-fpm container
PHP - Pulling array and splitting the strings
PHP Constant include
Bluemix- Connect dashDB to PHP application
Getting a PHP error updating PHP code for PHP 7.0 [duplicate]
PHP when sending no image to the form
PHP multiple file upload script storing file path in different rows
Create new array using items from current array [duplicate]
Exception: BlogBundle:Comment object not found
Search submit button not working in CodeIgniter
order post according to custom field position

Categories

HOME
php
xpath
apache-nifi
writefile
histogram
autocomplete
javafx-8
firebase-database
telegraf
driver
android-externalstorage
createjs
live-streaming
mpu6050
jodatime
supervisord
sonicwall
heap-dump
pinterest
asciimath
maven-plugin
significance
amazon-mws
bsd
altium-designer
network-analysis
oracleforms
ninject
nic
netbios
rust-cargo
markov-chains
db2-luw
xerces-c
jfxtras
wdk
numerics
assertions
bluemixtools
jade4j
elastica
sspi
linq.js
sbjson
icepdf
cloudera-sentry
code-push
webalizer
tinyioc
deployd
uiautomatorviewer
jgrapht
renderman
punctuation
android-webservice
line-intersection
pen
nivo-slider
kendo-editor
dex
http4s
fitbit
email-injection
petsc
ndk-build
mathjs
always-on-top
openseadragon
apple-configurator
xcopy
mnesia
persistent-object-store
largenumber
screen-orientation
hmisc
karma-coverage
mgwt
jcheckbox
operands
dnsjava
dc
image-rotation
inputbox
aho-corasick
full-text-indexing
otl
category-theory
pageviews
cidr
mod-perl
principalcontext
facebook-game-groups
lru
jbox2d
multiscaleimage
multiple-dispatch
jquery-dialog
system-information
taskkill
uitextfielddelegate
pcspim
firebird1.5
prototypal-inheritance
dropshadow
nsrangeexception
directshow.net
airprint
ning
index.dat

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App