Solved

php with javascrpt

Posted on 2009-07-03
9
188 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon Redshift 2 28
MySQL Grouping 2 25
WP_Query multiple custom taxonomies 5 29
configure dependency in POM for new database 3 18
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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