Problems with radio buttons and text field

Questions about programming languages and debugging
Post Reply
User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Problems with radio buttons and text field

Post by intern3t »

am trying to link the two together that if you click no on the radio button, it will activate the text field
<form>
<input type="radio" name="oRad2" onClick="javascript:this.form.text2.disabled=true;" /> Yes<br />
<input type="radio" name="oRad2" onClick="javascript:this.form.text2.disabled=false;" />No.If No-Reason
<textarea name="text2" rows="5" cols="30"></textarea>
</form>

this seem to solve the problem but how do i code the name to be passed to the database?
Last edited by intern3t on 07 Nov 2012, 16:27, edited 1 time in total.

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by Gogeta70 »

Narrow down your code to the area that's causing a problem. Nobody wants to read 100 lines of HTML to find your problem.
¯\_(ツ)_/¯ It works on my machine...

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by intern3t »

i have finally arrived at this:

THE PHP:

Code: Select all

<?php require_once('Connections/nawfia_test.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO testing (agree_yes, agree_no_reason) VALUES (%s, %s)",
                       GetSQLValueString($_POST['grp_two'], "text"),
                       GetSQLValueString($_POST['text_two'], "text"));

  mysql_select_db($database_nawfia_test, $nawfia_test);
  $Result1 = mysql_query($insertSQL, $nawfia_test) or die(mysql_error());
}
?>

THE HTML

Code: Select all

<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
    <input type="radio" name="grp_two" class="radclass cant" value="option_two" />
    Yes
    <br />
  <br />
  <input type="radio" name="grp_two" class="radclass can" value="option_other" />
  No. Give Reason
  <textarea name="text_two" class="textclass grp_two" rows="5" cols="30"></textarea>
    <br />
    <input type="submit" name="submit" value="submit">
    <input type="hidden" name="MM_insert" value="form1" />
</form>

<script type="text/javascript">
 $('.textclass').attr({disabled:true,enabled:false});
    
    $('.radclass.can').click(function(){
        var grp = $(this).attr('name');
        $('.' + grp).attr({disabled:false,enabled:true});
    });

    $('.radclass.cant').click(function(){
        var grp = $(this).attr('name');
        $('.' + grp).attr({disabled:true,enabled:false});
    });
	</script>
The problem is when i click on submit, it gives me

Code: Select all

Notice: Undefined index: text_two in C:\xampp\htdocs\NAWFIA\test.php on line 42
please i need help to resolve this.thanks

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by ayu »

Why are you disabling the textarea?
$('.textclass').attr({disabled:true,enabled:false});
"The best place to hide a tree, is in a forest"

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by intern3t »

cats wrote:Why are you disabling the textarea?
$('.textclass').attr({disabled:true,enabled:false});


Because i want it to be activated only when you want to give reasons.

if you select yes, it will be disabled, when you select no, the text area will be enabled for you to give reasons. Is there anything i should correct.please help.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by ayu »

Alright.

Well I haven't tried your code yet, but it looks to me like this wont work

The class of the textarea is
grp_two
While you are selecting "grp" here.
Or maybe I have missed something.
Is this all the code or have you made any changes since you posted it?
$('.' + grp).attr({disabled:true,enabled:false});
"The best place to hide a tree, is in a forest"

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by intern3t »

cats wrote:Alright.

Well I haven't tried your code yet, but it looks to me like this wont work

The class of the textarea is
grp_two
While you are selecting "grp" here.
Or maybe I have missed something.
Is this all the code or have you made any changes since you posted it?
$('.' + grp).attr({disabled:true,enabled:false});

i haven't made any changes.if you can recode mine, i will be glad.i just need the way forward or you can drop your suggestion.thanks

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by intern3t »

Now working!!!

i had to change this :

Code: Select all

$insertSQL = sprintf("INSERT INTO testing (agree_yes, agree_no_reason)
to

Code: Select all

@$insertSQL = sprintf("INSERT INTO testing (agree_yes, agree_no_reason)
i used @ to suppress the notice.i guess it doesn't matter since its a notice, not an error.or does it?

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: PROBLEM WITH RADIO BUTTONS AND TEXT FIELD

Post by ayu »

Well that depends ^^

If text_two is not defined then there might be another problem that makes it so that the data isn't submitted correctly.
But maybe that was the point from the beginning? That the textarea is disabled at some point and thus not submitted?

You might want to fix the error instead by checking if the data is submitted, and if not, insert a default value or something instead, as it might give you unwanted results later from the database.
"The best place to hide a tree, is in a forest"

Post Reply