Jump to content

radio button validation with php


elmas156

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.