Jump to content

Problem with Sticky Radio Button


doubledee

Recommended Posts

NetBeans is saying I am missing a tag, and I think this code is causing my Form to error out as well.

 

What is wrong with it?

 

<label for="gender">Gender:</label>
<input type="radio" name="gender" value="F" <?php echo (isset($gender) && $gender == "F") ? 'checked="checked"' : ''; ?>/>Female
<input type="radio" name="gender" value="M" <?php echo (isset($gender) && $gender == "M") ? 'checked="checked"' : ''; ?>/> Male

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

only thing i think is technically wrong is I think in your label tag, I think the for attribute is supposed to point to an element id not, name.

 

I messed that up.

 

Does this look better...

<input id="female" type="radio" name="gender" value="F" <?php echo ((isset($gender) && $gender == "F") ? 'checked="checked"' : ''); ?> />
<label for="female">Female</label>
<input id="male" type="radio" name="gender" value="M" <?php echo ((isset($gender) && $gender == "M") ? 'checked="checked"' : ''); ?> />
<label for="male">Male</label>

 

 

Debbie

 

Link to comment
Share on other sites

looks okay to me.. I guess netbeans is buggin'

 

Really?

 

That simple?

 

 

Debbie

 

 

I've noticed that things tend to get "buggy" sometimes when php tags are embedded inside html tags like that; it's pretty hard to accurately parse (I've seen this in other IDEs too, and if you look at your posted code, you can see even smf's syntax highlighter is buggin')

 

only thing i think is technically wrong is I think in your label tag, I think the for attribute is supposed to point to an element id not, name.

 

I messed that up.

 

Does this look better...

<input id="female" type="radio" name="gender" value="F" <?php echo ((isset($gender) && $gender == "F") ? 'checked="checked"' : ''); ?> />
<label for="female">Female</label>
<input id="male" type="radio" name="gender" value="M" <?php echo ((isset($gender) && $gender == "M") ? 'checked="checked"' : ''); ?> />
<label for="male">Male</label>

 

 

Debbie

 

 

looks okay to me

Link to comment
Share on other sites

looks okay to me.. I guess netbeans is buggin'

 

Really?

 

That simple?

 

 

Debbie

 

 

I've noticed that things tend to get "buggy" sometimes when php tags are embedded inside html tags like that; it's pretty hard to accurately parse (I've seen this in other IDEs too, and if you look at your posted code, you can see even smf's syntax highlighter is buggin')

 

only thing i think is technically wrong is I think in your label tag, I think the for attribute is supposed to point to an element id not, name.

 

I messed that up.

 

Does this look better...

<input id="female" type="radio" name="gender" value="F" <?php echo ((isset($gender) && $gender == "F") ? 'checked="checked"' : ''); ?> />
<label for="female">Female</label>
<input id="male" type="radio" name="gender" value="M" <?php echo ((isset($gender) && $gender == "M") ? 'checked="checked"' : ''); ?> />
<label for="male">Male</label>

 

 

Debbie

 

 

looks okay to me

 

Okay, thanks.

 

 

So, is there any way to write the same block inside a PHP echo?  (One that is legible if that is possible?!)

 

 

Debbie

 

Link to comment
Share on other sites

Sorry, but my code is NOT working...

 

(This is another case where I built this script weeks ago, and was certain it was working just fine, and now it is broken when I haven't touched it?!)

 

 

At the top of my script I have...

// *************************************************************
// HANDLE FORM.											 *
// *************************************************************
if ($_SERVER['REQUEST_METHOD']=='POST'){
	// Form was Submitted (Post).

	// Initialize Errors Array.
	$errors = array();

	// Trim all form data.
	$trimmed = array_map('trim', $_POST);


	// ************************
	// Validate Form Data.	*
	// ************************
	// Validate Gender.
	if (empty($trimmed['gender'])){
		// No Gender.
		$errors['gender'] = 'Choose a Gender.';
	}else{
		// Gender Exists.
		if (($trimmed['gender'] == "F") || ($trimmed['gender'] == "M")){
			// Valid Gender.
			$gender = $trimmed['gender'];
		}else{
			// Invalid Gender.
			$errors['gender'] = 'Gender must be "Female" or "Male".';
		}
	}//End of VALIDATE GENDER

 

 

And then at the bottom on my script in the HTML section I have...

<!-- CHANGE DETAILS FORM -->
<form id="changeDetails" action="" method="post">
	<fieldset>
		<legend>Change My Details</legend>

		<!-- Gender -->
		<label for="gender">Gender:</label>
		<input type="radio" name="gender" value="F" <?php echo (isset($gender) && $gender == "F") ? 'checked="checked"' : ''; ?>/> Female
		<input type="radio" name="gender" value="M" <?php echo (isset($gender) && $gender == "M") ? 'checked="checked"' : ''; ?>/> Male

 

When I change the Gender in my Form, the script seems to run and displays a "Details Updated" message, but...

 

1.) The radio buttons - in the reloaded Form - keep defaulting to "Male"

 

2.) My "View Member Details" script/page isn't showing any Gender.

 

3.) In the database, I see a "0" for Gender which means "unknown".  (That is the default, which mean the database is doing its job, but my script is failing miserably!!)

 

What am I doing wrong here??

 

 

Debbie

 

 

Link to comment
Share on other sites

All by itself, I'm not seeing anything wrong with the code you've posted, you'll have to post more.

 

1) I'm not 100% but I don't think browsers default to a radio button being selected, so if it is "defaulting" to "Male" then I'm pretty sure either that's what you selected or you have other code (not posted) that's setting $gender. I don't see it defaulting to "Male" in the latest FF, Chrome and IE anyways...

 

