Solved

php with javascrpt

Posted on 2009-07-03
9
189 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

778 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