Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts

    If client submits value higher than X, then return false

    Lets say I have a MySQL value of 4... and I have a HTML INPUT field.. Is there a way to make it so that if a client tries to submit a value higher than 4, then they will be returned a message? Something like:

    Code:
    <SCRIPT type="text/javascript">
    	function validateForm() {
    		if (document.forms["form"]["quantity"].value== (+$row['quantity']) {
    			alert ("Cannot submit because the quantity specified is not available.");
    			return false;
    		}
    	}
    </SCRIPT>
    <INPUT name="quantity" onsubmit="return validateForm()>
    Last edited by FlashDance; 10-10-2011 at 08:10 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Code:
    var maxqty = <?php echo $row["quantity"]; ?>;
    
    function validateForm() 
    {
         if (document.forms["form"].quantity.value > maxqty ) 
         {
            alert ("Cannot submit because the quantity specified is not available.");
            return false;
        }
    }
    You can drop the PHP value right into the middle of the "if":
    Code:
         if (document.forms["form"].quantity.value > <?php echo $row["quantity"] ?> )
    but don't you agree that is hard to read and will be hard to debug later?

    Keep the coding neat and simple.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Hi Old Pedant,


    I applied the code logic you suggested above into the code I wrote, but it didn't work. When I enter a value that exceeds the value in the MySQL table, the page just goes ahead and submits.
    Perhaps I have misinterpreted what you have said?

    Javascript:
    Code:
    if (document.forms["form"].quantity.value > $row[sizes]) {
    	alert ("Cannot submit because the quantity exceeds availability.");
    	return false;
    }
    HTML:
    Code:
    <INPUT name="quantity">

    Quote Originally Posted by Old Pedant View Post
    but don't you agree that is hard to read and will be hard to debug later?
    Personally, I do not. The code is not very long so if one understands how the code is suppose to work then they would not have much code to go over and debug.
    Last edited by FlashDance; 10-11-2011 at 12:31 PM.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    if (document.forms["form"].quantity.value > $row[sizes]) {
    The PHP value is not echoed using PHP syntax <?php echo ?>. On the other hand, I doubt that the key [sizes] is PHP defined. Could be
    Code:
    <?php echo $row[$sizes]; ?>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    The PHP value is not echoed using PHP syntax <?php echo ?>. On the other hand, I doubt that the key [sizes] is PHP defined. Could be
    Code:
    <?php echo $row[$sizes]; ?>
    $row[sizes] is from MySQL

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by FlashDance View Post
    $row[sizes] is from MySQL
    Then make it to be a PHP variable and echo it.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I tried this, but it didn't work.

    I tried this, but the page just went ahead and submitted:


    Javascript:
    Code:
    function validateForm() {
    	var integer = parseInt (document.forms["form"]["quantity"].value);
    	$noavailability = $row['sizes'] + 1;
    	if (integer > $noavailability) {
    		alert ("Cannot submit because quantity exceeds availability.");
    		return false;
    	}
    }
    HTML
    Code:
    <INPUT class="quantity" name="quantity" value="1">

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    This is everything related to the script in regards to the quantity validation.

    Code:
    <?php
    	$link = mysql_connect('localhost', 'testusr', 'testpw');
    	mysql_select_db('testdb', $link);
    	$query = "select * from products where id='$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result);
    	mysql_query ($query);
    	mysql_close($link);
    ?>
    
    <SCRIPT type="text/javascript">
    	function validateForm() {
    		if (document.forms["form"]["quantity"].value=="") {
    			alert ("Cannot submit because a quantity has not been specified.");
    			return false;
    		}
    		var integer = parseInt (document.forms["form"]["quantity"].value);
    		if (integer > <?php echo $row['sizes']; ?>) {
    			alert ("Cannot submit because quantity exceeds availability.");
    			return false;
    		}
    		if (document.forms["form"]["size"].value=="") {
    			alert ("Cannot submit because a size has not been selected.");
    			return false;
    		}
    	}
    </SCRIPT>
    
    <FORM action="../../../../cart/addtocart.html" method="post" name="form" onsubmit="return validateForm()">
    	<DIV>
    		Add <INPUT class="quantity" name="quantity" value="1"> <SELECT class="size" name="size"><OPTION>Small</OPTION><OPTION>Large</OPTION></SELECT> to <INPUT class="cart" type="submit" value="CART"><INPUT name="id" type="hidden" value="<?php echo $id; ?>">
    	</DIV>
    </FORM>

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Give it a simple echo on the document. See if the variable $row['sizes'] is really defined and it has a value. Or better check with a var_dump() what kind of variable is and which is its structure
    Code:
    <body>
    <?php echo $row['sizes'];
    var_dump($row['sizes']);
    ?>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I've decided to start fresh as I have had my code trashed by asking for help on the wrong forum, so I'll explain again from the start:


    I am wanting to make it so if a client tries to submit a value in a HTML INPUT form that is higher than a specific value, then JavaScript will deny the form being submitted and return a message to the client.
    The value of which the number cannot go over will be variable and be pulled from a MySQL table.

    I am thinking the JavaScript and HTML portion of the code being something along the lines of this:

    JavaScript:
    function validateForm() {
    if (document.forms["form"]["quantity"].value > <?php $row['quantity'] ?>) {
    alert ("Cannot submit because quantity exceeds availability.");
    return false;
    }
    }

    HTML:
    <INPUT name="quantity" onsubmit="return validateForm()">
    Anyone got any suggestions?

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Bring the page up in your browser.

    Click on the VIEW menu of the browser.

    Click on the SOURCE or PAGE SOURCE menu item.

    You will now be looking at the HTML *as seen by the browser*.

    Locate your validateForm function in that HTML.

    Copy/Paste what is there in the HTML to here.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    It just occurred to me: Is your JS code in a *separate" xxx.js file??? If so, then of course this won't work. You would have to convert that xxx.js file to xxx.php, as well.

    But there's an easier way, if that's the problem.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Hello Old Pedant,


    This is the JavaScript source for function validateForm():

    Code:
    function validateForm() {
    	if (document.forms["form"]["quantity"].value=="") {
    		alert ("Cannot submit because a quantity has not been specified.");
    		return false;
    	}
    	if (document.forms["form"]["size"].value=="") {
    		alert ("Cannot submit because a size has not been selected.");
    		return false;
    	}
    	if (document.forms["form"]["size"].value=="Small") {
    		if (document.forms["form"]["quantity"].value > ) {
    			alert ("Cannot submit because the quantity exceeds availability.");
    			return false;
    		}
    	}
    }
    One problem I can see is that <?php $row['sizes'] ?> did not output. When I placed the PHP code inside the HTML it worked, so perhaps I have to brake out of JavaScript or something?


    The Javascript (and all the code) is in a single file.

  • #14
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I have found one problem - the function validateForm() isn't even working. I'll look into this now and report back my finding.

    EDIT: The form is only not working because <?php $row['sizes'] ?> is producing nothing. When I replace <?php $row['sizes'] ?> with "3", the whole page works!
    So, the error lays in getting <?php $row['sizes'] ?> outputting.
    Last edited by FlashDance; 10-12-2011 at 03:51 AM.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Think you will now have to show the actual PHP code. And now this is purely a PHP problem, nothing directly to do with JS.

    Are you possibly ignoring the error(s) you are getting from PHP? Seems to me that's likely the case.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •