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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

Thanks very much for your help again!

Its still not working though and I think its cause most of the user input is on the register page and that part is fine! however from the dropbox on data is being sent to the getuser.php but i dont know how to fix this? :/

Link to comment
Share on other sites

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:

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.