user23 Posted August 22, 2012 Share Posted August 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/ Share on other sites More sharing options...
ialsoagree Posted August 22, 2012 Share Posted August 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371511 Share on other sites More sharing options...
Barand Posted August 22, 2012 Share Posted August 22, 2012 If there are several options and the user can only select one, use radio buttons (or selection menu). If there are several options and the user can select more than one, use checkboxes (or multiselect selection menu). Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371525 Share on other sites More sharing options...
user23 Posted August 22, 2012 Author Share Posted August 22, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371547 Share on other sites More sharing options...
Barand Posted August 22, 2012 Share Posted August 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371560 Share on other sites More sharing options...
user23 Posted August 22, 2012 Author Share Posted August 22, 2012 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? :/ Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371569 Share on other sites More sharing options...
Barand Posted August 22, 2012 Share Posted August 22, 2012 Define "not working". What isn't happening that should or what is happening that should not? Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371583 Share on other sites More sharing options...
user23 Posted August 22, 2012 Author Share Posted August 22, 2012 ha, sorry! Its not adding the user to the database :/ something is stopping the information from adding to the db Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371587 Share on other sites More sharing options...
Barand Posted August 22, 2012 Share Posted August 22, 2012 Perhaps because there are no insert or update queries in your code? Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371629 Share on other sites More sharing options...
user23 Posted August 23, 2012 Author Share Posted August 23, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371731 Share on other sites More sharing options...
user23 Posted August 23, 2012 Author Share Posted August 23, 2012 Would having the array on a seperate page to where the subjects are being echoed make a difference? anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/267432-dynamic-radio-buttons/#findComment-1371810 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.