Solved

php with javascrpt

Posted on 2009-07-03
9
192 Views
Last Modified: 2013-12-13
HI
i am having some probme to solved an issue

there would be select with some values,
when user will select a value, it will check against a database to see if the selected value is greated then the filed value.
if yes, then it will say, out of stock

now, i know you can do this via pure php, but i want a short cut way without modify too much my existing code.( due to time issue)

i am tinking is there any way, i can do this via java script ??
so javascript will get the value from select , then it will compare with the database ??
if its see the value is bigger then database filed, it will say , outof stock , thats all

here is the link if you want to see what i meant

http://www.websolution4u.co.uk/sharp-user/addtobasket.php?id=1

<script type="text/javascript">
 
function output(selectobj)
{
alert(selectobj.selectedIndex);
 
}
              
              </script>
			
			<?php
				echo "<td><select name='amountBox' select id='sample' onChange='output(this)' >";
			
				for($i=0;$i<=10;$i++)
				{
					echo "<option>" . $i . "</option>";
				}
			
				echo "</select></td>";
			
				
				echo "<td><INPUT TYPE='image' name='submit' src='images.jpg' onclick='this.form.submit()'  border='0' ><input type='hidden' name='submit' value='Add to basket'> </td>";
				echo "</tr>";
							
			echo "</table>";
			echo "</form>";
		}

Open in new window

0
Comment
Question by:fosiul01
  • 5
  • 4
9 Comments
 
LVL 2

Expert Comment

by:dinu_sorin
ID: 24771892
There's the first way to do that:
<script type="text/javascript">
 
function output(selectobj,max)
{
if(selectobj.selectedIndex.value>max)
{
alert('not available!');
return false;
}
return true; 
}
              
              </script>
                        
                        <?php
//$max_value must be the same variable you print in qty available
                                echo "<td><select name='amountBox' select id='sample' onChange='return output(this,$max_value)' >";
                        
                                for($i=0;$i<=10;$i++)
                                {
                                        echo "<option>" . $i . "</option>";
                                }
                        
                                echo "</select></td>";
                        
                                
                                echo "<td><INPUT TYPE='image' name='submit' src='images.jpg' onclick='this.form.submit()'  border='0' ><input type='hidden' name='submit' value='Add to basket'> </td>";
                                echo "</tr>";
                                                        
                        echo "</table>";
                        echo "</form>";
                }

Open in new window

0
 
LVL 2

Accepted Solution

by:
dinu_sorin earned 500 total points
ID: 24771903
the second way to do that (i think this is better)

<script type="text/javascript">
 
function output(selectobj)
{
alert(selectobj.selectedIndex);
 
}
              
              </script>
                        
                        <?php
                                echo "<td><select name='amountBox' select id='sample' onChange='output(this)' >";
                        //$max is the value printed in qty available td
                                for($i=0;$i<=$max;$i++)
                                {
                                        echo "<option>" . $i . "</option>";
                                }
                        
                                echo "</select></td>";
                        
                                
                                echo "<td><INPUT TYPE='image' name='submit' src='images.jpg' onclick='this.form.submit()'  border='0' ><input type='hidden' name='submit' value='Add to basket'> </td>";
                                echo "</tr>";
                                                        
                        echo "</table>";
                        echo "</form>";
                }

Open in new window

0
 
LVL 2

Expert Comment

by:dinu_sorin
ID: 24771912
And the 3rd way, but i dont recomand (why to compare with the database when you already know the values?): Using ajax.

http://www.w3schools.com/Ajax/Default.Asp - here is all you need to know about ajax.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 29

Author Comment

by:fosiul01
ID: 24771920
Hi
thanks, just little bit of problem

how will i define this $max_value value ??

because according to your idea, $max_value would be $prodrow[qty]

so how i will  pass this $prodrow[qty]  to $max_value

0
 
LVL 29

Author Comment

by:fosiul01
ID: 24771933
hahahaha
thanks i know , it would be this


                        for($i=0;$i<=$prodrow[qty];$i++)
                        {
                              echo "<option>" . $i . "</option>";
                        }

so user would not be able to order any thing more then
$prodrow[qty]

0
 
LVL 29

Author Comment

by:fosiul01
ID: 24771960
thanks for your advise, you gave me the idea!!!

what i just did its to change the for loop

so it will show only list from stock, which i guess the easiest way!!

here what i have done ..
http://www.websolution4u.co.uk/sharp-user/addtobasket.php?id=1

thanks
i will award you full point to give me nice advise
0
 
LVL 29

Author Closing Comment

by:fosiul01
ID: 31599538
thanks for the idea
0
 
LVL 2

Expert Comment

by:dinu_sorin
ID: 24771984
Thanks. Happy coding at your Web Site.
0
 
LVL 29

Author Comment

by:fosiul01
ID: 24771994
once upon a time, i used to work with php, but since did not touch it almost a year, totaly forgot coding and idea about putting things together!!!

anyway thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question