Jump to content

[SOLVED] Form Help: Mapping Radio Button Set to Multiple Fields


Recommended Posts

Hi, all —

 

I've been staring at a form for about a day now and hope a fresh set of eyes can shed some light on it.  It has a couple of issues, but here's the first:

 

I'm trying to map a set of four radio buttons to eight fields in a MySQL database.  If you select Radio Option 1, for example, that value should be passed into SQLField1, the now() value passed into SQLField2, and SQLField3 through SQLField8 would be passed no value.

 

The way I've gone about this is to set the radio buttons up like so:

 

<?php
if (isset($_POST['submit']) && $jobType == 'postJob') {
echo "<input type=\"radio\" name=\"jobType\" value=\"postJob\" checked>";
}
else {
echo "<input type=\"radio\" name=\"jobType\" value=\"postJob\">";
}
?>
Install a post<br/>

 

(The "if" statement is intended to make options sticky, in case the form isn't filled out properly on the first go-'round.)

 

And the handling bit is set up thus:

 

	// IF ONE OF THE RADIO BUTTONS IS SELECTED TO INDICATE A JOB TYPE,
// CONVERT THE RADIO BUTTON VALUE TO A FIELD VALUE FOR THE MYSQL DATABASE
// (and make the Job Type sticky)
if (isset($_POST['jobType'])) {
	$jobType = $_POST['jobType'];
	$jobTypeSubmitted = TRUE;
	// BASED ON THE TYPE OF JOB, SUBMIT DATA TO THE APPROPRIATE FIELDS
	if ($jobType == 'postJob') {
		$postJob = '1';
		$postOrdered = 'now()';
		$forsaleJob = '0';
		$forsaleOrdered = '0';
		$soldJob = '0';
		$soldOrdered = '0';
		$postpickupJob = '0';
		$postpickupOrdered = '0';
	}
	elseif ($jobType == 'forsaleJob') {
		$postJob = '0';
		$postOrdered = '0';
		$forsaleJob = '1';
		$forsaleOrdered = 'now()';
		$soldJob = '0';
		$soldOrdered = '0';
		$postpickupJob = '0';
		$postpickupOrdered = '0';
	}
	elseif ($jobType == 'soldJob') {
		$postJob = '0';
		$postOrdered = '0';
		$forsaleJob = '0';
		$forsaleOrdered = '0';
		$soldJob = '1';
		$soldOrdered = 'now()';
		$postpickupJob = '0';
		$postpickupOrdered = '0';
	}
	elseif ($jobType == 'postpickupJob') {
		$postJob = '0';
		$postOrdered = '0';
		$forsaleJob = '0';
		$forsaleOrdered = '0';
		$soldJob = '0';
		$soldOrdered = '0';
		$postpickupJob = '1';
		$postpickupOrdered = 'now()';
	}
}

 

(This is all part of a sub-conditional based on if(isset($_POST['submit'])).)

 

Yet none of my radio values are being passed to the database.  They worked fine when there were four checkbox inputs on the page, but mapping the radio buttons to the MySQL fields this way doesn't seem to work.

Your code for setting the values is pretty inefficient. I don't see where you actually submit the values to the database. I am assuming you have a query later on in your script. Have you at least put in some echos to see what values are being set?

 

Here is a more efficient method of setting the values:

 

<?php

if (isset($_POST['jobType'])) {

    $jobType = $_POST['jobType'];

    $postJob =     ($jobType=='postJob')?'1':'0';
    $postOrdered = ($jobType=='postJob')?'now()':'0';
    $forsaleJob =     ($jobType=='forsaleJob')?'1':'0';
    $forsaleOrdered = ($jobType=='forsaleJob')?'now()':'0';
    $soldJob =     ($jobType=='soldJob')?'1':'0';
    $soldOrdered = ($jobType=='soldJob')?'now()':'0';
    $postpickupJob =     ($jobType=='postpickupJob')?'1':'0';
    $postpickupOrdered = ($jobType=='postpickupJob')?'now()':'0';

}

?>

 

I would suggest putting an echo right after that code to display the value of all those variables to see if the values are being set properly. If so, you need to look at the code that makes the db update

Thanks, MJ! I knew there was a better way to set the values, but hadn't bothered to look it up yet. (That would've been in my next round of questions.) Since you were kind enough to point it out, I went ahead and incorporated your code into the page.

 

The actual problem seems to have been in another section of the file. The original page was called submitJob.php, but I was working from a modified version called submitJob2.php, which was saved in the same directory on the web. For some reason I can't identify, clicking the "submit" button was passing the values over to the original file (submitJob.php) instead of passing them back to submitJob2.php for processing and database submission. The original file didn't have assigned values for the radio buttons (they were checkboxes then), so the values weren't being written to the database. Instead, everything was being passed through as zeroes (the default behavior for the old form if the boxes weren't checked).

 

I'm still not sure why the submit button was behaving this way, but deleting the old file and renaming submitJob2.php to submitJob.php solved my database-writing issue, as well as problems with my form validation.

Also, I thought I'd leave the topic open for a bit while I'm working and surfing around here, in case anyone wants to post insights on the submit issue.  Otherwise, I'll mark it solved before I log off.

 

(As a newbie to the forum, someone please notify me if my etiquette is off-base.)

 

Thanks again!

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.