2) Dunno what to tell you on this count since near as I can tell, you didn't post relevant code for that

 

3) Again, you didn't post relevant code for that (the query string and query insert code)

 

Link to comment
Share on other sites

All by itself, I'm not seeing anything wrong with the code you've posted, you'll have to post more.

 

1) I'm not 100% but I don't think browsers default to a radio button being selected, so if it is "defaulting" to "Male" then I'm pretty sure either that's what you selected or you have other code (not posted) that's setting $gender. I don't see it defaulting to "Male" in the latest FF, Chrome and IE anyways...

 

The purpose of my code, is the radio buttons are supposed to be both 'sticky" (i.e. retain last selection) AND default to one choice.

 

I thought my code would do that.

 

(Sorry, I'm having a really rotten day, and this bug is annoying me to know end, and the answer s probably right in front of me.)

 

 

2) Dunno what to tell you on this count since near as I can tell, you didn't post relevant code for that

 

3) Again, you didn't post relevant code for that (the query string and query insert code)

 

But I step through the code in NetBeans, and my query is getting here...

 

	// ******************************
	// Attempt to Change Details.		*
	// ******************************
	if (empty($errors)){
		// Valid form data.


		// ************************
		// Update Member Record.	*
		// ************************

		// Build query.
		$q1 = "UPDATE member
						SET first_name=?,
								gender=?,
								birth_year=?,
								location=?,
								occupation=?,
								interests=?,
								about_me=?,
								updated_on=NOW()
						WHERE id=?
						LIMIT 1";

		// Prepare statement.
		$stmt1 = mysqli_prepare($dbc, $q1);

		// Bind variables to query.
		mysqli_stmt_bind_param($stmt1, 'ssissssi', $firstName, $gender, $birthYear, $location,
										$occupation, $interests, $aboutMe, $sessMemberID);

		// Execute query.
		mysqli_stmt_execute($stmt1);

		// Verify Update.
		if (mysqli_stmt_affected_rows($stmt1)==1){
			// Update Succeeded.
			$_SESSION['resultsCode'] = 'DETAILS_NEW_DETAILS_SET_2131';

 

 

So there are two issues I can see...

 

a.) My query runs successfully, yet my database apparently isn't getting updated.

 

b.) My Form radio buttons are not defaulting to one Gender, and are not getting populated when the Form loads.

 

I think this is because of the code I posted.

 

 

Debbie

 

Link to comment
Share on other sites

I figured it out.

 

I was working with "M" and "F" in my code when my database was using "1" and "2" for gender.

 

That, and I accidentally left out my error handling code to flash a message that the Gender field was wrong.

 

I know it was something stupid...

 

Now I just wish I could get rid of those damn annoying "Missing required end tag" error icons in NetBeans?!

 

 

Debbie

 

 

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.