elmas156 Posted September 4, 2008 Share Posted September 4, 2008 Hey guys, I'm having a problem getting this to work and I need your help. What I have is a list of saved addresses displayed on a page using a while loop. Each address is actually part of a radio group with the variable $addressid as the value. Then under this I have a spot where a user can enter a new address if they don't want to use a saved address. I'm trying to make an alert (trying with javascript) to alert a user if they select an existing address AND enter a new address and tell them they can't do that. Here's what I have, maybe someone can add to it or better yet tell me if there is a way to do this using php. Just FYI, I've tried changing up the IF statement in the javascript function several different ways and I just can't seem to get it to work right. Also, it doesn't seem to recognize the radio group selection as a value at all. I'm totally lost. Any ideas? <head> <script language="JavaScript"> <!-- function validate(form) { if (form.selectaddress.value.length > 0 && form.address.value.length > 0) { alert("You have selected an address that is saved to your account, therefore you cannot enter a new service address at this time.") form.address.focus() return false } } </script> </head> <body> <?php $result3 = mysql_query("SELECT address,city,state,zip,addressid FROM addresses WHERE email = '$email'"); $addys = mysql_num_rows($result3); // Checks to see if anything is in the db. echo "<form name=\"form1\" action=\"schedule.php\" method=\"GET\">"; while( $row = mysql_fetch_array( $result3 ) ) { $address = $row[ "address" ]; $city = $row[ "city" ]; $state = $row[ "state" ]; $zip = $row[ "zip" ]; $addressid = $row[ "addressid" ]; echo "<input name=\"selectaddress\" id=\"selectaddress\" type=\"radio\" value=\"$addressid\"> "; echo "$address<br>"; echo " $city, $state $zip<br> <br>"; echo "Or enter a new address."; echo "<input name=\"address\" id=\"address\" type=\"text\" size=\"37\">"; echo "<input type=\"submit\" name=\"submit\" value=\"Schedule my service\" onClick=\"return validate(form)\"> ?> </body> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 4, 2008 Share Posted September 4, 2008 All your inputs have the same name and id? Quote Link to comment Share on other sites More sharing options...
elmas156 Posted September 4, 2008 Author Share Posted September 4, 2008 isn't that the way a radio group is supposed to be... all buttons have the same name because whichever one is clicked will have the value of whatever that button's value is assigned to? I'm not really sure because I've never used a radio group before. If you know something that I don't, please enlighten me. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 4, 2008 Share Posted September 4, 2008 Oops, sorry. No you're right. But shouldn't you make all the IDs unique? Quote Link to comment Share on other sites More sharing options...
elmas156 Posted September 4, 2008 Author Share Posted September 4, 2008 I don't know. Actually I started the form without ids on any of the radio buttons. I just added the ids in the trial and error part of the process. When I didn't get anywhere with trial and error I resorted to good ol' phpfreaks.com. It's very possible that I'm doing something wrong with my form... that's why I came here for help. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 4, 2008 Share Posted September 4, 2008 Okay well, for the form, you should have an onsubmit attribute that calls the validate JavaScript function, given the parameter be this. echo "<form name=\"form1\" action=\"schedule.php\" method=\"GET\" onsubmit=\"validate(this);\">"; In the validate function, you can simply check if a selectaddress checkbox is checked or not with the .checked method. And if it is checked, then check if the address input box is empty or now. And then return true or false. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.