Jump to content

dynamic radio buttons


user23

Recommended Posts

hey guys ok my issue is a have a register page where you enter data into textboxes and they are in an array which is sent to a db! all that is fine, however, now iv added a dropbox where you select a school, then once a school has been selected the subjects the school offers appears with radio buttons attached to them..my problem is i dont know how to add them to the db.. with the inputboxes it was easier as they were named

 

 

  if (empty($_POST) === false && empty($errors) === true) { //if post data is not empty and there are no errors we can register user
        $register_data = array(
            'username'       => $_POST['username'],
            'password'       => $_POST['password'],
            'first_name'             => $_POST['first_name'],
            'last_name'      => $_POST['last_name'],
            'email'                  => $_POST['email'],
                        'school'                 => $_POST['school'],
                        'email_code'         => md5($_POST['username'] + microtime())



        ); 

        register_user($register_data);
        header('Location: register.php?success'); 

        exit(); 

    }else if (empty($errors) === false){
        echo output_errors($errors);

    } 


?>
<form method="get" action="http://www.google.com/search">
<input type="text" name="q" size="30" x-webkit-speech />
<input type="submit" value="Google Search" />
</form>

<input type='text' onkeyup='ajax();' oninput='ajax();' placeholder='Search for a contact...' x-webkit-speech >



<form action="" method="post">

    <ul>
        <li>--------------Personal Details-----------------</li>
        <li>
        Username*:<br>
        <input type="text" name="username">
        </li>
        <li>
        Password*:<br>
        <input type="password" name="password">
        </li>
        <li>
        Password again*:<br>
        <input type="password" name="password_again">
        </li>
        <li>
        First Name*:<br>
        <input type="text" name="first_name">
        </li>
        <li>
        Last Name:<br>
        <input type="text" name="last_name">
        </li>
        <li>
        Email*:<br>
        <input type="text" name="email">
        </li>
        <li>
        School*:<br>
        <input type="text" name="school">
        </li>
        <li>
        --------------------Subjects Area ------------------
        </li>
        <html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">St Marys</option>
<option value="2">CBS</option>
<option value="3">Presentation, Athenry</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>
        <li>
            <input type="submit" value="register">
        </li>
    </ul>
</form>


<? 
}
include 'includes/overall/overallfooter.php'; ?>

 

However i dont know how you can send dynamic radio buttons??! Also the dropbox uses another php page..which i will add here

$con = mysql_connect('localhost', 'root', 'root');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("lr", $con);

$sql="SELECT subject_name FROM subjects WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='0' style='background-color: transparent; color: black;'>
<tr>
<th>Please Select your subjects</th>


</tr>";
echo "<br>";
while($row = mysql_fetch_array($result))
  {

  echo "<tr>";
  echo "$subject_name";
  echo "<td>" . $row['subject_name'] .  "</td>"; 
  echo "<td>"; echo "<input type='radio' name='$subject_name'>"; echo "</td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

18880_.php

Link to comment
https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/
Share on other sites

Your radio buttons need to have a value:

 

<input type="radio" name="radioButtonName" value="someValue" /> someValue <input type="radio" name="radioButtonName" value="someOtherValue" /> someOtherValue

 

Which value is selected will be stored in $_POST['radioButtonName'] in this case.

Thank you so much for your help guys. Ive made the adjustments you suggested but its still not adding to the db

 

register.php file

 

<?php
if (isset($_GET['success']) && empty($_GET['success'])) { //if success is added to end of url
echo 'You\'ve been registered successfully!';
} else {

if (empty($_POST) === false && empty($errors) === true) { //if post data is not empty and there are no errors we can register user
	$register_data = array(
		'username' 		 => $_POST['username'],
		'password'		 => $_POST['password'],
		'first_name'	         => $_POST['first_name'],
		'last_name'		 => $_POST['last_name'],
		'email' 		         => $_POST['email'],
            'school' 		         => $_POST['school'],
            'subject_name' 		         => $_POST['subject_name'],
            'email_code' 		 => md5($_POST['username'] + microtime())



	); 

	register_user($register_data);
	header('Location: register.php?success'); 

	exit(); 

}else if (empty($errors) === false){
	echo output_errors($errors);

} 


?>
<form method="get" action="http://www.google.com/search">
<input type="text" name="q" size="30" x-webkit-speech />
<input type="submit" value="Google Search" />
</form>

<input type='text' onkeyup='ajax();' oninput='ajax();' placeholder='Search for a contact...' x-webkit-speech >



<form action="" method="post">

<ul>
	<li>--------------Personal Details-----------------</li>
	<li>
	Username*:<br>
	<input type="text" name="username">
	</li>
	<li>
	Password*:<br>
	<input type="password" name="password">
	</li>
	<li>
	Password again*:<br>
	<input type="password" name="password_again">
	</li>
	<li>
	First Name*:<br>
	<input type="text" name="first_name">
	</li>
	<li>
	Last Name:<br>
	<input type="text" name="last_name">
	</li>
	<li>
	Email*:<br>
	<input type="text" name="email">
	</li>

	<li>
	--------------------Subjects Area ------------------
	</li>
	<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="school" onchange="showUser(this.value)">
<option value="">Select your school:</option>
<option value="1">St Marys, Charleville</option>
<option value="2">CBS, Charleville</option>
</select>
</form>
<br />
<div id="txtHint"><b>school/subjects will be listed here.</b></div>

</body>
</html>
	<li>
		<input type="submit" value="register">
	</li>
</ul>
</form>


<? 
}
include 'includes/overall/overallfooter.php'; ?>

 

getuser.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', 'root');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("lr", $con);

$sql="SELECT subject_name FROM subjects WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='0' style='background-color: transparent; color: black;'>
<tr>
<th>Please Select your subjects</th>


</tr>";
echo "<br>";
while($row = mysql_fetch_array($result))
  {
  
  echo "<tr>";
  echo "$subject_name";
  echo "<td>" . $row['subject_name'] .  "</td>"; 
  echo "<td>"; echo "<input type='checkbox' name='$subject_name' value='subject_name'>"; echo "</td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

 echo "<td>"; echo "<input type='checkbox' name='$subject_name' value='subject_name'>"; echo "</td>";

 

needs to be

 

 echo "<td>"; echo "<input type='checkbox' name='subject_name[]' value='$subject_name'>"; echo "</td>";

 

The selected subjects are then posted as an array

pretty sure this is supposed to add the data input to  db

	$register_data = array(
		'username' 		 => $_POST['username'],
		'password'		 => $_POST['password'],
		'first_name'	         => $_POST['first_name'],
		'last_name'		 => $_POST['last_name'],
		'email' 		         => $_POST['email'],
                        'school' 		         => $_POST['school'],
                        'subject_name' 		         => $_POST['subject_name'],
                        'email_code' 		 => md5($_POST['username'] + microtime())

 

it was adding to the db fine until i added the dropdown and checkbox part of the code!  :confused:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.