Solved

php with javascrpt

Posted on 2009-07-03
9
187 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now