Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php with javascrpt

Posted on 2009-07-03
9
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

636 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