Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choose data from drop-down or field 1

Status
Not open for further replies.

nedstar1

IS-IT--Management
Mar 2, 2001
127
US
Hi folks,

I'm working on a project, and I have a quick question. OK, it really isn't that quick. The site is PHP/MySQL, and I've just added a javascript component - my question is related to the PHP side of things:

I want to add a couple of radio buttons and a text field to my name input form. It has a drop-down for title, a text field for firstname, one for MI, one for last name, and a drop-down for degree.

What I want to do is to give my users the option to enter a degree not found on the list. Since my desires happen at the client side, I've worked out in javascript how to use a radio button to select one or the other - the degree dropdown, or the degreealt text box. I've also got the radio buttons set so that the non-selected option is dynamically enabled/disabled. While I haven't worked it into the pages, yet, that code is here:
Code:
<html>
<head>
<script type="text/javascript">
}
function doitall()
{
var x=document.getElementById("mySelect")
x.disabled=true
document.forms[0].field.focus()
}
function undoitall()
{
var x=document.getElementById("mySelect")
x.disabled=false
}


</script>

</head>

<body>
<form>
<input type="radio" name="browser" onclick="undoitall()" value="Opera">
<select id="mySelect">
	<option>MD</option>
	<option>MD PhD</option>
	<option>RN</option>
        <option>RN BSN</option>
</select>
<input type="radio" name="browser" onclick="doitall()" value="Opera">Other:
<input type="text" name="field" size="30"> 
</form>
</body>

</html>

It's heavily based on something I found at W3schools.com, but I clunked it together, and it does what I need it to, or almost.

I'll use this script to select the drop-down or the text-field - now I need to know how to capture the correct data.


This is the code that retrieves the input and adds the incrementals:

Code:
<?php
$x = 1;

$submit_form = "<form action='recipients_add.php' method='post'>";

while ($x != 26){
	$title = $info["Title"]["$x"][""];
	$first_name = $info["First"]["Name"]["$x"];
	$middle_name = $info["Middle"]["Name"]["$x"];
	$last_name = $info["Last"]["Name"]["$x"];
	$degree = $info["Degree"]["$x"][""];
	
	if($first_name != ""){	
	$submit_form .= "
	<input type='hidden' name='Title-$x' value='$title'>
	<input type='hidden' name='First-Name-$x' value='$first_name'>
	<input type='hidden' name='Middle-Name-$x' value='$middle_name'>
	<input type='hidden' name='Last-Name-$x' value='$last_name'>
	<input type='hidden' name='Degree-$x' value='$degree'>";
	}
	
	$x = $x + 1;
}

$x = 1;

$submit_form .= "<input type='submit' value='Submit Names!'></form>";
?>


This is the $form data that collects the data for the previous code snippet:
Code:
<tr>
<td><div align='center'>1</div></td>
<td><div align='center'>
<select name='Title-1' id='Title-1' value='$title_1'>

<option value='Dr.'>Dr.</option>
<option value='Mr.'>Mr.</option>
<option value='Mrs.'>Mrs.</option>
<option value='Ms.'>Ms.</option>
<option value='' selected='selected'>None</option>
</select>

</div></td>
<td><div align='center'>
<input name='First-Name-1' type='text' id='First-Name-1' value='$first_name_1' size='25' />
</div></td>
<td><div align='center'>
<input name='Middle-Name-1' type='text' id='Middle-Name-1' value='$middle_name_1' size='5' maxlength='5' />
</div></td>
<td><div align='center'>
<input name='Last-Name-1' type='text' id='Last-Name-1' value='$last_name_1' size='25' />
//cell and radio button to select drop-down goes here
</div></td>
<td><div align='center'>
<select name='Degree-1' id='Degree-1' value='$degree_1'>
<option value='MD'>MD</option>
<option value='MD, PhD'>MD, PhD</option>
<option value='RN'>RN</option>
<option value='BSN'>BSN</option>
<option value='' selected='selected'>None</option>
</select>
//cell and radio button for "other" field selection go here
//cell and "other" field go here
</div></td>
</tr>


