|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Compare form field value to JS array from PHP/MySQL
I'm stuck on how to go about comparing a entered value in a form field to a js array that needs to be loaded from a MySQL database. The scenario is this:
A customer is ordering something and has a coupon code, they enter the code in the box, then with Javascript I do a full form validation on all the other fields in the form. I want to be able to in the same validation be able to print out an error if the code they entered is invalid or expired, though both will be the same error message. The SQL query would be this but how to load the results into a Javascript array, and then compare to the entered value has me totally lost. Any ideas? |
|
#2
|
|||
|
|||
|
RE: Compare form field value to JS array from PHP/MySQL
Yes, open a new window where you do the sql query check.
Put this javascript between the headtags. <script language="JavaScript"> function validate(couponcode) { window.open("checkcoupon.php?couponcode="+couponcode+"","check","toolbar=0,scrollbars=0,location=0,statusbar=0,menu bar=0,resizable=0,width=300,height=200"); } </script> Add this to your form <input name="couponcode" type="text" value="" onBlur = "validate(document.formname.couponcode.value);"> (formname = the name of your form, maybe you should change it) Then create a new page that is named checkcoupon.php and put this code in it. <? $couponcode = $_GET["couponcode"]; $numrows = mysql_num_rows(mysql_query("SELECT couponcode FROM coupons WHERE couponcode='$couponcode' and active=='1'")); if ($numrows == 0) { $error = "The couponcode does not exist!"; echo "<body>"; echo "$error"; } else { echo "<body>"; echo "<script language="JavaScript">"; echo "window.close()"; echo "</script>"; } ?> </body> </html> Of course you should add somewhere code to be able to connect to your own database. I haven't really tested it, but a think it should work. Hope this helps you. |
|
#3
|
|||
|
|||
|
RE: Compare form field value to JS array from PHP/MySQL
That way of doing it isnt quite what I origianlly had in mind, but I like your idea better it is more secure to do it that way. Thanks alot
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > Compare form field value to JS array from PHP/MySQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|