PDA

View Full Version : alert box script wanted



paldo
06-23-2011, 06:53 AM
I have a wine order form. I accept orders of 8 or 15 bottles ( because of the size of the package). So if the order is another value than 8 or 15 a pop-up alert box should open up with a warning after a value has been entered in the textbox. How is this kind of javascript called? Are there some examples on the internet?

Thank you

bluewalrus
06-23-2011, 07:06 AM
Can you provide us with the code you have so far? Coding from scratch is a bit more work and may not work...

paldo
06-23-2011, 07:39 AM
Dear Bluewalrus

as the coding is long I send you the link (http://www.cochard.ch/sessions/commande.php) , you can have a look to the source.

Sorry, the site is in french.

Thank you

traq
06-23-2011, 02:20 PM
instead of a text field, why don't you use a <select> list that has the appropriate quantities?

that way, your users would never make the mistake in the first place.

paldo
06-23-2011, 04:18 PM
I've thought about this option, but before changing my order form I would like to see if I can implement a javascript.

molendijk
06-24-2011, 10:45 AM
In the head, put this script:

<script type="text/javascript">
function controle(){
var avertissement='Le nombre des bouteilles que vous avez mis pour une ou plusieurs de nos vins ne correspond pas aux choix indiqués.'

if(document.getElementById('quantity1').value!='' && document.getElementById('quantity1').value!=6 && document.getElementById('quantity1').value!=12){alert(avertissement)}

else
if(document.getElementById('quantity1a').value!='' && document.getElementById('quantity1a').value!=8 && document.getElementById('quantity1a').value!=15){alert(avertissement)}

else
if(document.getElementById('quantity2').value!='' && document.getElementById('quantity2').value!=6 && document.getElementById('quantity2').value!=12){alert(avertissement)}

else
if(document.getElementById('quantity2a').value!='' && document.getElementById('quantity2a').value!=10 && document.getElementById('quantity2a').value!=21) {alert(avertissement)}

}
</script>
I only made it ready for 'quantity1', 'quantity1a', 'quantity2' and 'quantity2a'. So you should add the other else ... ifs yourself. You must also be sure that each wine has an ID (I noticed that 'quantity2' only had a name in your source, not an id)!

Then at the end of your page, replace <input type="submit" name="submit" value="commander"> with <input type="submit" name="submit" value="commander" onclick="controle()">
===
Arie Molendijk.

paldo
06-24-2011, 04:49 PM
Thanks Traq, you pushed me to have a try with <select>.
I have prepared a small example with two select drop down lists. But I run into a problem: I would like to have the two drop down lists in a table, one list in a cell, the other in another cell. But I don't find the correct way. Can someone help me?

You can test my example here (http://paolinet.info/sessions/sessions_select_page1.php).
Here what I did:
<?php
session_start();

echo "<form name=\"aaaa\" method=\"post\" action=\"sessions_select_page2.php\">";
?>

<?php



function select( $selection , $values )
{
echo "<select name=\"$selection\">\n";
foreach( $values AS $val ) {
if ( isset( $_SESSION["$selection"] ) AND $_SESSION["$selection"] == $val )
$selected = " selected=\"selected\"";
else
$selected = "";

echo "\t<option value=\"$val\"$selected>$val</option>\n";
}
print "</select>\n";
}


echo 'Please choose item 1:'; select( "selection1" , array( "0", "6", "12", "18" ) );



echo '<br/>Please choose item 2:'; select( "selection2" , array( "0", "6", "12", "18" ) );




?>
<br/><br/>



<input type="submit" name="submit" value="order now" />

traq
06-24-2011, 11:54 PM
When you start doing more work with php, it is very helpful to separated your processing (php functions, etc.) from your output (using echo). Mixing them makes your code messy, confusing, and hard to work with.

in your existing function, what values do $selection and $values hold? Post what you want your finished HTML to look like, and I can try to give you an example of how the function might be built.

paldo
06-25-2011, 07:03 AM
Dear Adrian, thanks for your replay. The issue is that I want to change in my order form (http://paolinet.info/sessions/sessions_order_page1.php) every input field where the visitor enter the quantity of bottles that he wishes to order with a select drop down menu with (0,6,12,18..). $selection would be the type of wine, $value the quantity of bottles. The existing form with input field works fine.
Thanks for your help