This is the code on recipients_add.php that processes the input from the input page:
Code:
<?php
session_start();
include("include.php");

foreach ($_POST as $name => $value){
	// Take away the number at the end
	$namen = explode("-", $name);
	$name = $namen["0"];

	// Then we make it two words
	$name = str_replace("-", " ", $name);
	
	// Get the number
	$number = $namen["2"];
	$numbert = $namen["1"];
	
	if ($number < 26 ){
		$info["$name"]["$numbert"]["$number"] .= "$value";
	}
	
}

$x = 1;
$id = $_SESSION["id"];
$oid = $_SESSION["order_id"];


while ($x != 26){
	$title = $info["Title"]["$x"][""];
	$first_name = $info["First"]["Name"]["$x"];
	$middle_name = $info["Middle"]["Name"]["$x"];
	$last_name = $info["Last"]["Name"]["$x"];
	$degree = $info["Degree"]["$x"][""];
	
	if ($first_name != ""){
		
		$sql_insert = "INSERT INTO tbl_recips (userID, court_title, firstName,  middleInit, lastName, degree, orderID)
		VALUES
		('$id', '$title', '$first_name', '$middle_name', '$last_name', '$degree', '$oid')"; 
		
		$do_insert = $mysql->query($sql_insert);
	
        }
        
        $x = $x + 1;
 } 

 
    $sql_update = "UPDATE tbl_users SET order_complete=1 WHERE userID='$id'";
    $do_update = $mysql->query($sql_update);
?>

Basically, they are limited to a max of 25 names, and the insert shuts itself off if the first name field is blank (this is a required entry, and is validated in a previous step, so a blank value means there is no entry for that line . . . anyhow, it works.

The site uses a template class - this explains why there is some weird code here and there. There are also custom classes for email and mysql functions - the syntax works as shown.

Before anyone asks, I'm anovice coder, just above newbie, and this site was coded by a contractor. I've been asked to (try to) make enhancements after he was unable to commit to further development work. I've read a few books and read LOTS of FAQs, and I pretty much understand what is happening at each step. That said, I know I can use an IF...ELSE statement to determine which field is selected, but I do not know how to subsequently pass the variable of the field associated with the radio button off as the captured value for "degree."

I've tried to provide as much as possible - maybe too much, but I really need the help - we're supposed to go liove on Monday, and I'm hitting the wall.

If anyone can explain the steps I need to take, how I need to nest my if..elses, where I need to insert the js commands (I'm assuming it's odone inline, like the script example at top, anythig at all that would be of use to me, please chime in.

TIA, everyone.

Regards,
Nedstar1
 
Do the degreevariables have a different name , I assume they have?? and why work with radiobuttons ?? Personaly I would add an field under the select list with "if not in list type here" comment

if so you can make an if statement after submitting
and before the insert statement

if ($degreetext <> "") {$degree1=$degreetext;}

 
Hi Hos2,

Thanks for the reply.

The degree variables do have different names, so your if statement is a good idea. As for the radio buttons, I'm trying to take some of the burden of understanding away from my users. ;) The radio buttons make it graphically explicit that you can have one or the other, but not both - at least, that's my logic. Also, it's a pretty big form, with five fields and 25 lines. I thought adding a sub-row to each might ruin the look of the form, and I have little time for a redesign.

I'm going to mock up a one-row form and try the if statement - the validation scripts are on an intermediate page with no html - it goes from recipients_entry - recipients_add (no html, only processing) to recipients_confirmation to recipients_added. Since they can return to recipients_entry to make edits from recipients_confirmation, I need to insert it into the intermediate page - recipients_add - so that it will be part of the editable entry, right? Otherwise, I'd be selecting which field to capture well after the fact . . .

That seems so obvious now that I type it out. I suppose that's one way to figure things out. . .

Thanks again. You get a star for looking at my post seriously, instead of seeing massive blocks of code posted by a novice and not rolling your eyes and moving along the forum. You've been a real help.

Regards,
Nedstar1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top